AOF (Append-Only File) Persistence Model
AOF logs all write operations to a file in a human-readable format. This file contains a record of all the write operations performed on the dataset since the last save, making it possible to reconstruct the dataset in case of a crash. AOF provides better durability than RDB, as it logs every write operation to disk.
AOF can be configured to save the data periodically or based on certain conditions, such as a minimum number of write operations. However, AOF can lead to slower performance and larger disk space usage, as it logs every write operation to disk.
The append-only file is an alternative, fully-durable strategy for Redis, as the snapshotting is not very durable.
The AOF can be turned in the configuration file by,
appendonly yes
How AOF works?
- Redis forks a child process from the parent process.
- The child process creates a copy of the current state of the dataset in memory.
- The child process writes the copy of the dataset to a new AOF in a temporary file.
- The parent process accumulates all the new changes in an in-memory buffer (but at the same time it writes the new changes in the old append-only file, so if the rewriting fails, we are safe).
- When the child is done rewriting the file, the parent process gets a signal and appends the in-memory buffer at the end of the file generated by the child.
- Then, Redis automatically renames the old file into the new one and starts appending new data into the new file.
Advantages of AOF
- AOF Redis is much more durable, as we can have different fsync policies, no fsync at all, fsync every second, fsync at every query.
- It is an append-only log, so there are no seeks, nor corruption problems if there is power outage.
- The Redis check-of tool is automatically able to fix any half-written command if the log ends suddenly due to some disk-full or other reasons.
Disadvantages of AOF
Let us now take a comparative look at the Disadvantages of AOF:
- These files are generally bigger than equivalent RDB files for the same dataset.
- It can be slower than RDB depending on the exact fsync policy.
- AOF can improve the data consistency but does not guarantee so likely you can lose your data but less than RDB mode considering the RDB is faster.
Which one to choose ā Real-time database (RDB) or Append Only Files (AOF)?
The general thought process should be that we use both the persistence methods if we want a degree of data safety comparable to what normal databases like PostgreSQL, can provide us. If we care a lot about our data but still can live with a few minutes of data loss in case of disasters, we can simply use RDB alone.
Redis and its role in System Design
Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It is widely used for its fast performance, flexibility, and ease of use.
|
Contact Us