22 - System Design: Distributed In-Memory Cache (Redis Cluster)

Executive Summary: Disk storage operates in milliseconds (1โ€“10 ms), whereas main memory (RAM) operates in nanoseconds (100 ns) and local network transfers take sub-milliseconds (0.5 ms). A distributed in-memory cache bridges this 10,000ร— speed gap. Designing a production-grade distributed cache like Redis Cluster or Memcached requires understanding single-threaded I/O multiplexing (epoll), O(1) LRU memory eviction, 16,384-slot hash sharding, and durability persistence via point-in-time snapshots (RDB) and append-only logs (AOF).


๐Ÿข Real-world case studies: how Google & Netflix approach this, in REAL_WORLD_EXAMPLES.md.

โฑ๏ธ Prerequisites: What You Need to Know First

To get the most out of this module, make sure you understand:


๐Ÿง’ The 30-Second Version (ELI5)

Imagine you are a detective working on a case:


๐Ÿ• Everyday Analogy

Component Detective Office Analogy Technical Reality in Redis Cluster
RAM Storage Sticky notes on the desk corkboard Data held strictly in volatile RAM (sub-microsecond access)
Disk Storage Basement heavy metal filing cabinets Persistent relational database (PostgreSQL / MySQL)
Single-Threaded Loop One hyper-focused detective handling 1 note at a time without interruptions Single-threaded epoll event loop avoiding mutex lock contention
LRU Eviction Throwing away the sticky note untouched for the longest time Evicting tail node from doubly-linked list in O(1) time
TTL Expiration Sticky note marked: "Discard at 5:00 PM today" Unix timestamp field evaluated lazily or swept periodically
Hash Slots (16,384) Dividing case files into 16,384 standard color-coded folders CRC16(key) ยฑod{16384} partitioning
RDB Snapshots Taking a Polaroid photograph of the corkboard once an hour Background fork() saving point-in-time binary dump
AOF Log Writing every sticky note addition into a continuous diary Append-Only File recording every mutating command

๐Ÿงฉ The Problem It Solves

The Physical Limits of Disk I/O

How a Distributed Cache Solves This


๐Ÿ–ผ๏ธ Picture It: Architecture & Flow

High-Resolution In-Memory Cache Animation

Below is an animated trace of key routing across 16,384 hash slots, single-threaded in-memory execution, and AOF/RDB persistence:

Distributed In-Memory Cache


Internal Node Architecture & Sharding Flow

flowchart TD
    subgraph ClientLayer["Client & Routing Layer"]
        App["App Server / Microservice"]
        Router["Smart Client (Hashes CRC16(key) % 16384)"]
        App --> Router
    end

    subgraph Cluster["Redis Cluster (3 Masters, 3 Replicas)"]
        direction LR
        subgraph Master1["Master Shard 1 (Slots 0 - 5460)"]
            Loop1["โšก Single-Threaded Event Loop (epoll)"]
            RAM1[("In-Memory Store<br/>Dict + LRU Doubly Linked List")]
            AOF1["๐Ÿ“œ AOF Buffer (fsync=everysec)"]
            Loop1 --> RAM1
            Loop1 --> AOF1
        end

        subgraph Master2["Master Shard 2 (Slots 5461 - 10922)"]
            Loop2["โšก Single-Threaded Event Loop (epoll)"]
            RAM2[("In-Memory Store<br/>Dict + LRU Doubly Linked List")]
        end

        subgraph Master3["Master Shard 3 (Slots 10923 - 16383)"]
            Loop3["โšก Single-Threaded Event Loop (epoll)"]
            RAM3[("In-Memory Store<br/>Dict + LRU Doubly Linked List")]
        end

        Router -->|Slot: 1042| Master1
        Router -->|Slot: 7891| Master2
        Router -->|Slot: 14200| Master3

        Replica1["๐Ÿ›ก๏ธ Replica 1<br/>(Async Replication)"]
        Master1 -.->|Replication Stream| Replica1
    end

    style Master1 fill:#161b22,stroke:#3fb950,stroke-width:2px;
    style Master2 fill:#161b22,stroke:#58a6ff,stroke-width:2px;
    style Master3 fill:#161b22,stroke:#d29922,stroke-width:2px;
    style Replica1 fill:#161b22,stroke:#8b949e,stroke-width:1px;

