How to Configure AOF Persistence in Redis for Data Durability
To configure
AOF persistence in Redis, enable it by setting appendonly yes in the redis.conf file. You can customize how often Redis writes to the AOF file using appendfsync options like always, everysec, or no.Syntax
The main configuration for AOF persistence is done in the redis.conf file with these directives:
appendonly yes|no: Enables or disables AOF persistence.appendfilename <filename>: Sets the name of the AOF file (default isappendonly.aof).appendfsync always|everysec|no: Controls how often Redis writes AOF data to disk.
conf
appendonly yes appendfilename appendonly.aof appendfsync everysec
Example
This example shows how to enable AOF persistence with Redis writing to disk every second for a good balance between performance and durability.
conf
# redis.conf snippet appendonly yes appendfilename appendonly.aof appendfsync everysec
Common Pitfalls
Common mistakes when configuring AOF persistence include:
- Forgetting to set
appendonly yes, so AOF is not enabled. - Using
appendfsync alwayswhich can slow Redis due to frequent disk writes. - Not restarting Redis after changing
redis.conf, so changes do not apply. - Ignoring AOF file growth, which can become large without periodic rewriting.
conf
## Wrong: AOF disabled appendonly no ## Right: AOF enabled with balanced fsync appendonly yes appendfsync everysec
Quick Reference
| Directive | Description | Typical Values |
|---|---|---|
| appendonly | Enable or disable AOF persistence | yes, no |
| appendfilename | Name of the AOF file | appendonly.aof |
| appendfsync | Frequency of fsync to disk | always, everysec, no |
| auto-aof-rewrite-percentage | Rewrite AOF when file grows by % | 100 |
| auto-aof-rewrite-min-size | Minimum AOF size to trigger rewrite | 64mb |
Key Takeaways
Enable AOF by setting 'appendonly yes' in redis.conf to persist data.
Use 'appendfsync everysec' for a good balance of durability and performance.
Restart Redis after changing AOF settings to apply them.
Monitor AOF file size and configure automatic rewriting to prevent large files.
Avoid 'appendfsync always' unless strict durability is required, as it impacts performance.