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[Client Pool / Application 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

Client Pool (Application Servers)

The client pool of application servers (App Server 1, App Server 2, and App Server 3) runs the client-facing logic that queries and updates data. Each server dispatches GET and SET commands to the Redis Primary first, falling back to the Main Database on a cache miss before populating the resulting value back into the in-memory store.

Redis Primary (Primary Instance)

The primary instance serves as the single-threaded write coordinator and authoritative in-memory store. Its single-threaded event loop processes client write commands sequentially without locking overhead, updates key structures in RAM, enforces TTL expirations, and streams replication state updates to replicas.

Redis Replicas / Shards

Replica and shard nodes scale read throughput or partition memory across a cluster:

  • Replicas: Maintain read-only copies of the primary instance to absorb heavy read query traffic and stand ready for Sentinel promotion.
  • Shards: Divide the keyspace into 16,384 logical hash slots so keys distribute across separate cluster nodes during sharded phases.

Main Database

The main database provides transactional, durable disk storage of record (such as PostgreSQL). It handles authoritative writes and slower SELECT fallback queries whenever a requested key misses the Redis cache or has expired.

Disk Storage (AOF & RDB)

Disk storage persistence prevents complete data loss when the in-memory Redis server crashes or reboots:

  • RDB (Snapshotting): Flushes compact point-in-time binary snapshots of in-memory state to disk.
  • AOF (Append Only File): Logs every write command sequentially to an append-only file on disk.

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.