Redis provides the AOF (Append-Only File) method for permanently storing data on disk. However, as time goes on and the size of the AOF file increases, it can lead to performance degradation and disk space issues. To address this, Redis offers the AOF Rewrite (rewrite, compression) feature. In this post, we will take a closer look at what AOF Rewrite is, how it works, and how to configure it.
1. What is AOF (Append-Only File)? Why is Rewrite necessary?
Redis uses the AOF file to sequentially log all write operations for the purpose of permanent storage. For example, commands like SET key value
are recorded in the AOF file each time they are executed. This allows data to be restored even if the Redis server restarts.
However, over time, duplicate commands for the same key continue to be added to the AOF file, resulting in increased size and longer recovery times.
To resolve this, the AOF Rewrite (compression, cleaning up) feature is provided.
2. What is AOF Rewrite?
AOF Rewrite is the process of replacing the existing appendonly.aof
file with an optimized new file. It creates a new file while keeping the existing AOF file intact and removes unnecessary commands to reduce size. The key part of this description is that it refers to replacing the existing file after optimization, which summarizes how rewrite operates.
๐ How AOF Rewrite Works
- Redis does not directly modify the existing AOF file but creates a new AOF file.
- The new file contains only the minimum commands reflecting the latest state.
- For example, if the existing AOF file looks like this:
SET key1 "hello"
SET key1 "world"
SET key1 "final"
After AOF Rewrite, it is optimized as follows:
SET key1 "final"
Thus, once the new appendonly.aof.tmp
file is completed, the existing file is deleted and replaced with the new file. (The tmp file replaces the AOF file.)
3. How to Configure AOF Rewrite
โ Automatic AOF Rewrite Configuration
Redis can be set to execute AOF Rewrite automatically. You can configure it in redis.conf
as follows:
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
auto-aof-rewrite-percentage 100
: Rewrite will be performed when the file size after the last AOF Rewrite increases by 100% (doubles).auto-aof-rewrite-min-size 64mb
: Rewrite will be performed only when the AOF file is at least 64MB.
In other words, if the existing AOF file size is 50MB and later becomes 100MB, a rewrite will be executed automatically.
It is advisable to always set these two options together. If you do not specify the auto-aof-rewrite-min-size
value, unnecessary rewrites can occur the moment the previous AOF file size increases from 20MB to 40MB. Thus, by setting auto-aof-rewrite-min-size
, you can ensure that rewrites are only executed when the size exceeds a specific threshold.
In summary, if the existing AOF file size is 50MB and later becomes 100MB, a rewrite will automatically execute.
โ Execute AOF Rewrite Manually
Executing the command below will make Redis perform AOF Rewrite immediately.
redis-cli BGREWRITEAOF
When this command is executed, Redis creates a new AOF file in the background, and the existing file is replaced during this process.
4. Results of AOF Rewrite
When AOF Rewrite is performed, the following files are generated in Redis's data directory (e.g., /var/lib/redis
).
ls /var/lib/redis
Example output:
appendonly.aof
appendonly.aof.tmp # temporary file created during rewrite
appendonly.aof.tmp
: The new optimized AOF file generated during AOF Rewrite.appendonly.aof
: After the rewrite is complete, the existing file is deleted and replaced with the new file.
๐จ AOF Rewrite does not compress the existing AOF file; it creates a new AOF file to replace the existing one.
5. When Should AOF Rewrite Be Utilized?
โ When the AOF file size becomes too large and takes up a lot of disk space - Redis continuously logs data, so the file size increases over time. - Cleaning up unnecessary commands can reduce disk usage.
โ When you want to speed up recovery on Redis restart - A large AOF file requires Redis to execute all commands to recover data upon restart, which may take a long time. - Performing AOF Rewrite reduces the file size and speeds up recovery.
โ When you wish to optimize Redis performance - A large AOF file can degrade Redis performance. - Regularly executing AOF Rewrite allows Redis to operate more stably.
6. Differences Between RDB Snapshots and AOF Rewrite
Comparison Item | AOF (Append-Only File) | RDB (Redis Database) |
---|---|---|
Storage Method | Records all write operations | Dumps all data at regular intervals |
Recovery Speed | Slow (requires execution of all commands) | Fast (binary dump recovery) |
Data Loss Possibility | Low (depends on fsync settings) | Possible (depends on snapshot intervals) |
File Size | Large (all operations are recorded) | Small (only the latest state is stored) |
Performance Impact | High (all operations are logged) | Low (periodic dumping) |
Suitable Use Case | When high durability is needed | When fast recovery is needed |
๐ In Redis environments, it is common to configure both AOF and RDB together for optimal performance and stability.
7. Conclusion and Summary
- Redis's AOF Rewrite is performed to reduce the AOF file size and improve recovery speed.
- It operates by creating a new optimized AOF file to replace the existing file, rather than directly modifying the existing AOF file.
- By appropriately setting
auto-aof-rewrite-percentage
andauto-aof-rewrite-min-size
, automatic AOF Rewrite can be performed, allowing for efficient management of performance and storage space. - Using it in conjunction with RDB snapshots can optimize Redis's durability and performance simultaneously.
๐ Make sure to appropriately utilize AOF Rewrite to optimize performance while operating Redis! ๐
By searching for redis in the search box on the right, you can find many more posts regarding Redis configurations.
If you need clarification of the concepts of RDB
and AOF
, I recommend checking the following post.
Redis Part 2: Data Preservation Options in Redis - RDB vs AOF
Add a New Comment