Skip to content

Redis persistence Explanation - RDB and AOF

homepage-banner

RDB

In redis.conf, configure bgsave to be automatically triggered if more than y modifications occur within x seconds.

save x y

Manually Trigger

# Block the whole process
save
# Block fork and non-block the rest
bgsave
# Timestamp of the last time RDB was generated
lastsave

Auto Trigger

  1. Trigger configuration for generating RDB
  2. Full replication
  3. debug reload to reload
  4. Execute shutdown if AOF is not enabled

Generate RDB Process

  1. Determine if there are any child processes currently executing
  2. Parent process forks and creates a child process (20ms/GB)
  3. The child process creates an RDB file, generates a snapshot based on the parent process memory, and replaces the original RDB file.
  4. The child process sends a signal to the parent process, updating the info persistence
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1631974350
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0

Ops Command

# Modify the RDB file directory
config set dir
# Modify the RDB file name
config set dbfilename
# Whether the RDB is compressed, default is yes
config set rdbcompression yes|no
# Check for errors in the RDB file
redis-check-dump

AOF

Configure in redis.conf

# Enable
appendonly yes
# Flush frequency, default everysec
appendfsync everysec|no|always

Generate AOF Process

  1. Append all write commands to aof_buf
  2. AOF buffer flushes according to policy
  3. Loaded when Redis is started

AOF Rewrite

# Manually trigger
bgrewriteaof
# Configuration trigger
## Percentage growth
auto-aof-rewrite-percentage 100
## Default is 64M
auto-aof-rewrite-min-size 67108864
## Batch write control, the default is yes, write 32MB at a time
aof-rewrite-incremental-fsync yes|no
## Whether to disable fsync during AOF rewrite, default is no
no-appendfsync-on-rewrite no|yes

AOF Rewrite Process

  1. Determine if there are any child processes currently executing
  2. Parent process forks and creates a child process, then continues to respond to requests and saves write requests to the AOF rewrite buffer
  3. The child process merges the write requests into the new AOF file based on the memory snapshot
  4. The child process sends a signal to the parent process to update the info persistence
  5. The parent process writes the AOF rewrite buffer data to the new AOF file
  6. Replace the original AOF file with the new AOF file
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:0
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:311296

Fix Broken AOF

Fix broken AOF
redis-check-aof --fix appendonly.aof

Load Order

  1. If AOF is enabled and an AOF file exists, it is loaded and started after AOF is enabled.
  2. If AOF is enabled and no AOF file exists, it is loaded and started after RDB is loaded.
  3. If AOF is not enabled, it is loaded and started after RDB is loaded.
Feedback







Small world. Big idea!
  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback me#imzye.com, replace # with @