Redis Persistence: Understanding and Implementing Append Only Files (AOF)

Arpit Bhayani

Arpit Bhayani

May 10, 2026 • 9 min read

Play

Note: This article is an AI-generated write-up based on the captions and transcript of the video above. Watch the embedded video for the full visual walk-through and nuances.

Introduction to Redis Persistence

It’s a common misconception that Redis is a purely in-memory data store, offering no persistence. In reality, Redis provides robust persistence mechanisms, allowing you to optionally flush your in-memory data to disk. This ensures data durability even in the event of a server restart or crash. Redis offers two primary flavors of persistence:

  1. RDB (Redis Database) Files: Point-in-time snapshots of the dataset.
  2. AOF (Append Only File) Files: A log of every write operation.

This article will delve into both, with a particular focus on understanding and implementing AOF persistence.

RDB Persistence: Point-in-Time Snapshots

What is RDB?

RDB persistence involves creating a point-in-time snapshot of the entire Redis dataset. When an RDB file is generated, Redis takes all the data currently in memory and dumps it into a single, highly compact binary file on disk. This file is extremely portable; you can move it to S3, Google Drive, or any other location for backup and disaster recovery purposes.

How RDB Snapshots are Created

To create an RDB file, you can configure Redis to flush at a specific frequency (e.g., every five minutes). When the snapshot is triggered, Redis employs a clever mechanism to avoid blocking the main thread, which is crucial for a single-threaded server:

  1. Redis forks a new process (a child process).
  2. This child process has access to the parent’s memory (via copy-on-write) and performs all the heavy lifting of dumping the in-memory data to disk in the RDB format.
  3. Meanwhile, the main Redis process continues to accept and process incoming client requests without any performance impact.

This BGSAVE (background save) mechanism ensures that Redis remains responsive while persistence operations are underway.

Trade-offs of RDB

Advantages:

  • Space-efficient: RDB files are highly compressed and compact, making them ideal for backups and archival.
  • Simple: A single file output is easy to manage and port.
  • Performance: Minimal impact on Redis performance during snapshot creation due to forking.

Disadvantages:

  • Potential for Data Loss: Since RDB is a point-in-time snapshot, any data updates that occur between the last successful snapshot and a server crash will be lost. If snapshots are taken every five minutes, you could lose up to five minutes of data.
  • Costly for Frequent Dumps: Dumping the entire dataset repeatedly can become resource-intensive as the data size grows, making very frequent snapshots impractical.

AOF Persistence: The Commit Log Approach

What is AOF?

AOF (Append Only File) persistence takes a different approach, akin to a database commit log or MySQL’s binlog. Instead of saving the entire dataset at intervals, AOF logs every single write operation that occurs on the Redis server. Read operations (like GET) are not logged, only modifications to the dataset.

The AOF file stores these operations as raw, RESP (Redis Serialization Protocol) encoded commands. This means the file is essentially a sequence of commands that, when replayed, can reconstruct the entire in-memory dataset. For example, if you execute INCR mykey, and mykey’s value changes from 4 to 5, the AOF might log SET mykey 5 instead of INCR mykey to simplify replay and optimize the log.

Durability and Data Loss with AOF

AOF offers significantly higher durability compared to RDB. Because every write operation is logged, the potential for data loss is drastically reduced. If AOF is configured to flush to disk once every second, the maximum data loss in case of a crash would be just one second. Upon booting up, Redis can load the AOF file and replay all the commands to reconstruct the in-memory dataset to its most recent state.

Managing AOF File Size: BG REWRITE AOF

The Problem of AOF File Growth:

Continuously logging every write operation can lead to AOF files growing very large. For instance, if a key K is set to V1, then V2, then V3, and finally V4, the AOF file would contain four SET entries for K. However, the actual dataset only holds the final value V4. This redundancy makes the AOF file much larger than necessary.

The Solution: BG REWRITE AOF:

Redis addresses this by periodically rewriting the AOF file in the most efficient way possible. This process is triggered by the BG REWRITE AOF command (Background Rewrite AOF). Here’s how it works:

  1. Background Operation: Similar to RDB, Redis forks a new process to perform the AOF rewrite in the background, ensuring the main server remains responsive.
  2. Optimized Content: The background process doesn’t simply copy the old AOF file. Instead, it iterates through the current in-memory dataset and generates a new, optimized AOF file. This new file contains only the minimal set of commands required to reconstruct the current state (e.g., for key K with final value V4, it would only write SET K V4).
  3. Atomic Swap: The new AOF content is initially written to a temporary file. Once the rewrite is complete, Redis atomically renames this temporary file to replace the old AOF file. This ensures that existing write operations are not interrupted and the switch is seamless.

This periodic rewriting keeps the AOF file size in check, preventing it from growing indefinitely while maintaining high durability.

AOF File Validation: redis-check-aof

Redis provides a command-line utility called redis-check-aof to validate the integrity of an AOF file. If an AOF file becomes corrupt due to an incomplete write or other issues, this tool can check its validity and even attempt to fix it, ensuring that Redis can reliably load and replay the file.

RDB vs. AOF: A Comparative Summary

FeatureRDB (Redis Database)AOF (Append Only File)
MechanismPoint-in-time snapshot of the entire datasetLogs every write operation as RESP commands
DurabilityLower (data loss up to flush frequency)Higher (data loss typically 1 second or less)
File SizeVery compact, highly compressed binary fileGenerally larger, contains command history (optimized by rewrite)
ReadabilityBinary, not human-readableHuman-readable (RESP commands)
Use CaseExcellent for backups, disaster recovery, portabilityPrimary for minimal data loss, continuous durability
PerformanceBGSAVE forks, minimal impact on main threadContinuous writes, BG REWRITE AOF forks

It’s common for production Redis deployments to use both RDB and AOF simultaneously to achieve an optimal balance of durability, backup capabilities, and recovery speed.

Implementing AOF Persistence: A Practical Walkthrough

Let’s walk through a simplified implementation of AOF persistence, focusing on the BG REWRITE AOF functionality.

Core Logic for BG REWRITE AOF

The implementation starts by defining a command, BG REWRITE AOF, which, when executed, triggers the AOF rewrite process. In a simplified example, this command might directly invoke a function like dumpAllAOF.

// In eval.go (simplified)
func eval(command []string) string {
    cmd := strings.ToUpper(command[0])
    switch cmd {
    case "BGREWRITEAOF":
        dumpAllAOF() // In a real Redis, this would fork a new process
        return "+OK\r\n"
    // ... other commands
    }
}

Note: In a real Redis server, dumpAllAOF would be executed in a forked child process to prevent blocking the main thread, similar to RDB’s BGSAVE. The example here keeps it synchronous for simplicity to focus on the core logic.

dumpAllAOF Function Details

The dumpAllAOF function is responsible for iterating through the current in-memory dataset and writing its state to a new AOF file. This function would typically reside in a file like aof.go.

  1. Open Temporary File: It opens a new file, often named appendonly.aof or a configured name (e.g., dice-master.aof), in write-only and append mode. If the file doesn’t exist, it’s created.
  2. Iterate Dataset: It then iterates through all the keys and their corresponding values currently stored in Redis’s internal hash table.
  3. Construct and Encode Commands: For each (key, value) pair, it constructs a SET key value command. This command is then RESP-encoded into an array of strings.

RESP Encoding for AOF

The AOF file stores commands in the RESP format, which is how Redis clients communicate with the server. For a command like SET K V, the RESP encoding would look like this:

*3\r\n$3\r\nSET\r\n$1\r
K\r
$1\r
V\r

// Explanation:
// *3\r\n      -> Array of 3 elements
// $3\r\nSET\r\n -> Bulk string of length 3: "SET"
// $1\r\nK\r\n   -> Bulk string of length 1: "K"
// $1\r\nV\r\n   -> Bulk string of length 1: "V"

The dumpAllAOF function would encode each SET command in this format and write it to the AOF file. This makes the AOF file directly replayable by a Redis server.

Demonstration and Validation

Let’s observe this in action:

  1. Start the Server: Run the Redis server implementation.
  2. Execute SET Commands: Interact with the server using a Redis CLI:
    SET K1 V1
    SET K2 V2
    SET K3 V4
    SET K3 V3  # Overwrites K3's value
    At this point, the in-memory dataset contains K1:V1, K2:V2, K3:V3. Although four SET operations were performed, K3 was overwritten.
  3. Trigger AOF Rewrite: Execute the BG REWRITE AOF command:
    BGREWRITEAOF
    The server will log that the AOF file is being rewritten and that the rewrite is complete.
  4. Inspect the AOF File: Use cat to view the contents of the generated AOF file (e.g., cat ./dice-master.aof). You will observe:
    *3

3SET3 SET 2 K1 2V132 V1 *3 3 SET 2K22 K2 2 V2 *3 3SET3 SET 2 K3 $2 V3 Notice that despite four `SET` commands being issued, the rewritten AOF file only contains three entries, reflecting the final state of the keys in the dataset. This demonstrates the optimization achieved by `BG REWRITE AOF`. 5. **Validate with `redis-check-aof`**: Use the official Redis utility to confirm the integrity of the generated AOF file: bash redis-check-aof ./dice-master.aof ``` The output AOF is valid confirms that the generated file is a proper, replayable AOF file that a Redis server can use to reconstruct its state.

Conclusion

Redis persistence is a fundamental aspect of its reliability, offering two distinct yet complementary modes: RDB for compact snapshots and AOF for high-durability, minimal data loss. While RDB excels in space efficiency and portability, AOF provides a continuous log of changes, ensuring that virtually no data is lost upon recovery. The BG REWRITE AOF mechanism is crucial for managing the size of AOF files, keeping them optimized and efficient.

Understanding these internals and their trade-offs is vital for designing robust, fault-tolerant systems with Redis. The ability to implement such core features, even in a simplified manner, highlights the elegance and compliance of Redis’s design.

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses