Back to All Concepts
DatabaseCachingPerformanceSystem DesignPro

Redis Internals: Why is it Fast?

Single-threaded Event Loop, RDB vs AOF Persistence, and Pipelining.

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

Connection Queue (Socket Buffer)
Waiting for requests...
Main Thread
Idle
0
Ops Done

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.

  1. One main thread runs an infinite loop.
  1. Zero Context Switch: No expensive thread overhead.
mermaid
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
Click to expand code...

Trade-off: Blocking commands (e.g., KEYS *) freeze all clients.


Persistence

RDB (Snapshot)

  • Every X minutes, fork and dump all RAM to .rdb file.
  • 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
Click to expand code...

Pipelining

Problem: Network Round Trips are expensive.

Without pipelining:

1 command → wait 50ms → 1 result
(max 20 ops/sec)
Click to expand code...

With pipelining:

python
pipe = redis_client.pipeline()
pipe.get('key1')
pipe.get('key2')
pipe.get('key3')
pipe.execute()  # Send all 3 at once!
Click to expand code...

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 keys
  • volatile-ttl: Evict keys with soonest expiration
  • noeviction: Return errors
maxmemory 2gb
maxmemory-policy allkeys-lru
Click to expand code...

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

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