Question #45 of 60•Caching•Hard• ~3 min read

How do you design an In-Memory Key-Value Store (Redis Architecture)?

Executive Takeaway

Deconstruct single-threaded event loops (epoll), hash table dict resizing, and RDB/AOF persistence modes.

Architectural Breakdown & AnalysisFAANG Aligned

Redis achieves sub-millisecond latencies by storing all data in memory and using an asynchronous single-threaded event loop.

1. Single-Threaded Event Loop (I/O Multiplexing)

Uses Linux epoll system calls to process thousands of client socket requests sequentially on a single CPU core without lock contention or context switching.

2. Incremental Hash Resizing (Rehash)

When Redis hash tables expand, dict entries migrate incrementally across background requests to avoid blocking the main event thread.

3. Persistence Modes

  • RDB (Snapshotting): Point-in-time binary snapshot saved to disk at configured intervals.
  • AOF (Append-Only File): Logs every write command sequentially. Recovered by replaying log records.
NOTE — Redis 6+ Multi-threading

Redis 6+ introduced multi-threading exclusively for offloading network socket I/O parsing, while core command execution remains single-threaded.

Key Architectural Comparison

Redis ComponentArchitectural MechanismTechnical Benefit
I/O MultiplexingNon-blocking Linux epoll event loop.Single thread handles 100k+ concurrent client sockets without thread contention.
Dynamic Resizing (Rehash)Progressive incremental rehashing across requests.Zero latency freezing when expanding hash table buckets.
Persistence (AOF vs RDB)Sequential disk append vs fork-based memory snapshots.Fast recovery time with sub-second durability guarantees.
Previous QuestionHow do Database Deadlocks occur and how do engines detect and resolve them?Next Question What are the trade-offs of Blue-Green, Canary, and Rolling Deployments?

Continue Learning on Being SDE

Explore deep-dive system architecture modules, low-level object-oriented designs, and interview cheat sheets.