Redis persistence Explanation - RDB and AOF
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
- Trigger configuration for generating
RDB
- Full replication
debug reload
to reload- Execute
shutdown
ifAOF
is not enabled
Generate RDB Process
- Determine if there are any child processes currently executing
- Parent process
forks
and creates a child process (20ms/GB) - The child process creates an
RDB
file, generates a snapshot based on the parent process memory, and replaces the originalRDB
file. - 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
- Append all write commands to
aof_buf
AOF
buffer flushes according to policy- 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
- Determine if there are any child processes currently executing
- Parent process
forks
and creates a child process, then continues to respond to requests and saves write requests to the AOF rewrite buffer - The child process merges the write requests into the new
AOF
file based on the memory snapshot - The child process sends a signal to the parent process to update the
info persistence
- The parent process writes the AOF rewrite buffer data to the new
AOF
file - Replace the original
AOF
file with the newAOF
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
- If
AOF
is enabled and anAOF
file exists, it is loaded and started afterAOF
is enabled. - If
AOF
is enabled and noAOF
file exists, it is loaded and started afterRDB
is loaded. - If
AOF
is not enabled, it is loaded and started afterRDB
is loaded.
Leave a message
Disclaimer
- 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 @