๐Ÿ”ข Walk-Through with Real Numbers

Let's calculate the hardware specifications and network capacity for a 10-node caching cluster:

1. Latency Physics Comparison

L1 / L2 CPU Cache โ‰ˆ 0.5โ€“2 ns
Main Memory (RAM) Access โ‰ˆ 100 ns = 0.0001 ms
NVMe SSD Read โ‰ˆ 100 ฮผs = 0.1 ms
Spinning Mechanical HDD Read โ‰ˆ 10,000 ฮผs = 10 ms

RAM is 1,000x faster than NVMe SSDs and 100,000x faster than traditional hard drives!

2. Network NIC Bottleneck Calculation

Many engineers assume CPU is the bottleneck in Redis. It is almost always the network card (NIC)!

10 Gbps โ‰ˆ 1.25 Gigabytes/sec = 1,250,000 KB/sec
Max Throughput = (1,250,000 KB/s) / (1 KB/req) โ‰ˆ 1,250,000 requests/second

At 1.25 million requests/second, the network interface is 100% pegged, regardless of available CPU!

3. Memory Sizing for 200 Million Keys

Total Memory = 200,000,000 ร— 332 bytes โ‰ˆ 66.4 GB of RAM

A 3-node cluster with 32 GB RAM per node provides 96 GB total RAM, running comfortably below 70% memory utilization.


โš™๏ธ How It Works Under the Hood

1. Why is Redis Single-Threaded for Execution?

A common interview question: "Why doesn't Redis use 64 worker threads to utilize modern multi-core CPUs?"


2. The 16,384 Hash Slot Sharding Mechanism

Unlike arbitrary consistent hashing rings where clients walk clockwise:

slot = CRC16(key) ยฑod{16384}

3. Persistence: RDB vs. AOF

Mechanism How It Works Durability Guarantee Recovery Speed Performance Impact
RDB (Snapshot) Linux fork() creates child process; writes compact binary dump via Copy-on-Write (COW) Weak (loses data written since last snapshot, e.g. last 5โ€“15 mins) Fastest (loads directly into memory) Rare CPU/memory spikes during fork() of huge RAM instances
AOF (fsync=everysec) Logs every mutating command (SET, DEL) to disk; background fsync every second High (at most 1 second of data loss) Slower (must replay every command from log) Negligible continuous background sequential I/O

โš–๏ธ Trade-Offs & Decision Matrix

Dimension Redis Memcached
Data Structures Rich: Strings, Hashes, Lists, Sets, Sorted Sets, Bitmaps, HyperLogLog Simple Key-Value Strings only
Threading Model Single-threaded event loop (epoll) Multi-threaded with fine-grained locking
Persistence RDB Snapshots + Append-Only Files (AOF) None (pure volatile memory)
Replication Built-in Master-Replica with automatic failover (Sentinel / Cluster) No built-in replication (handled by client)
Best For Leaderboards, session stores, pub/sub, caching complex data Pure, simple, high-throughput static caching

