How to Configure RDB Persistence in Redis for Data Durability
To configure
RDB persistence in Redis, enable snapshotting by setting save directives in the redis.conf file. Redis will then save snapshots of your dataset to disk at specified intervals, creating .rdb files for data durability.Syntax
The main configuration for RDB persistence is done in the redis.conf file using the save directive. Each save line specifies when Redis should create a snapshot based on the number of seconds and the number of changes (writes) to the dataset.
save <seconds> <changes>: Save the DB if at leastchangeskeys changed withinseconds.dbfilename <filename>: Name of the RDB file (default isdump.rdb).dir <directory>: Directory where the RDB file is saved.
conf
save 900 1 save 300 10 save 60 10000 dbfilename dump.rdb dir /var/lib/redis
Example
This example shows a Redis configuration snippet that saves a snapshot if at least 1 key changes in 900 seconds, or 10 keys change in 300 seconds, or 10000 keys change in 60 seconds. The snapshot is saved as dump.rdb in /var/lib/redis.
conf
# redis.conf snippet save 900 1 save 300 10 save 60 10000 dbfilename dump.rdb dir /var/lib/redis
Common Pitfalls
Common mistakes when configuring RDB persistence include:
- Not setting any
savedirectives, which disables snapshotting. - Setting intervals too long or changes too high, causing infrequent snapshots and risking data loss.
- Incorrect
dirpath causing Redis to fail saving the RDB file. - Not restarting Redis after changing
redis.conf, so changes don't take effect.
Always verify permissions on the snapshot directory and test persistence by restarting Redis and checking if data is restored.
Quick Reference
| Directive | Description | Example |
|---|---|---|
| save | Sets snapshot intervals based on seconds and changes | save 900 1 |
| dbfilename | Sets the name of the RDB file | dbfilename dump.rdb |
| dir | Sets the directory to save the RDB file | dir /var/lib/redis |
Key Takeaways
Enable RDB persistence by setting
save directives in redis.conf.Choose snapshot intervals balancing data safety and performance.
Set correct
dir and dbfilename for saving snapshots.Restart Redis after configuration changes to apply them.
Verify snapshot files exist and Redis restores data after restart.