Redis is an in-memory data store, which is fundamentally volatile. When the Redis server restarts, all data may be lost, and to mitigate this, Redis offers two data persistence methods: RDB (Redis Database Snapshot) and AOF (Append-Only File). In this post, we will examine the characteristics and differences of both methods, along with their configuration approaches, pros, and cons.
1. RDB (Redis Database Snapshot)
RDB is a method where Redis periodically saves data stored in memory as snapshots to disk. Redis generates RDB files according to the set intervals, allowing data recovery upon server restart.
Main Features of RDB
- Saving Interval: The interval for storing data to disk can be set through the
save
option in theredis.conf
file. For example,save 60 1000
creates a snapshot if more than 1000 keys change within 60 seconds.
save 60 1000
- File Location: RDB files are typically stored in the directory specified by
dir
, with the default path being/var/lib/redis
and the filenamedump.rdb
.
dir /var/lib/redis/
dbfilename dump.rdb
Advantages of RDB
- High Performance: The RDB method does not significantly affect Redis's performance, and if the snapshot interval is long, it does not consume much CPU resources either.
- Small File Size: Since the entire data is recorded as snapshots periodically, the file size is relatively small and backup management is easier.
Disadvantages of RDB
- Risk of Data Loss: Because the RDB method saves data periodically, if Redis shuts down unexpectedly, data after the last snapshot may be lost.
- Recovery Speed: RDB saves all data at once in a snapshot, which can take longer when recovering large amounts of data.
Appropriate Use Cases for RDB
RDB is suitable for cases where periodic backups of cache data are acceptable. It is advantageous when periodic data backups are needed rather than real-time work queues, allowing simple data persistence while maintaining the performance of the Redis server.
2. AOF (Append-Only File)
AOF provides continuous data persistence by sequentially recording all write operations to disk. The AOF file captures all data changes, enabling complete recovery even after a restart of the Redis server.
Main Features of AOF
- Command Logging Method: AOF files log all data change commands. With each write command, the changes are stored on disk, ensuring all change history is recorded.
- File Location and Name: AOF files are stored with the name specified in the
appendfilename
option. The default isappendonly.aof
, and they are stored in the directory specified by thedir
setting.
dir /var/lib/redis/
appendonly yes
appendfilename appendonly.aof
- Synchronization Interval: The
appendfsync
option allows you to set the frequency of data writes to disk.
appendfsync everysec # Synchronize every second
Advantages of AOF
- Data Persistence: By writing all write operations to disk, data can be recovered up to the last recorded state even if the server crashes unexpectedly.
- Reading Commands Not Recorded: AOF only logs commands that change data, thus saving space by excluding unnecessary read commands.

Disadvantages of AOF
- Potential Performance Degradation: Recording all write operations to disk can lead to performance issues, especially if
appendfsync
is set toalways
. - Increased File Size: If data changes frequently, the AOF file can grow significantly. To address this, Redis can compress the AOF file using the BGREWRITEAOF command.
Appropriate Use Cases for AOF
AOF is useful for task queues or applications where data persistence is crucial. In scenarios where data retention is important, using a periodic synchronization setting (everysec
) can balance performance and data preservation.
3. Using RDB and AOF Together
In Redis, you can use both RDB and AOF methods simultaneously to maintain a balance between data persistence and performance. When both methods are enabled, Redis prioritizes AOF files for recovery.
- RDB: Provides fast and simple data storage and basic backups in snapshot form.
- AOF: Records all write operations, enhancing data persistence and minimizing the risk of data loss.
4. Example Redis Configuration
Below is an example configuration for using RDB and AOF simultaneously while maintaining appropriate performance and data persistence.
# Redis basic directory and file settings
dir /var/lib/redis/
dbfilename dump.rdb
appendfilename appendonly.aof
# RDB snapshot settings (save snapshot if more than 1000 keys change every 60 seconds)
save 60 1000
# AOF settings
appendonly yes
appendfsync everysec # Synchronize to disk every second
# AOF file rewrite settings (automatically rewrite when filesize exceeds 64MB and increases by 100%)
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
Conclusion
In this post, we explored the two data persistence methods in Redis: RDB and AOF. Both methods differ in terms of preservation and performance, so it is crucial to choose the appropriate method based on the characteristics of your application. In the next part, we will discuss Redis Memory Management and Performance Optimization, introducing ways to efficiently utilize Redis memory.
Add a New Comment