Redis Interview: Why 'Memory + Single Thread' Fails — 12 Core Internals Explained
This comprehensive Redis interview guide covers 12 core internals including memory model, single-threaded architecture, I/O multiplexing, data structures, persistence, replication, clustering, cache pitfalls, distributed locks, consistency patterns, and Redis 6.0/7.0 features with diagrams and source-level insights.
Why Redis Is Fast: Memory, Single Thread, I/O Multiplexing, Efficient Data Structures
Redis achieves 100,000+ QPS by combining four key factors: pure in-memory operations, a single-threaded execution model, I/O multiplexing via epoll, and highly optimized data structures.
1. Pure In-Memory Operations
All data resides in RAM. Random access latency is ~100 ns for RAM vs ~100 μs for SSD and ~10 ms for HDD — a 1,000,000x speed advantage. Redis also uses O(1) data structures (hash tables, skip lists, compressed lists) for near-instant lookups.
🧠 Data in memory + ⚡ O(1) data structures = blazing fast accessSummary: In-memory storage is the fundamental reason Redis is fast.
2. Single-Threaded Model
Contrary to intuition, single-threaded design avoids context-switching overhead, lock contention, and provides natural atomicity. Redis 6.0 added multi-threading only for network I/O and serialization; command execution remains single-threaded.
❌ Multi-thread: Thread1 🔒data → wait lock → execute → unlock
Thread2 ⏳wait lock... (overhead)
✅ Redis single-thread: Request1 → execute → Request2 → execute (seamless)3. I/O Multiplexing
Redis uses epoll (Linux) to monitor thousands of sockets with one thread, processing only ready connections. This avoids the thread-per-connection model that would exhaust system resources.
🧑🍳 Traditional (BIO): One waiter per table, stands idle while guests eat.
🦸 Multiplexing (epoll): One waiter watches entire hall, only approaches tables that signal.4. Efficient Data Structures
Redis tailors underlying structures per data type: SDS for Strings, quicklist (ziplist + doubly linked list) for List, intset + hashtable for Set, ziplist + hashtable for Hash, ziplist + skiplist for ZSet. It also uses custom allocators (jemalloc/tcmalloc), copy-on-write for RDB, and a simple text protocol.
Five Basic Data Types & Internal Encodings
Redis exposes five user-facing types but maps them to multiple internal encodings based on data size and element count (configurable via hash-max-ziplist-entries, zset-max-ziplist-entries, etc.).
String — Use cases: caching, counters, distributed locks. Underlying: Simple Dynamic String (SDS). Encodings: int, embstr, raw. Max size: 512 MB.
List — Use cases: message queues, article lists. Underlying: compressed list (ziplist) + quicklist. Encodings: ziplist, quicklist. Max elements: 2^32-1.
Set — Use cases: deduplication, set operations. Underlying: integer set (intset) + hashtable. Encodings: intset, hashtable. Max elements: 2^32-1.
Hash — Use cases: object storage, shopping carts. Underlying: compressed list (ziplist) + hashtable. Encodings: ziplist, hashtable. Max elements: 2^32-1.
ZSet — Use cases: leaderboards, delayed queues. Underlying: compressed list (ziplist) + skiplist. Encodings: ziplist, skiplist. Max elements: 2^32-1.
Internal Structure Deep Dive
1. SDS (Simple Dynamic String)
struct sdshdr {
int len; // used length
int free; // free space
char buf[]; // data array
}Three encodings: int (integer values), embstr (≤44 bytes, contiguous allocation), raw (>44 bytes). SDS provides O(1) length, binary safety, and pre-allocation/lazy free to reduce reallocations.
2. Ziplist (Compressed List)
Contiguous memory with variable-length encoding — extremely memory-efficient but suffers from O(N²) worst-case "chain updates" on insert/delete, so only used for small collections.
3. Quicklist
Linked list of ziplist nodes (introduced in Redis 3.2). Balances memory locality with efficient head/tail operations. Configured via list-max-ziplist-size.
4. Intset (Integer Set)
Sorted array of integers (int16/int32/int64) with automatic upgrade (no downgrade). Supports binary search.
5. Dict (Hash Table)
Chained hashing with incremental rehashing to avoid blocking on large keys. Used by Hash and Set (values set to NULL).
6. Skiplist
Multi-level linked list with O(log N) search/insert/delete. ZSet combines skiplist (range queries) with a dict (O(1) member→score lookup) sharing element data.
Advanced Data Types: Bitmap, HyperLogLog, Geo, Stream
Bitmap (Bit Array)
Treats String as bit array; each bit represents a state. Max 2^32 bits (~512 MB). Commands: SETBIT, GETBIT, BITCOUNT, BITOP. Use cases: user sign-in, online status, Bloom filter basis. 100M users ≈ 12 MB vs GBs in MySQL.
HyperLogLog (Cardinality Estimation)
Probabilistic structure using 16,384 buckets (6 bits each) = fixed 12 KB memory. Standard error ~0.81%. Commands: PFADD, PFCOUNT, PFMERGE. Cannot retrieve elements. Ideal for UV counting, unique visitor stats.
Geo (Geospatial)
Built on ZSet + Geohash (52-bit integer score). Commands: GEOADD, GEOPOS, GEODIST, GEORADIUS, GEORADIUSBYMEMBER. Precision ~0.6 m. Use cases: nearby people, delivery range.
Stream (Message Queue)
Introduced in Redis 5.0. Based on Radix Tree + List. Each message has unique ID (timestamp + sequence). Supports consumer groups (like Kafka), acknowledgment ( XACK), persistence, replay. Commands: XADD, XREAD, XGROUP CREATE, XREADGROUP, XTRIM. Not a full Kafka replacement for massive scale.
Persistence: RDB vs AOF vs Hybrid
RDB (Snapshots)
Forks child process to write binary .rdb file. Triggered by save m n rules or BGSAVE. Pros: fast restore, compact file, minimal main-thread impact. Cons: data loss since last snapshot, fork() blocks main thread on large datasets.
AOF (Append-Only File)
Logs every write command. Three fsync policies: always (slow, safest), everysec (recommended, ≤1s loss), no (OS-controlled). AOF rewrite ( BGREWRITEAOF) compacts file. Pros: high durability, readable. Cons: larger files, slower restore, frequent I/O.
Hybrid Persistence (Redis 4.0+)
Combines RDB snapshot (full) + AOF increments. On rewrite, child writes RDB preamble then appends subsequent AOF commands. File structure: [RDB binary][AOF commands]. Enabled by aof-use-rdb-preamble yes (default on in 5.0+). Best of both: fast restore + high durability + smaller size.
Production Best Practices
Enable hybrid persistence.
Use RDB only if data loss acceptable.
Avoid AOF-only; RDB better for backup/restore.
Tune save and appendfsync.
Off-site backup of persistence files.
Data Safety
↑
AOF ● (high)
│
Hybrid ●──┼──● RDB (fast)
└─────────→ Restore SpeedExpired Key Deletion: Lazy + Periodic
Redis uses a hybrid strategy to balance CPU and memory.
Lazy Deletion (Passive)
On every key access, expireIfNeeded() checks expiry; if expired, deletes immediately and returns nil. Pros: CPU-friendly. Cons: memory leak if expired keys never accessed.
if (key has expiry && now >= expiry) {
delete key;
return NULL;
}
return value;Periodic Deletion (Active)
Runs 10 times/sec (configurable hz). Each run: randomly sample 20 keys from expiry dict, delete expired ones. If >25% expired, repeat. Time-capped at 25 ms per run. Pros: bounds CPU, cleans orphaned keys. Cons: may miss some expired keys.
Why Combine?
Pure lazy → memory bloat. Pure periodic → either too infrequent (memory) or too aggressive (CPU spikes). Together they achieve low CPU, moderate memory, moderate real-time guarantees.
Memory Eviction Policies (8 Strategies)
When maxmemory reached, Redis evicts keys per maxmemory-policy. Categories: noeviction (error), random (allkeys/volatile), LRU (allkeys/volatile), LFU (allkeys/volatile), TTL (volatile). Redis 4.0+ recommends allkeys-lfu (best hit-rate) or volatile-lfu if permanent keys exist. Implementation uses approximate LRU (random sampling, default 5 keys) and LFU (Morris counter with decay).
Transactions: MULTI/EXEC, No Rollback
Redis transactions are command batches with atomic execution (no interleaving), but no rollback on runtime errors. Commands: MULTI, EXEC, DISCARD, WATCH (optimistic locking).
Error Handling
Syntax errors (detected at queue time) → EXEC aborts, no commands run.
Runtime errors (e.g., INCR on string) → offending command skipped, rest execute, no rollback.
Why No Rollback?
Performance: undo logs would slow single-threaded core.
Runtime errors are programmer bugs, should be caught in testing.
Simplicity: keeps implementation minimal.
Alternatives for Atomicity
WATCH+ MULTI/EXEC for CAS.
Application-level compensation.
Lua scripts (atomic execution).
High Availability: Replication → Sentinel → Cluster
Master-Slave Replication
Master handles writes; slaves replicate for read scaling and backup. Full sync (RDB) then incremental via replication backlog ( repl_backlog_buffer). Offset tracks consistency. Drawback: manual failover.
Sentinel
Automates failover. Three roles: monitor, notify, auto-failover. Flow: subjective down → objective down (quorum) → leader election (Raft) → promote best slave. Minimum 3 sentinels to prevent split-brain. Still single-master write bottleneck.
Cluster
Shards data across 16,384 hash slots ( CRC16(key) % 16384). Each master owns slot range; slaves provide HA. Decentralized: every node knows slot map. Supports horizontal scaling. Limitations: no multi-key ops, no transactions, no cross-slot Lua.
Replication — Core problem: read scaling, backup. HA: low (manual). Write capacity: single. Storage: single. Complexity: low. Best for: read-heavy, small data.
Sentinel — Core problem: auto failover. HA: medium (auto). Write capacity: single. Storage: single. Complexity: medium. Best for: read-heavy, need HA.
Cluster — Core problem: write scaling, sharding. HA: high (per-shard). Write capacity: distributed. Storage: sum of nodes. Complexity: high. Best for: large data, high write.
Cache Pitfalls: Penetration, Breakdown, Avalanche
Penetration — Query non-existent data; neither cache nor DB has key; every request hits DB. Solutions: Bloom filter (pre-check), cache empty values (short TTL).
Breakdown — Single hot key expires; high concurrency on expiring hot key; sudden spike on DB. Solutions: hot key never expires (async refresh), mutex lock (Redis SETNX or Redisson), pre-warm.
Avalanche — Mass key expiry / cache down; large-scale expiry or cluster failure; DB overwhelmed, cascade failure. Solutions: randomize TTL (jitter), HA cluster (Sentinel/Cluster), circuit breaker (Sentinel/Hystrix), multi-level cache (Caffeine + Redis).
Distributed Locks: SETNX, Redisson, RedLock
Native SETNX
SET lock_key unique_value NX EX 30Release via Lua script to ensure atomic check-and-delete. Drawbacks: non-reentrant, no auto-renewal, single-point failure.
Redisson
Mature Java client. Features: reentrant (hash with thread ID + count), watchdog auto-renewal (default 30s TTL, renew every 10s), fair/read-write/multi-lock. Still vulnerable to master-failover lock loss if replication lag.
RedLock (Red Algorithm)
Uses N independent masters (typically 5). Acquire lock on majority (N/2+1) within TTL. Solves single-point failure but controversial: clock drift, GC pauses, operational complexity. Rarely used in practice; prefer Redisson or CP systems (ZooKeeper/etcd) for strict consistency.
SETNX — Reentrant: ❌, Auto-renewal: ❌, Atomicity: partial, Single-point failure: ❌, Performance: ⭐⭐⭐⭐⭐, Complexity: ⭐, Production recommendation: ⭐
Redisson — Reentrant: ✅, Auto-renewal: ✅, Atomicity: ✅, Single-point failure: ❌, Performance: ⭐⭐⭐⭐, Complexity: ⭐⭐, Production recommendation: ⭐⭐⭐⭐⭐
RedLock — Reentrant: ✅, Auto-renewal: ✅, Atomicity: ✅, Single-point failure: ✅, Performance: ⭐⭐, Complexity: ⭐⭐⭐⭐⭐, Production recommendation: ⭐
Cache-Database Dual-Write Consistency
Core issue: two independent stores, non-atomic updates. Never update cache then DB (dirty cache on DB failure). Prefer Cache Aside : update DB, then delete cache. Add TTL as safety net.
Patterns
Update DB → Delete Cache (standard, eventual consistency).
Delayed Double Delete : delete, update DB, sleep, delete again (covers race window).
Retry via MQ : on delete failure, send message for retry.
Binlog Listener (Canal) : async cache invalidation from DB logs — decoupled, eventual consistency.
Distributed Lock : strong consistency for critical data (performance cost).
Update DB → Delete Cache — Consistency: eventual, Performance: high, Complexity: low, Use case: most scenarios.
Delayed Double Delete — Consistency: eventual, Performance: high, Complexity: low, Use case: high concurrency.
Canal + MQ — Consistency: eventual, Performance: high, Complexity: medium, Use case: high throughput, microservices.
Distributed Lock — Consistency: strong, Performance: low, Complexity: high, Use case: finance, payments.
Redis 6.0 / 7.0 New Features
Multi-Threaded I/O (6.0)
Only network read/write and protocol parsing are multi-threaded; command execution stays single-threaded. Configure io-threads 4 and io-threads-do-reads yes. Typical 2-3x QPS boost for large values/high concurrency.
ACL (6.0)
Fine-grained access control: multiple users, per-command, per-key-pattern, per-database permissions. Example:
ACL SETUSER alice on >password ~cache:* +@read +@write -@dangerous. Replaces single global password.
Redis Functions (7.0)
Named, persistent, versioned server-side scripts replacing EVAL. Loaded via FUNCTION LOAD, called via FCALL. Advantages: persisted in RDB/AOF, replicated as write commands (not script body), no-writes flag for read-only replicas, lower network overhead.
#!lua name=mylib
redis.register_function('myincr', function(key, amount)
return redis.call('INCRBY', key, amount)
end)
FUNCTION LOAD "$(cat mylib.lua)"
FCALL myincr 1 counter 10These three features close enterprise gaps: performance ceiling, security governance, and programmable atomic logic.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
