Build Redis
10 scenes · ~70 min · build the primitive

Build your own Redis

An in-memory data-structure server: one thread, rich types, optional persistence, async replication. Internalize the cost of single-threaded simplicity and a dozen caching/HA decisions get easier.

Scenes
10 interactive scenes
Time
about 70 minutes
Topic
Caching, Proxies & the Edge

What you are building, and why

You are designing an in-memory data-structure server: clients send commands, the server mutates rich types (strings, lists, hashes, sets, sorted sets, streams) held in RAM, and optionally persists to disk. This is the canonical "your data structures, on a server" system — once you have built it, you understand why a single thread is fast (and where it is fatal), why "Redis transactions" are not ACID, why default Redis can lose ~1 second of writes, why Sentinel is not CP, why Cluster has no proxy, and how every distributed-systems decision in Redis flows from one foundational choice: one thread.

Resist the urge to "describe Redis." Make decisions yourself, defend them, and let the AI push back. The point is to make every choice the Redis authors made and feel why they made it.

What you will be able to explain afterwards

  • single-threaded event loop
  • data-structure encodings
  • RDB + AOF persistence
  • sampled LRU / LFU eviction
  • async PSYNC replication
  • Sentinel quorum + majority
  • Cluster hash slots & client routing
  1. 01
  2. 02
  3. 03
  4. 04
  5. 05
  6. 05a
  7. 06
  8. 07
  9. 08
  10. 09

The loop

One thread, one command — and the encoding under it.

  1. 01
    Foundations — what Redis is, words you'll hear
    An in-memory data-structure server, the eight core nouns, and the six canonical types. Orientation before you touch the internals.
    ~7 min
  2. 02
    One thread, one command at a time — the Redis event loop and slow-command stalls
    Why a single event loop is fast — and why one slow command (KEYS *, big LRANGE, slow Lua) stalls every client.
    ~7 min
  3. 03
    Encodings flip under you
    Listpack ↔ hashtable, intset ↔ hashtable, embstr ↔ raw — crossing a threshold silently rewrites memory and op-cost.
    ~7 min

Memory

Persistence, eviction, TTL — what makes RAM disappear.

  1. 04
    Persistence — fork, CoW, and the 1-second window
    RDB snapshots via fork+CoW, AOF fsync policies, and why default Redis can lose ~1s of writes on crash.
    ~7 min
  2. 05
    Eviction is sampled, not exact
    maxmemory + sampled LRU/LFU — Redis only inspects N keys per pass; tunable via `maxmemory-samples`.
    ~7 min
  3. 05a
    TTL and cleanup — lazy, active, and the freer thread
    Passive + 10Hz active sampler expire keys; DEL of a big value freezes the loop unless `lazyfree-*`/UNLINK offloads to the background freer thread. Cache stampede + jitter / SET NX rebuilder lock.
    ~7 min

HA

Replication and Sentinel — async by design.

  1. 06
    Replication is async — acked writes can vanish
    PSYNC, replication backlog, and the AP-not-CP gotcha: WAIT doesn't fix it; min-replicas-to-write does (at the cost of unavailability).
    ~7 min
  2. 07
    Sentinel — quorum detects, majority elects
    SDOWN/ODOWN ladder, epoch-based election, and the operator footgun: quorum ≠ majority.
    ~7 min

Shard & ship

Cluster slots, MOVED/ASK, and the design canvas.

  1. 08
    Cluster — 16384 slots and the client routes
    CRC16 mod 16384, MOVED vs ASK (permanent vs transient), hash tags, configEpoch — and why sharding alone is not HA.
    ~7 min
  2. 09
    Design your Redis deployment
    Capstone: pick persistence, HA, and sharding for a stated SLO; the verifier traces each choice back to the scene that taught it.
    ~7 min

Where you'll use this

Product designs whose trade-offs turn on what this curriculum teaches.

More in Caching, Proxies & the Edge

Everything between the client and the origin: in-memory caches, CDNs, load balancers and service proxies — and the three ways a cache betrays you.

Prefer to design it yourself?

The same subject as a staged workspace: draw the architecture, and a simulator traces requests through the boxes you drew.

Open the Build Redis workspace