๐Ÿšซ Common Mistakes & Production Pitfalls

  1. Running O(N) Blocking Commands (KEYS *) in Production:

    • Pitfall: An engineer executes KEYS user:* in production to debug a user issue.
    • Result: Because Redis is single-threaded, scanning 50,000,000 keys blocks the entire server for 5 seconds. All other 500,000 incoming requests queue up, time out, and crash upstream API services!
    • Fix: Disable the KEYS command in redis.conf (rename-command KEYS ""). Use non-blocking cursor iteration with SCAN instead.
  2. The Huge fork() Memory Spike (Copy-on-Write OOM):

    • Pitfall: Running Redis on a server with 30 GB of data on a 32 GB RAM instance. When RDB snapshotting triggers, the Linux kernel executes fork().
    • Result: If high write volume occurs during the snapshot, Linux Copy-on-Write allocates duplicate memory pages. The memory footprint climbs past 32 GB, triggering Linux OOM-Killer, which abruptly sends SIGKILL to redis-server! Always keep memory utilization โ‰ค 60โ€“70% of physical RAM on persisting nodes.
  3. Hot-Key CPU Saturation:

    • Pitfall: An ultra-viral celebrity account or live breaking news article has all traffic routed to a single key (e.g. news:breaking).
    • Result: That key hashes to a single slot on a single Redis master. That single CPU core hits 100%, throttling all operations on that node.
    • Fix: Replicate hot keys with random suffix salting (news:breaking:1, news:breaking:2...) or deploy a local L1 in-process cache (e.g. Guava/Caffeine) on application servers.

๐Ÿ’ป Try It Yourself: Python Simulation

The companion simulation script demo.py implements:

  1. An O(1) LRU Cache Node with Doubly-Linked List and Hash Map pointers.
  2. TTL expiration mechanisms (lazy evaluation on read + automatic deletion).
  3. Append-Only File (AOF) write-ahead logging and recovery replay on server reboot.
  4. Consistent Hash Ring sharding keys across a multi-node cache cluster.

Run it locally in your terminal:

python demo.py

๐ŸŒ Where You'll See It in Production


๐ŸŽฏ Top Interview Questions & Deep Answers

Q1: "How does Redis achieve high performance despite being single-threaded?"

Interviewer Goal: Testing candidate's understanding of operating system primitives and concurrency trade-offs.

Model Answer: "Redis achieves >100,000 operations per second on a single thread due to four architectural reasons:

  1. Pure RAM Execution: All primary operations manipulate data directly in main memory (100 ns access time), eliminating disk seek and block erase latency.
  2. I/O Multiplexing (epoll / kqueue): Instead of allocating a thread per client (which wastes memory and incurs heavy context-switching overhead), a single thread uses OS-level event polling to monitor non-blocking sockets, processing requests as bytes arrive.
  3. No Lock Contention: Multi-threaded memory access requires mutexes and synchronization primitives. Under high concurrency, lock acquisition overhead and thread context switching degrade CPU throughput. Redis's single-threaded model eliminates race conditions, deadlocks, and locking overhead completely.
  4. Optimized Low-Level Data Structures: Redis uses custom, cache-friendly C data structures like Simple Dynamic Strings (SDS), Skiplists, and ZipLists."

Q2: "What is the difference between Redis's RDB and AOF persistence, and which should you use?"

Interviewer Goal: Assessing understanding of durability, recovery speed, and operational overhead.

Model Answer: "Both solve data durability, but with different trade-offs:

  1. RDB (Redis Database Snapshot):
    • Takes a point-in-time binary snapshot of all data in memory at configurable intervals (e.g. every 15 minutes).
    • Uses the Linux fork() system call to spawn a child process that writes to disk in the background while the parent continues serving traffic via Copy-on-Write (COW).
    • Pros: Extremely fast recovery on restart; compact single file.
    • Cons: High data loss risk (all writes since the last snapshot are lost if the server crashes).
  2. AOF (Append-Only File):
    • Logs every mutating write command (SET, DEL) to disk.
    • Typically configured with fsync=everysec (flushes OS page cache to disk once per second).
    • Pros: Maximum durability (at most 1 second of data loss).
    • Cons: Larger file size; slower recovery on startup. Best Practice: Enable both simultaneously. Redis will use AOF on startup for maximum data completeness, while RDB provides fast disaster-recovery backups to S3."

Q3: "How does Redis Cluster shard data across nodes, and how do client drivers know where to send keys?"

Interviewer Goal: Evaluating distributed hash sharding and routing mechanisms.

