0
0
RedisConceptBeginner · 3 min read

What is AOF in Redis: Explanation and Usage

In Redis, AOF (Append Only File) is a persistence method that logs every write operation received by the server. It helps Redis recover data by replaying these commands after a restart, ensuring durability and minimizing data loss.
⚙️

How It Works

AOF in Redis works like a diary where every change you make to the database is written down as a command. Imagine you keep a notebook and every time you add or change something, you write down exactly what you did. If your computer crashes, you can read the notebook from the start and repeat all the steps to get back to the same state.

Redis appends each write command to the AOF file immediately or in batches, depending on the configuration. When Redis restarts, it reads the AOF file and replays all the commands in order to restore the data exactly as it was before the crash. This method is reliable because it records every change, but the file can grow large over time, so Redis also supports rewriting the AOF file to keep it compact.

💻

Example

This example shows how to enable AOF persistence in Redis and check its status.

redis
127.0.0.1:6379> CONFIG SET appendonly yes
OK
127.0.0.1:6379> CONFIG GET appendonly
1) "appendonly"
2) "yes"
127.0.0.1:6379> SET user:1 "Alice"
OK
127.0.0.1:6379> GET user:1
"Alice"
Output
OK 1) "appendonly" 2) "yes" OK "Alice"
🎯

When to Use

Use AOF when you want a durable Redis database that minimizes data loss in case of crashes. It is ideal for applications where every write operation is important, such as session storage, real-time analytics, or caching with persistence.

AOF is preferred when you need a balance between performance and data safety because it can be configured to write data to disk every second or after every command. However, if you want faster recovery and smaller files, you might consider Redis's RDB snapshots or a combination of both.

Key Points

  • AOF logs every write command to a file for durability.
  • It allows Redis to recover data by replaying commands after restart.
  • AOF file can grow large but can be rewritten to save space.
  • Configurable to balance between performance and data safety.
  • Useful for applications needing minimal data loss.

Key Takeaways

AOF is a Redis feature that logs every write operation for data persistence.
It helps recover data by replaying commands after a server restart.
AOF can be configured for different durability and performance needs.
It is best used when minimizing data loss is critical.
The AOF file can be compacted to manage its size.