Databases advanced LIVE DEMO
πŸ—οΈ

Redis Architecture

A comprehensive deep-dive into Redis internals: caching strategies, data structures, single-threaded execution, pipelining, persistence (RDB & AOF), replication, failover

redisarchitecturecachingpersistencereplicationclustershardingpipelining
Interactive Demo

Continue with the guided workspace

Use the paired workspace to connect this guide's mental model with live component state, visible signals, and the outcome each step or scenario is meant to prove.

Mental Model

Redis (Remote Dictionary Server) is a high-performance, in-memory, key-value database designed to serve millions of reads and writes per second with sub-millisecond latency.

Unlike standard disk-backed databases, Redis keeps all keys and data structures in RAM. To achieve high throughput without locks or race conditions, it processes commands sequentially in a single-threaded event loop. For reliability and scalability, Redis supports replication, failover via Sentinel, write partitioning via Cluster Sharding, and persistence configurations (RDB and AOF) to reload memory from disk after crashes.

graph TD
    App[App Servers] -->|1. Read/Write| Redis[Redis Primary Node]
    Redis -->|2. Async Replication| Replica[Redis Replica Nodes]
    Redis -.->|3. Write-Behind| Disk[Disk Storage RDB/AOF]
    App -->|4. Cache Miss Fallback| DB[(Main Database)]

Components and Why They Exist

App Servers

Runs the client-side application logic querying for data. Directs read requests to Redis. If a key is missing (cache miss), it queries the Main Database and writes the resulting value back to Redis. In the demo, you can see App Servers dispatching GET requests to Redis, querying the DB on a miss in Step 4, and writing back.

Redis Primary (Primary Instance)

Serves as the single-threaded write coordinator and authoritative copy of data. Processes writes, maintains in-memory key structures, enforces TTLs, and streams replication packets to replicas. In the demo, observe it handling client write commands and coordinating state updates.

Redis Replicas / Shards

Scales read capacity (Replicas) or scales write capacity and partitions memory (Shards).

  • Replicas: Maintain read-only copies of the primary instance to handle heavy read traffic.
  • Shards: Divide the keyspace into 16,384 logical hash slots, with each cluster node handling a specific range.

In the demo, watch read queries routed to replicas and keys distributed across separate shards in the relevant guided phases.

Main Database

Provides transactional, persistent database storage of record (e.g. PostgreSQL). Handles writes and reads, serving as the source of truth when cache entries are absent. In the demo, you can see the slow Select queries flowing to the database in Step 1.

Disk Storage (AOF & RDB)

Prevents complete data loss when the in-memory server crashes or reboots.

  • RDB (Snapshotting): Writes point-in-time binary files to disk.
  • AOF (Append Only File): Logs every write command sequentially to a file.

In the demo, look for the persistence phases when memory changes are synced to disk storage.


Edge Cases and Misconceptions

Cache Miss Stampede (Thundering Herd)

A common misconception is that adding Redis solves all database bottleneck problems. If a highly-frequented key (like the homepage payload) expires under high traffic, thousands of concurrent requests will experience a cache miss at the exact same millisecond. They will all fall back to query the main database simultaneously, potentially causing a DB crash. Systems solve this using lock locks (like mutexes) or pre-warming caches before TTL expiration.

Asynchronous Replication Loss

Because Redis uses asynchronous replication to keep latency low, writing to the primary and receiving β€œOK” does not guarantee replication to replica nodes. If the primary crashes before the replication packets reach the replicas, Sentinel will elect a replica as the new primary, resulting in the loss of that write.

Blocking the Single Thread

Since Redis is single-threaded, any command with a complexity of O(N)O(N) (where NN is large, like KEYS * or KEYS pattern) will block the entire event loop. No other read or write request can be processed until the scan completes. Production systems forbid commands like KEYS and use SCAN instead to query keys incrementally.

Common Questions

What is the difference between RDB and AOF persistence?

- **RDB** writes compact, point-in-time snapshots of the database to disk at set intervals. It is fast to load but can lose minutes of data on crashes. - **AOF** logs every write operation received by the server to a file. It is slower to load but much safer, typically configured to sync to disk once per second (losing at most 1 second of writes).

How does Redis Cluster shard data using hash slots?

Redis Cluster does not use consistent hashing. Instead, it divides the keyspace into 16,384 hash slots. Every key is mapped to one of these slots using CRC16 hashing (`hash_slot = CRC16(key) mod 16384`). The master nodes in the cluster divide these slots among themselves (e.g., Node A gets slots 0-5460, Node B gets 5461-10922, Node C gets 10923-16383).

What happens during a Sentinel failover?

Redis Sentinel monitors primary and replica instances. If a primary fails, Sentinels run a consensus vote. Once a quorum agrees the primary is offline, Sentinel promotes the healthiest replica to be the new primary, updates the other replicas to replicate from the new primary, and notifies the client applications to redirect write traffic.

Why is Redis single-threaded if CPUs are multi-core?

CPU speed is rarely the bottleneck for Redis; memory access speed and network bandwidth are the primary limits. A single-threaded design avoids the complexity of lock management, thread context switching, and CPU cache synchronization, which would actually degrade performance for simple key-value lookups.