Model Answer: "Redis Cluster does not use a central proxy. Instead, it uses 16,384 fixed Hash Slots:

  1. When a key is accessed, the slot is determined by:
slot = CRC16(key) ยฑod{16384}
  1. The 16,384 slots are divided among master nodes (e.g. Master 1 gets 0โ€“5460, Master 2 gets 5461โ€“10922, etc.).
  2. Smart Client Routing:
    • Modern Redis client libraries (like jedis or redis-py-cluster) fetch and cache the slot-to-node topology map upon startup.
    • The client computes the slot locally and routes the query directly to the correct master IP in O(1) time.
    • If a slot was migrated to another node during rebalancing, the old node returns a MOVED <new_slot> <new_ip> redirect. The client updates its internal slot map and replays the request automatically."

Q4: "How does Redis expire keys with TTLs, and why doesn't it crash the CPU when millions of keys expire simultaneously?"

Interviewer Goal: Testing memory reclamation algorithms and timer management.

Model Answer: "Redis combines two complementary algorithms to expire keys efficiently:

  1. Passive / Lazy Expiration:
    • When a client issues a GET key, Redis inspects the key's TTL timestamp.
    • If NOW() > expires_at, Redis does not return the value; it immediately deletes the key from memory and returns nil.
  2. Active / Periodic Sweeping:
    • Relying solely on lazy expiration would leak memory for keys that are never read again.
    • Therefore, Redis runs a background periodic timer (10 times per second):
      1. It randomly samples 20 keys with active TTLs.
      2. It evicts all expired keys in that sample.
      3. If more than 25% of the sampled keys (i.e. >5 keys) were expired, it immediately repeats the process.
    • To prevent blocking the main event loop, this active sweep has a strict time limit (typically 25 ms). If the limit is reached, it yields control back to serving client traffic, preventing CPU starvation."

Q5: "What is the Cache Breakdown (Hot-Spot Key Expiration) problem, and how do you prevent it in Redis?"

Interviewer Goal: Checking production stability patterns and concurrency control.

Model Answer: "Cache Breakdown occurs when an ultra-popular hot key (e.g., live election results or a viral sale item) that is being requested by 100,000 queries/second suddenly reaches its TTL expiration and evaporates from memory. In the next 50 milliseconds, 5,000 parallel application requests miss the cache and all hammer the database with the exact same expensive query, crashing the database.

Mitigation Strategies:

  1. Distributed Mutex (Singleflight): When a cache miss occurs, the worker attempts to acquire a Redis lock (SET lock:key token NX EX 5). Only the single worker that acquires the lock queries the database; the other 4,999 workers sleep and re-read the cache.
  2. Logical Expiration (Probabilistic Early Refresh): Store an internal logical_expire = NOW() + 300s inside the JSON payload, while setting the actual Redis TTL to 600s. When an application sees that NOW() > logical_expire, it serves the cached data immediately while asynchronously spawning a background worker to refresh the value."

โœ… Check Yourself: Flash Quiz

1. Why does executing KEYS * in a production Redis instance with 50M keys cause a severe site outage?
Because Redis is single-threaded for command execution. KEYS * traverses the entire dictionary in O(N) time, blocking the event loop for several seconds. During that time, all other incoming client requests are blocked from executing, leading to client connection timeouts and cascading API failures. Always use SCAN.
2. How many hash slots does Redis Cluster have, and why is this number fixed?
Redis Cluster has exactly 16,384 hash slots. The slot number is fixed to allow cluster nodes to exchange slot allocation maps via lightweight 2 KB heartbeat gossip bit-vectors (16,384 / 8 = 2,048 bytes), keeping inter-node network gossip overhead minimal.
3. What happens if you run Redis with maxmemory set, but without an eviction policy (noeviction)?
When memory limit is reached, Redis will refuse to accept any new mutating write commands (like SET, HSET, LPUSH), returning an error: OOM command not allowed when used memory > 'maxmemory'. Read commands (like GET) will continue to work.