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

AOF

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. 

  • What is Redis
  • Redis Data Types
  • Benefits of using Redis
  • Working Architecture of Redis
    • 1. Single Redis Instance
    • 2. Redis HL (High Availability)
    • 3. Redis Sentinel
    • 4. Redis Cluster / Redis Cluster Master-Slave Model
      • What is gossiping in the Redis cluster?
  • Types of Redis Persistence Models
    • 1. RDB (Real-time Data Base) Persistence Model:
      • Snapshotting in RDB
      • Advantages of RDB(Real-time database)
      • Disadvantages of RDB(Real-time database)
    • 2. AOF (Append-Only File) Persistence Model
      • How AOF works?
      • Advantages of AOF
      • Disadvantages of AOF
    • 3. No Persistence Model
    • 4. Hybrid (RDB+AOF) Persistence Model
  • Availability, Consistency, and Partitioning in Redis
  • Can we use Redis as an alternative to the original DB?
  • Conclusion

Similar Reads

Redis Data Storage Types

Redis allows developers to store, retrieve, and manipulate data in various data structures such as strings, bitmaps, bitfields, hashes, lists, sets, sorted sets, geospatial, hyperlogs, and streams....

Benefits of using Redis

All Redis data resides in the serverā€™s main memory, in contrast to databases such as PostgreSQL, SQL Server, and others that store most data on disk. Redis can therefore support higher orders of magnitude of operations and faster response times. Thus, it results in super-fast performance with average read and writes operations taking less than milliseconds, and thus accordingly supports millions of operations per second....

Working Architecture of Redis

There are several Redis architectures, depending on the use case and scale:...

1. Single Redis Instance

This is the most straightforward Redis deployment. It involves users setting up and running small instances that can help them grow and speed up their services. However, it has its own drawback, as all calls made to Redis would fail if this running instance crashes or is unavailable. Thus there is a degradation in the overall performance and speed of the system....

2. Redis HA (High Availability)

Another popular setup is the main deployment with a secondary deployment that is always kept in sync with the replication. The secondary instances can be one or more instances in our deployment, which helps in scale reads from Redis, and provide failover in the case when the main is lost....

3. Redis Sentinel

Sentinel corresponds to a distributed system. It is designed in a way where there is a cluster of sentinel processes working together for coordination of state to provide constant availability of the Redis system. Here are the responsibilities of the sentinel: Monitoring: Ensuring main and secondary instances are working as expected. Notification: Notify all the system admins about the events occurring in Redis instances. Management during failure: Sentinel nodes can start a process during failure if the primary instance is not available for long enough, and enough nodes agree that it is true....

4. Redis Cluster / Redis Cluster Master-Slave Model: The Ultimate Architecture of Redis

The Redis cluster is the ultimate architecture of Redis. It allows for horizontal scaling of Redis....

Types of Redis Persistence Models

Redis provides two main persistence options to save data to disk: RDB and AOF. Both options have their own advantages and disadvantages, and which one to use depends on the specific needs of the application. Given below are the several persistence options listed:...

1. RDB (Real-time Data Base) Persistence Model:

RDB is a point-in-time snapshot of the Redis dataset stored as a binary file. The RDB file contains a representation of the dataset at a particular point in time and can be used to restore the dataset in case of a server crash or restart. RDB is very efficient in terms of disk space usage and performance, as it uses a binary format to store the data....

2. 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....

3. No Persistence Model

Redis also provides an option to disable persistence altogether, in which case the data is stored only in memory. This option is useful when Redis is used as a cache, and the data can be regenerated if lost....

4. Hybrid (RDB+AOF) Persistence Model

Redis provides an option to use both RDB and AOF persistence together, which is known as hybrid persistence. This option provides the benefits of both RDB and AOF, as the AOF log is used to replay write operations after a restart, and the RDB snapshot is used to restore the dataset at a specific point in time....

Availability, Consistency, and Partitioning in Redis

Hereā€™s a brief overview of how Redis handles availability, consistency, and partitioning:...

Can we use Redis as an alternative to the original DB?

Based on the above discussion, Redis seems to be a better option for the original DB as it provides faster retrievals. Even then Redis is not used as a primary option for the database in the system....

Conclusion

Overall, Redis is a powerful tool for system design, but it may not be suitable for all use cases. It is important to carefully consider its limitations when deciding whether to use Redis in a particular application....

Contact Us