RAM Today, Gone Tomorrow: How Redis Keeps Its Memory

Nurul HasanNurul Hasan
4 min read

Redis stores data in RAM which makes it fast. But what happens when it crashes or restarts? How does it remember your data?

In this article, we’ll explore how Redis handles persistence using two core mechanisms: AOF (Append-Only File) and RDB (Redis Database File). I’ll break down the concepts with simple analogies, real-world examples, and code snippets to help you truly understand what’s going on under the hood.


Why Does Redis Need Persistence?

Redis stores everything in memory. That makes it blazing fast, but also volatile. If Redis crashes or restarts, the data could disappear. To solve this, Redis provides two main persistence options:

  1. RDB – Think of this as saving your game progress at checkpoints.

  2. AOF – Think of this as a recording of every move you make in the game.


RDB (Redis Database File) — Snapshot-Based Persistence

What is RDB?

RDB creates a snapshot of your data at a point in time. It saves this snapshot to a binary file (dump.rdb) on disk.

Analogy:

Imagine you’re writing a long essay in Microsoft Word. Every 10 minutes, Word automatically saves your document. If your computer crashes, you'll only lose the last few minutes of work.

That’s RDB. It's fast and efficient, but might lose recent changes.

How RDB Works:

  1. At a regular interval (e.g., every 60 seconds), Redis writes the entire dataset to a .rdb file.

  2. If Redis restarts, it loads this file into memory.

Pros:

  • Fast to read/load.

  • Compact file size.

  • Good for backups.

Cons:

  • Risk of data loss between snapshots.

Sample Code (Simplified C++):

std::unordered_map<std::string, std::string> kv_store;

void save_to_rdb() {
    std::ofstream file("dump.rdb", std::ios::binary);
    for (auto& [key, val] : kv_store) {
        file << key.length() << " " << key << " ";
        file << val.length() << " " << val << "\n";
    }
    file.close();
}

AOF (Append-Only File) — Operation-Based Persistence

What is AOF?

AOF logs every write command to a file in the same format that the client sends. Redis can replay these commands on restart to reconstruct the data.

Analogy:

Think of AOF like the Undo/Redo history in a code editor. Every change is recorded, so you can go back step-by-step.

Even if Redis crashes 1 second after a write, that command was already saved in the AOF log.

How AOF Works:

  1. Every time a client sends a write command (e.g., SET key value), Redis logs it to appendonly.aof.

  2. On restart, Redis replays the file line-by-line to restore data.

Pros:

  • Much more durable than RDB.

  • You can configure it to write after every command (fsync).

Cons:

  • File grows continuously.

  • Slower than RDB on large workloads.

Sample Code (Simplified C++):

void append_to_aof(const std::string& cmd) {
    std::ofstream aof("appendonly.aof", std::ios::app);
    aof << cmd << "\n"; // Store commands like SET key value
    aof.close();
}

void replay_aof(std::unordered_map<std::string, std::string>& store) {
    std::ifstream aof("appendonly.aof");
    std::string line;
    while (getline(aof, line)) {
        // Very basic parser for "SET key value"
        std::istringstream iss(line);
        std::string cmd, key, val;
        iss >> cmd >> key >> val;
        if (cmd == "SET") store[key] = val;
    }
    aof.close();
}

RDB vs AOF

FeatureRDBAOF
FormatBinary snapshotLog of commands (text)
Write FrequencyPeriodicallyAfter every write (configurable)
DurabilityMedium (may lose data)High (can be very durable)
Load TimeFasterSlower
File SizeSmallerGrows larger
Use CaseBackups, speedDurability, no data loss

Pro Tip:

Redis allows you to enable both AOF and RDB. On restart, it prefers AOF for better durability, but RDB is still there as a backup.


Implementing Persistence in the Projects

When building your own Redis server (as I’m doing), here’s what I’d suggest:

  1. Start with AOF — it's easier to implement (just log commands).

  2. Add AOF replay on server startup.

  3. Later, implement RDB for quick saves and performance boosts.

  4. Add AOF rewrite — rewrite the file occasionally to remove redundant commands.


Final Thoughts

Understanding how Redis handles persistence helps you appreciate the design of high-performance systems — and lets you build one yourself!

Whether you prefer snapshots (RDB) or command logs (AOF), both have their place. It’s like choosing between automatic saves and version control: one is fast, the other is reliable, and together they give you the best of both worlds.


Let’s Chat!

  • Are you building your own Redis?

  • Have questions about parsing commands or file I/O?

  • Want help implementing these in C++?

Drop your thoughts in the comments or connect with me on Twitter/LinkedIn — I’d love to discuss Redis internals with you!


Happy hacking, and may your key-value pairs persist forever! 🔥


Thanks for reading it ❤️

0
Subscribe to my newsletter

Read articles from Nurul Hasan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nurul Hasan
Nurul Hasan