The Coding Gopher

The Coding Gopher

A Deep Dive into Redis Data Persistence: RDB and AOF

A deep dive into AOF, RDB, and keeping your data safe when the lights go out.

The Coding Gopher's avatar
The Coding Gopher
Apr 23, 2026
∙ Paid

A deep dive into RDB snapshots, AOF ledgers, and the mechanics of in-memory durability.

Crash Course Redis – Redis Persistence Models – D4Debugging

The defining characteristic of Redis is that it lives entirely in RAM. This is the source of its blistering, sub-millisecond speed. But it also introduces a terrifying physical reality: if the server loses power, or the process crashes, the RAM is instantly wiped clean.

Why is Redis so Fast and Efficient?

So, how can we trust Redis with critical data like user sessions, persistent queues, or primary application state?

The answer lies in its persistence mechanisms. Redis solves the “volatile RAM” problem by elegantly bridging the gap between memory and the physical hard drive using two completely different strategies: RDB (Redis Database) and AOF (Append-Only File).

Redis Explained - MyBlueLinux.com

1. RDB: The Point-in-Time Snapshot

RDB is the default persistence mode in Redis. Think of it like taking a photograph of your entire dataset at a specific moment in time.

If you configure Redis to save every 5 minutes, it will take all the keys, lists, sets, and hashes currently sitting in RAM, compress them into a tiny binary file (usually named dump.rdb), and write that file to the hard disk.

The Magic of fork() and Copy-on-Write

You might be wondering: If Redis is single-threaded, doesn’t writing a massive 10GB dataset to a slow hard drive block the entire database and cause requests to time out?

It would, if Redis did the writing itself. Instead, Redis uses a brilliant UNIX system call called fork().

When the fork() system call is called, the operating system kernel makes a copy of the parent process’s memory space, open file descriptors, and execution state. Both processes then run independently and concurrently, continuing execution from the exact same point immediately after the fork() call.

Crash Course Redis – Redis Persistence Models – D4Debugging
  1. When it’s time to take a snapshot, the main Redis process forks itself, creating an exact child clone of the database in memory.

  2. The main process continues serving client requests with zero interruption.

  3. The child process takes the snapshot, writes the dump.rdb file to the disk, and then silently kills itself.

Redis: How Does Redis Achieve Rapid Data Recovery After a Crash? (RDB) | by  Dylan Smith | Javarevisited | Medium

Because modern operating systems use Copy-on-Write (CoW), the child process doesn’t actually duplicate all 10GB of memory. It simply shares the memory with the parent, only duplicating the specific memory pages that get modified while the snapshot is being written.

Copy-on-write (CoW) is an optimization strategy that delays copying data until it is actually modified. Instead of creating a full duplicate immediately, the system creates a reference to the original, allowing multiple actors to share the same data. A true copy is only made when a write operation occurs.

A Crash Course in Redis - ByteByteGo Newsletter

The Catch: The Data Loss Window

RDB is incredibly efficient, but it has one fatal flaw. If Redis takes a snapshot at 12:00 PM, and the server crashes at 12:04 PM, you lose all the data written during those four minutes. RDB trades absolute durability for performance.


2. AOF: The Indestructible Ledger

If losing even a single second of data is unacceptable for your application, you need AOF (Append-Only File).

Instead of taking periodic snapshots, AOF acts as a continuous ledger. Every time a write command changes the database (like SET user:1 "Alice"), Redis immediately logs that exact command as text to the bottom of the AOF file on the disk.

What is Redis aof-use-rdb-preamble and How Does It Work? | NootCode  Knowledge Hub

If the server crashes, Redis simply reads the AOF file from top to bottom, replaying every single command to reconstruct the database state perfectly.

Redis Explained Internally — The Design Behind the Speed | by Anmol Sehgal  | CodeX | Medium

𝐋𝐞𝐚𝐫𝐧 𝐭𝐨 𝐛𝐮𝐢𝐥𝐝 𝐆𝐢𝐭, 𝐃𝐨𝐜𝐤𝐞𝐫, 𝐑𝐞𝐝𝐢𝐬, 𝐇𝐓𝐓𝐏 𝐬𝐞𝐫𝐯𝐞𝐫𝐬, 𝐚𝐧𝐝 𝐜𝐨𝐦𝐩𝐢𝐥𝐞𝐫𝐬, 𝐟𝐫𝐨𝐦 𝐬𝐜𝐫𝐚𝐭𝐜𝐡. Get 40% OFF CodeCrafters: https://app.codecrafters.io/join?via=the-coding-gopher

This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.

The fsync Dilemma

User's avatar

Continue reading this post for free, courtesy of The Coding Gopher.

Or purchase a paid subscription.
© 2026 The Coding Gopher · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture