Blog 14 — Redis: Architecture Internals
C
Qubits of DPK
March 21, 2026
Core Case Studies
Core Concept: Single-threaded event loop, Redis Cluster + consistent hashing, Eviction policies (LRU, LFU), Redis Sentinel for HA
Why SDE-2 Critical: Every backend interview touches caching — surface-level Redis knowledge is not enough at SDE-2 level
Status: Draft notes ready
Quick Revision
- Problem: Keep ultra-hot data in memory without turning the cache into a single point of failure.
- Core pattern: Single-threaded command execution, persistence options, and cluster/sentinel modes.
- Interview one-liner: Redis is fast because it keeps the model simple and pushes complexity to topology choices.
️ Architecture Overview
javascript
QUBITS OF DPK
Core Concepts
Why Single-Threaded is Actually Fast
javascript
QUBITS OF DPK
Redis Persistence Options
javascript
QUBITS OF DPK
Eviction Policies
javascript
QUBITS OF DPK
Redis Cluster (Horizontal Sharding)
javascript
QUBITS OF DPK
Redis Sentinel (High Availability)
javascript
QUBITS OF DPK
Performance
5 Interview Questions This Blog Unlocks
Q1. How does Redis handle 1 million operations per second with a single thread?
Answer: All data in RAM (no disk I/O). Single thread eliminates mutex overhead and context switching. Event loop multiplexes thousands of connections via epoll. Each command executes in microseconds. Bottleneck is network, not CPU — single thread is sufficient.
Q2. What is the difference between LRU and LFU eviction?
Answer: LRU (Least Recently Used) evicts the key not accessed for the longest time. Good for temporal access patterns. LFU (Least Frequently Used) evicts the key accessed fewest times. Better for popularity-based caching — a viral tweet used 1M times/day won't be evicted even if not accessed in the last minute.
Q3. What is consistent hashing and how does Redis Cluster use it?
Answer: Redis Cluster uses 16384 fixed hash slots. Each key maps to a slot via CRC16(key) % 16384. Nodes own ranges of slots. Adding/removing nodes means migrating only some slots, not rehashing everything. Unlike naive modulo hashing, only K/N keys remapped when N changes.
Q4. What is the difference between Redis Sentinel and Redis Cluster?
Answer: Sentinel = high availability for single-shard Redis. Monitors master, promotes replica on failure. Cluster = horizontal scaling + HA. Data sharded across multiple masters. Each master has replicas. Built-in failover. Use Sentinel when dataset fits in one node. Use Cluster when you need to shard.
Q5. How do you prevent cache stampede in Redis?
Answer: Three approaches: (1) Mutex lock: first request acquires lock, fetches from DB, sets cache, releases lock. Others wait. (2) Probabilistic early expiration: before TTL expires, randomly refresh with small probability. (3) Cache warming: pre-load popular keys before they expire. Combination of all three is production-grade.