Redis Persistence: RDB Snapshots
What you'll learn: How Redis uses RDB snapshots to create point-in-time backups of in-memory data and recover from failures.
The Persistence Challenge
Redis stores data in memory for speed, but memory is volatile—system crashes or restarts would lose everything. RDB (Redis Database) snapshots solve this by periodically saving the entire dataset to disk as a compact binary file.
How RDB Snapshots Work
Think of RDB like taking a photograph of your entire dataset at a specific moment. Redis uses a clever fork-based approach:
- Fork Process: Redis calls the system
fork()command, creating a child process that shares the same memory view - Copy-on-Write: The parent process continues serving requests while the child writes the snapshot to disk. Thanks to copy-on-write, only modified memory pages are duplicated
- Atomic Write: The snapshot is written to a temporary file, then atomically renamed to replace the old snapshot
Configuration Example
Redis triggers snapshots based on rules like:
- Save after 60 seconds if 1000+ keys changed
- Save after 300 seconds if 10+ keys changed
The result is a compact .rdb file containing the compressed dataset.
Recovery Process
On restart, Redis simply loads the .rdb file back into memory. Recovery is fast because the format is optimized for bulk loading.
Trade-offs
Advantages: Compact files, minimal performance impact during normal operation, fast restarts.
Disadvantages: Potential data loss (everything since the last snapshot), can be CPU/memory intensive on large datasets during fork.
Key Takeaway: RDB snapshots provide durable point-in-time backups of Redis data using efficient fork-based writes, trading some risk of recent data loss for performance and compact storage.