RDB Snapshotting
Redis is an in-memory database, but it provides two primary mechanisms to ensure your data survives a server restart. The first of these is RDB (Redis Database) Snapshotting.
1. What is RDB?
RDB persistence performs point-in-time snapshots of your dataset at specified intervals.
- Compact: RDB is a single, compact file representing the state of Redis at a specific moment.
- Fast Recovery: Because it’s a direct representation of the in-memory data, restarting from an RDB file is much faster than replaying logs.
- Minimal Impact: The process of creating an RDB file happens in a child process (via
fork()), so the main Redis thread stays responsive to clients.
2. Triggering a Snapshot
- SAVE: A synchronous command that blocks Redis while the snapshot is created. Avoid this in production!
- BGSAVE: An asynchronous command that forks a child process to handle the write.
- Config-based: You can tell Redis to automatically snapshot based on a rule:
save 900 1(save if 1 key changed in 900 seconds).
3. The Fork() Mechanism
When you run BGSAVE, Redis calls the operating system’s fork() function.
- A child process is created. It has a copy-on-write (COW) view of the parent’s memory.
- The child writes the memory contents to a temporary RDB file.
- Once finished, it replaces the old RDB file with the new one.
[!WARNING] While
fork()is efficient, if your Redis instance consumes 50GB of RAM and you don’t have enough swap space or free memory, the fork might fail or cause the system to swap, significantly slowing down performance.
4. Interactive: Snapshot Simulator
Trigger a background save and see how the child process handles the work.
Main Process
Handling Requests
Child Process
Idle
System: Up
5. Pros and Cons
- Pros: Perfect for disaster recovery and cloud backups. Extremely fast restarts.
- Cons: Potential for data loss. If Redis crashes between snapshots, all data since the last snapshot is lost. For maximum durability, you need AOF.