Course Content
Introduction to Redis
Introduction to Redis
AOF Persistence
The illustration shows how Redis operates using AOF. A client command is first executed in memory for fast data access, and then the command is written to a file on disk to ensure reliable data recovery in case of a failure.
How AOF Works
When AOF mode is enabled, every write command sent to the server is saved to a file. Upon restarting Redis, the commands from the AOF file are read and used to restore the dataset.
Over time, this file can grow significantly in size as it contains the entire history of key changes. To address this, Redis periodically rewrites the file, removing unnecessary commands and retaining only the current state of each key. For instance, if we use a key named total and modify its value multiple times, the original AOF file might look like this:
After rewriting, Redis will keep only the latest value for the key:
When Redis appends a new command to the AOF file, the operating system initially saves it in a buffer before writing it to disk at specific intervals. If a power outage occurs, data still in the buffer may be lost. To minimize this risk, Redis flushes the buffer every second by default. You can configure it to write data to the disk immediately after each command, but this significantly slows down operations.
How to Enable AOF
To enable AOF, update the redis.conf
file with the following settings:
When configuring AOF in Redis, you can choose how frequently data is synchronized to the disk. This affects both system performance and reliability:
appendfsync always
– sync after every operation (slower but most reliable);appendfsync everysec
– sync once per second (optimal balance of performance and durability);appendfsync no
– data remains in memory until flushed by the system (fast but risky).
Difference Between AOF and RDB
Configuring Combined Persistence
You can enable both RDB and AOF simultaneously to combine their strengths. This approach provides a reliable backup (RDB) while minimizing data loss (AOF).
Summary
Persistence in Redis strikes a balance between performance and data reliability, allowing you to choose the approach that best suits your needs. If your priority is high performance, RDB is the ideal choice. On the other hand, if minimizing data loss is essential, AOF is more suitable. For those requiring maximum reliability, using both methods together provides the best results.
1. Which Redis persistence method minimizes data loss?
2. When should you use both RDB
and AOF
persistence in Redis?
Thanks for your feedback!