Understanding Redis Persistence: RDB and AOF Mechanisms
This article explains how Redis, an in‑memory key‑value store, ensures data durability through two persistence methods—RDB snapshotting and AOF command logging—detailing their commands, file structures, configuration options, performance trade‑offs, and rewrite processes.
Redis is an in‑memory key‑value store, and because its data is lost on power‑off, it provides persistence mechanisms to write data to disk.
There are two persistence methods: RDB (snapshotting) and AOF (append‑only file). RDB creates a compressed snapshot of the dataset, while AOF logs every write command.
RDB can be triggered with the SAVE (blocking) or BGSAVE (non‑blocking) commands. SAVE blocks the server until the snapshot is written, whereas BGSAVE forks a child process to perform rdbSave() while the parent continues serving clients.
Both commands ultimately call the same function rdbSave() . The article shows pseudo‑code for SAVE and BGSAVE , illustrating the fork‑and‑wait logic:
def save():
rdbSave()
def BGSAVE():
# create child process
pid = fork()
if pid == 0:
# child creates RDB file
rdbSave()
# signal parent when done
signal_parent()
elif pid > 0:
# parent continues handling requests and polls for signal
handle_request_and_wait_signal()
else:
# handle fork error
handle_fork_error()The RDB file format consists of a 4‑byte version header ( db_version ), a databases section containing key‑value pairs, an EOF marker, and an 8‑byte check_sum used to verify file integrity.
AOF records every write operation in the Redis protocol format. Example commands ( SET , SADD , RPUSH ) and the resulting AOF content are shown. When the server starts, it replays the AOF to restore the dataset.
The server’s appendfsync configuration determines how often the AOF buffer ( aof_buf ) is flushed and synced to disk. Three options are described:
always : write and sync on every event loop (safest but slowest).
everysec : write on every loop and sync at most once per second (default, good balance).
no : write on every loop but let the OS decide when to sync (fastest but riskier).
AOF rewrite creates a compact AOF file by reading the current dataset instead of the existing log, eliminating redundant commands. The rewrite can be performed in a child thread and handles large collections by splitting them into multiple commands.
Overall, the article explains how Redis persistence works, the trade‑offs between RDB and AOF, and how configuration affects durability and performance.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.