The Speed of Single-Threaded Design
Redis handle 100,000 requests/second. It is widely known to be Single-Threaded.
- Question: How can it be fast if it uses only 1 CPU core?
- Answer: Because it's I/O Bound, not CPU Bound. Accessing RAM is fast (nano-seconds). The bottleneck is waiting for the Network.
Event Loop
Redis Single-Threaded Event Loop
Notice: Even though Redis uses I/O Multiplexing to handle thousands of connections, it executes commands sequentially. A slow command (like KEYS *) blocks the entire server for everyone.
- One main thread runs an infinite loop.
- Zero Context Switch: No expensive thread overhead.
sequenceDiagram
participant C1 as Client 1
participant C2 as Client 2
participant R as Redis (Main Thread)
C1->>R: GET key1
R->>C1: value1
C2->>R: SET key2 val
R->>C2: OK
Trade-off: Blocking commands (e.g., KEYS *) freeze all clients.
Persistence
RDB (Snapshot)
- Every X minutes, fork and dump all RAM to
.rdbfile. - Pro: Fast restarts.
- Con: Lose minutes of data if crash.
AOF (Append Only File)
- Log every write command to a file.
- Pro: Minimal data loss.
- Con: Slow restarts (replay log).
Config:
appendonly yes appendfsync everysec # Balanced: ~1 sec loss
Pipelining
Problem: Network Round Trips are expensive.
Without pipelining:
1 command → wait 50ms → 1 result (max 20 ops/sec)
With pipelining:
pipe = redis_client.pipeline()
pipe.get('key1')
pipe.get('key2')
pipe.get('key3')
pipe.execute() # Send all 3 at once!
Result: 10x-100x throughput.
Data Structures
Redis isn't just Key-Value. It's a Data Structure Server:
- String:
SET counter 1,INCR counter - Hash:
HSET user:123 name "Alice" - List:
LPUSH queue task1,RPOP queue - Set:
SADD tags "redis",SISMEMBER tags "redis" - Sorted Set:
ZADD leaderboard 100 "Alice",ZRANGE leaderboard 0 9
Memory Management
Eviction Policies (when maxmemory is hit):
allkeys-lru: Evict least recently used keysvolatile-ttl: Evict keys with soonest expirationnoeviction: Return errors
maxmemory 2gb maxmemory-policy allkeys-lru
Interview Tips
- Why single-threaded? "I/O bound workload. Event loop via epoll handles 100k connections with zero context switching."
- Persistence trade-off: "RDB for fast restarts, AOF for durability, or hybrid (RDB + AOF incremental)."
- Use cases: "Session storage, leaderboards (sorted sets), pub/sub messaging."
- Blocking commands: "Avoid KEYS in production. Use SCAN instead."
Related Concepts
- Caching Strategies — Write-through vs write-back
- Database Replication — Master-replica patterns
- Eviction Policies — LRU, LFU, TTL
About ScaleWiki
ScaleWiki is an interactive educational platform dedicated to demystifying distributed systems, software architecture, and system design. Our mission is to provide high-quality, technically accurate resources for software engineers preparing for interviews or solving complex scaling challenges in production.
Read more about our Editorial Guidelines & Authorship.
Educational Disclaimer: The architectural patterns and system designs discussed in this article are based on common industry practices, technical whitepapers, and public engineering blogs. Actual implementations in enterprise environments may vary significantly based on specific product requirements, legacy constraints, and evolving technologies.
Related Articles
Redis Internals: Why is it Fast?
Deep dive into Redis architecture: single-threaded event loop, data structures, persistence strategies (RDB/AOF), replication, and cluster mode.
LSM Trees (Database Internals)
Log-Structured Merge Trees: The data structure powering write-heavy databases like Cassandra, RocksDB, and DynamoDB.
Document Databases
The most popular type of NoSQL database. Storing data in flexible, JSON-like documents with embedded structures and dynamic schemas.