Why Redis Is So Fast: Four Core Pillars, Real Incidents, and Source Code Insights
This article explains why Redis is fast through four core factors—memory storage, single-threaded command execution, I/O multiplexing, and optimized data structures—illustrated with three real production incidents, source code walkthroughs of key components like SDS and incremental rehashing, and practical guidelines for avoiding common pitfalls such as big keys, hot keys, and misconfigured eviction policies.
Introduction: Three Incidents, Three Lessons
A new ten-part series dedicated to Redis. It may be the component you use most but understand least: daily SET/GET operations, yet few can explain why it's fast, when it slows down, or how to fix it beyond three layers. This series aims to restore Redis from "a cache package" to "an in-memory data structure server," each article backed by real incidents, core source code, and production criteria.
As the opener, this article answers three fundamental questions:
Why Redis is fast (four aces: memory, single-threaded, multiplexing, data structures)
Whether single-threaded is an advantage or defect (why no lock contention, why one slow command can stall the whole site)
What it's actually suited for (not just cache—locks, counters, leaderboards, message queues are natural projections of the same data structures)
Three incidents map to three lessons:
Hit rate is the lifeline of cache; key design determines hit rate, not luck.
Single-threaded is both performance source and failure source; one slow command equals site-wide latency.
Memory is Redis's only hard constraint; when maxmemory fills, eviction policies decide for you—usually not what you want.
Incident Scene: Three Ways to Die
Incident 1: Cache Added, Database Got Busier
Team added Redis for product details, expecting DB QPS to halve. A week later: hit rate only 28%, DB QPS unchanged, extra network overhead. Root causes:
Cache keys were concatenated strings; same product generated 10+ keys due to parameter order differences (e.g., /p/1001?c=3&s=1 vs /p/1001?s=1&c=3).
All TTLs set to same 300 seconds, causing simultaneous generation and expiration.
Hit rate is designed, not gifted with Redis —key normalization and TTL jitter decide cache survival.
Incident 2: One 500KB Value, Whole Site Queued
Someone cached entire product detail HTML (~500KB) as a single string. At 8000 QPS, single-threaded Redis spent 4GB/sec serializing/transmitting values. Each GET occupied main thread for hundreds of microseconds; all other commands—including other businesses' SET —queued behind it. Site-wide P99 latency jumped from 8ms to 130ms while CPU only 20%. In single-threaded systems, the bottleneck isn't CPU, it's queuing.
Incident 3: Memory Full, Hot Data Evaporates
Memory hit 100%, clients reported abnormal prices. Root cause: maxmemory reached, default noeviction caused write errors. Ops switched to allkeys-lru, but hot price caches without TTL were bulk-evicted, triggering backsource avalanche. Memory is Redis's only hard constraint —when full, the system decides for you, and that decision is rarely what you want.
Three incidents point to one realization: Redis is an in-memory data structure server, not an infinite-capacity accelerator. To master it, you must understand its four aces and two lifelines.
Technical Analysis: Four Aces and Two Lifelines
2.1 Positioning: What It Is, What It Isn't
Redis stands for REmote DIctionary Server — a data structure server running in memory . "Cache" is just its most popular use case, not its essence. Its core capability: operate in-memory data structures at O(1) or O(logN) complexity . Understanding this positioning makes its five classic uses derivations: cache (KV read acceleration), distributed lock ( SETNX semantics), counter ( INCR atomic increment), leaderboard (ZSET sorting), message queue (List and Stream). Not five features, but five projections of one data structure set.
Equally important: what it is not :
Not a database —though RDB/AOF persist, reliability ceiling can't match MySQL.
Not a search engine —no inverted index or relevance scoring.
Not a graph database .
Accidents from forcing it into these roles appear in later articles.
2.2 Why Fast: A Latency Ledger
Memory random read ~ 100 ns
Redis single command ~ microseconds (incl. network)
Same-DC RPC ~ 1 ms
SSD random read ~ 100 μs
MySQL with index ~ 10 ms
HDD random read ~ 10 msThis table explains everything: Redis is fast primarily because data lives in memory , random access is three orders faster than SSD, five orders faster than HDD. But memory is only necessary condition—if every command needed locks, lock contention at 10k concurrency would eat all gains. So second ace: single-threaded —command execution lock-free, no context switches, deterministic ordering; at 100k QPS CPU spends cycles on "work" not "coordination". Third ace: I/O multiplexing (the epoll Reactor model from Netty series; Redis's aeEventLoop is minimalist implementation). Fourth ace: custom data structures for own access patterns —SDS, dict, skiplist, each optimized to the extreme for specific patterns.
2.3 Single-Threaded Model: 6.0's Compromise
Many know "Redis is single-threaded," but post-6.0 that's imprecise. Accurate statement: Command execution remains single-threaded; network I/O optionally multi-threaded from 6.0 . Why? Benchmarks showed large-value scenarios: read/write syscalls and protocol parsing consumed 50%+ CPU, command execution only a fraction—bottleneck shifted from "compute" to "move". I/O threads only move data (read requests, write responses); parsing and execution stay serialized on main thread, so no concurrency issues introduced , atomicity guarantees identical to 5.0.
This design yields a key criterion: Unless your values are generally >10KB and QPS extremely high, don't enable I/O multi-threading —default 4 I/O threads add inter-thread sync overhead on small values. Diagnose performance by checking which segment in the 2.2 latency ledger burns time, then tune.
2.4 Data Structure Overview: Five Types, Multiple Encodings
Externally five basic types (string, hash, list, set, zset); internally each maps to multiple underlying encodings, auto-switching by data scale: small data uses compact structures to save memory, large data uses efficient structures to keep speed . This table is the foundation for all later performance discussions.
<strong>string</strong> int / embstr / raw (SDS)
<strong>hash</strong> listpack (small) → dict (large)
<strong>list</strong> quicklist (listpack nodes chained)
<strong>set</strong> intset (all-int small set) → listpack → dict
<strong>zset</strong> listpack (small) → skiplist+dict
Plus trio: bitmap / hyperloglog
GEO (built on string and zset)Two usage criteria derive directly:
Small elements, few elements = best performance and memory efficiency (listpack contiguous memory, cache-friendly), so hash storing objects should split fields, control field count.
Crossing encoding thresholds changes complexity abruptly (e.g., hash from O(N) scan to O(1) lookup), so stress tests must cover real element scales, not three elements then ship.
2.5 Expiration and Eviction: Two Different Lines
Beginners confuse this pair:
Expiration deletion handles "TTL key expired, what now?" (lazy deletion + periodic sampling, details in article 06).
Memory eviction handles "memory full, who stays, who goes?" (eight maxmemory-policy options, details in article 06).
Former is time problem, latter space problem. Incident 3's chaos came from treating them as same: TTL set doesn't mean memory won't grow—expired key deletion is async sampling; when write speed exceeds deletion speed, memory still climbs. Production must monitor both: expired key backlog (watch expired_keys curve) and memory water level (ratio of used_memory to maxmemory ) .
2.6 Persistence Overview: Snapshot and Log
Persistence trade-off in one sentence: RDB is a point-in-time full snapshot (fork child process writes full disk, fast recovery, more loss); AOF is append-only log of every write command (less loss, larger file, slower recovery). Not either/or but combined—4.0+ supports mixed persistence (AOF file first half RDB-format full, second half incremental commands). Remember for now: Pure cache can disable persistence for performance; data that "hurts if lost" must use AOF everysec . Deep dive in article 03 covering fork, COW, AOF rewrite source code.
2.7 Three-Layer Cache Architecture: Who Goes Where
Redis isn't cache terminus. Mature systems standard posture: In-process cache (Caffeine/local map) → Redis → Database . Local cache solves "hot key repeated deserialization cost" and "Redis network RTT 0.5ms"; Redis solves "cross-instance data consistency and capacity"; Database solves "persistence and authority". Placement criteria: Extremely hot, tolerates brief inconsistency → local (config, switches); cross-instance shared → Redis; rest → direct DB query . Systems skipping local cache straight to Redis eventually hit Incident 2's "single-threaded blasted by hot key"—local cache is hot key's first spillway, revisited in articles 07 and 10.
Core Flow: A SET Command's Complete Journey
Scenario: Client sends SET product:1001 "…" to single-node Redis, I/O multi-threading enabled, AOF everysec configured.
① Client sends RESP-protocol-encoded byte array
② Kernel receives packet, epoll notifies I/O thread ready
③ I/O thread assignment: this client handled by thread i
④ I/O thread <code>read</code> into request buffer
⑤ Main thread wakes, batch-harvests all I/O thread results
⑥ Main thread parses command, looks up command table gets <code>setCommand</code>
⑦ Validation: arg count, memory water level, read-only replica
⑧ Dict write: compute hash → locate bucket → create entry
⑨ If rehash enabled, move one bucket opportunistically (incremental)
⑩ Write AOF buffer (<code>aof_buf</code>); TTL into expiry dict
⑪ Write replication buffer, I/O thread <code>write</code> back to client
⑫ Main thread proceeds to next command, lock-free throughoutKey Point 1: Protocol Is RESP, Human-Readable
Redis protocol encoding minimal: *3 means array of three elements, $5 means following string 5 bytes. This "self-describing + line-delimited" design lets parser be written in hundreds of lines, and why telnet can debug Redis. Performance-wise it sacrifices compactness for implementation simplicity—a good trade for in-memory DB because bottleneck never in protocol bytes.
Key Point 2: Step ⑧ Is the Only "Compute"; Rest Is Moving
In a real SET latency breakdown, memory hash compute and write take microseconds; vast majority time in network RTT and kernel send/receive. That's why 6.0 moved I/O to multi-thread while keeping command execution single-threaded—optimize where time is spent.
Key Point 3: AOF Is Buffer-First, Disk-Later
Step ⑩ only writes aof_buf memory buffer; actual fsync by background thread per appendfsync policy (everysec = once per second). Thus "command returns success" ≠ "persisted". Crash loses at most one second of writes—this is AOF everysec's contract, not a bug.
Key Point 4: Step ⑨ Is One of Redis's Most Elegant Designs
Dict expansion requires moving all data; doing it at once causes millisecond stalls (amplifier of Incident 2). Incremental rehash spreads "move" across each insert/delete/update, moving one bucket per operation— turns one O(N) pause into N O(1) opportunistic chores . Article 03 shows fork persistence uses same "spread" philosophy.
In these 12 steps, latency distribution determined by ②④⑪ (network moving) and ⑧ (command execution); reliability determined by ⑩. Understanding this chain lets you pinpoint any performance issue to a specific step.
Core Source Code: Five Segments to Read Redis's Heart
4.1 aeMain: The Main Loop of Everything
void aeMain(aeEventLoop *el) {
el->stop = 0;
// Before sleep: process due time events
while (!el->stop) {
if (el->beforesleep)
el->beforesleep(el);
aeProcessEvents(el, AE_ALL_EVENTS|
AE_CALL_BEFORE_SLEEP);
}
}This is Redis's heart: an infinite while loop, each round processing "file events" (network I/O) and "time events" (expiry sampling, cluster heartbeats, rehash timing, etc.). Compared to Netty series article 02's NioEventLoop.run, structure identical— all high-performance single-threaded servers share skeleton: wait events, handle events, back to wait . Redis 6.0's I/O multi-threading just outsources "read" and "write" segments from this loop; main loop shape unchanged.
4.2 I/O Thread Division: 6.0's Key Change
// Main thread distributes pending-read clients to I/O threads
int handleClientsWithIO(void) {
// Round-robin assign to each I/O thread queue
while ((ln = listNext(&li))) {
tid = item_id++ % io_threads_num;
listAddNodeTail(io_threads_list[tid], ln->value);
}
// Participate itself, then wait for all to finish
waitForThreadsIO(IO_THREADS_OP_READ);
return 0;
}Two design details worth savoring:
Assignment is round-robin, not load-based —client-granularity round-robin sufficiently uniform, avoids load-statistics overhead.
Sync uses spin-wait, not locks — io_threads_op flag + busy wait, because wait time microseconds; lock context-switch cost not worth it. This is "single-threaded execution + multi-threaded moving" in implementation: threads share almost no data, only a flag.
4.3 processCommand: One Command Table Rules All
int processCommand(client *c) {
c->cmd = lookupCommand(c->argv, c->argc);
if (c->cmd == NULL)
return errUnknownCommand(c);
if (!checkArity(c))
return C_ERR;
// Memory full / read-only / in transaction… sequential gate checks
if (!checkMemoryWater(c))
return addReplyErrorOOM(c);
call(c, CMD_CALL_FULL);
return C_OK;
}All 200+ commands pass through this single gate: lookup → validate → call . Validation chain is the defense line you hit in Incident 3—memory water level check (write commands error OOM under noeviction at high water), read-only check, cluster slot check, transaction state check, queued sequentially. Each command registers its sflags and time complexity in the table; INFO commandstats can stats per-command call count and avg latency—when debugging "Redis slowed", first thing is check this table for abnormal usec_per_call.
4.4 Incremental Rehash: Turning Pause into Opportunistic Work
int dictRehash(dict *d, int n) {
int empty_visits = n * 10;
while (n-- && d->ht[0].used) {
while (!d->ht[0].table[d->rehashidx]) {
if (--empty_visits == 0)
return 1;
d->rehashidx++;
}
dictEntry *de = d->ht[0].table[d->rehashidx];
while (de) {
dictEntry *nx = de->next;
uint64_t h = dictHashKey(d, de->key) & d->ht[1].sizemask;
de->next = d->ht[1].table[h];
d->ht[1].table[h] = de;
d->ht[0].used--;
d->ht[1].used++;
de = nx;
}
d->ht[0].table[d->rehashidx]=NULL;
d->rehashidx++;
}
if (d->ht[0].used == 0) {
// Done: ht1 replaces ht0
return 1;
}
return 0;
}Three details make "incremental" concrete:
ht[0] and ht[1] online simultaneously , during rehash reads check both tables, writes only go to ht[1], service uninterrupted.
empty_visits rate-limits —10×n consecutive empty buckets and it stops, prevents sparse dict turning one "opportunistic" into big scan.
Every dict add/delete/update calls _dictRehashStep to move one bucket opportunistically , background timer does batch 1ms moves; two-pronged ensures migration always completes. Cost: double memory during rehash, reads check two tables—classic engineering trade: "small continuous cost to eliminate one big pause".
4.5 SDS: Why Not C Strings
// sds.h: different headers by length
struct sdshdr8{
unsigned char len; // used
unsigned char alloc; // total capacity
unsigned char flags;
char buf[]; // flexible array
};
struct sdshdr16{
uint16_t len;
uint16_t alloc;
unsigned char flags;
char buf[];
};
// 32/64 similar, header scales with lengthSDS uses one header to gain four capabilities C strings lack:
O(1) length retrieval (len field ready, no strlen scan)
Binary safe (no reliance on \0 terminator, can store image serialization bytes)
Buffer overflow prevention (append checks alloc - len remaining, expands if insufficient)
Space pre-allocation reduces reallocations (expansion gives 2x extra, consecutive appends amortize cost)
Even tastier is flags byte : low bits record "which header currently used"; short strings use 3-byte sdshdr8, long strings upgrade to 64-bit header— bytes saved per string × billion keys = several GB . This bit-level memory thrift runs through all Redis data structures (article 02 shows listpack and intset even more extreme).
Advanced Topics: Making Fast Measurable
5.1 Command Time Complexity = Latency Floor
Single-threaded model gives iron law: One command's execution time = wait time for all commands behind it . So each command's time complexity isn't theoretical but site-wide latency issue: GET/SET/INCR are O(1) LRANGE full range is O(N), N = collection length KEYS is O(entire DB), million keys stalls main thread seconds
Production must disable KEYS, use SCAN cursor batching —SCAN scans only part of one bucket per call, turns O(N) into multiple O(shard), same philosophy as incremental rehash. Similarly, collection commands should use COUNT param or SPOP/SSCAN batching.
5.2 Hit Rate Accounting: How to Calculate, How to Improve
Hit rate = keyspace_hits / (hits + misses), INFO shows instantly. Engineering reference lines: Read-heavy detail caches should exceed 90%; below 80% means key design needs rework . Three hit-rate boosters:
Key normalization (sort params before concatenation, one business entity = one key)
TTL stratification (core data long TTL + active update, long-tail short TTL, within same layer add 0-10% random jitter to prevent simultaneous expiry)
Pre-warming (inject hotspots before launch, avoid cold-start penetration)
Incident 1's 28% hit rate resulted from using none of these three.
5.3 Big Keys and Hot Keys: Two Danger Maps
Big Key: string value >10KB, collection elements >5000 = danger zone. Three-layer harm: command execution hogs main thread (Incident 2), network transmission squeezes bandwidth, synchronous deletion blocks (4.0 uses UNLINK async delete). Detect with redis-cli --bigkeys or MEMORY USAGE; remediation: split (hash sharding) and replace (store references not full content).
Hot Key: Single key QPS >25% of instance = hotspot; single shard overwhelmed while other shards idle. Solutions by priority: local cache fallback, key suffix replication to N replicas (read scenarios), client-side randomized access paths, ultimately cluster expansion with hot key migrated to dedicated instance. Article 07's "breakthrough" and article 10 expand full solutions.
5.4 Single-Instance Engineering Boundaries
Experience boundaries: single instance memory ≤10GB (larger, fork persistence and recovery duration spiral), QPS ≤100k (drops sharply with large values), key count tens of millions (larger, SCAN and migration slow). Beyond boundaries, don't hard-tune parameters; do two things: Read-write separation (replicas serve reads, cost = master-replica lag) and Sharding (client-side sharding or Cluster, article 05). Beyond numbers, more important mindset: First diagnose whether "can't compute", "can't move", or "can't fit"—three bottlenecks need three completely different remedies .
5.5 High-Frequency Pitfall Checklist
KEYS in production. O(full DB), million keys stalls main thread seconds. Always use SCAN.
TTL all same value. Simultaneous generation and expiry creates backsource spike. Add 0-10% random jitter.
Storing large JSON as string, repeated serialize/deserialize. Every read/write full serialization. Split object into hash, read/write by field.
Not setting maxmemory. Memory grows to physical limit, swap starts, performance cliff-dives with no alert signature.
Eviction policy chosen arbitrarily. noeviction full → write errors; allkeys-lru no TTL protection. Choose policy by data tier.
DEL large collection. Synchronous release of millions of elements blocks main thread. Use UNLINK or 4.0+ lazyfree.
Using RDB as backup without AOF. Writes between two snapshots all lost; "cache can rebuild" fails when warmup cost high.
Treating single instance as infinite capacity. 10GB, 100k QPS are engineering boundaries not recommendations; approaching them means plan sharding, not pray.
Learning Path and Interview Questions
6.1 Four-Stage Roadmap
Entry (3 days): Install single-node Redis, play with all five types, use OBJECT ENCODING to watch encoding switches as data volume changes; use telnet send raw RESP command, walk the 3.1 journey.
Intermediate (1 week): Benchmark compare: 10KB vs 1KB value QPS difference; KEYS vs SCAN at 1M keys main thread behavior; memtier-benchmark or redis-benchmark produce your own machine baseline numbers.
Deep (2 weeks): Compile Redis with debug symbols, gdb single-step through a SET; read server.c main and ae.c loop, draw "which time events registered"; read dict.c cover to cover.
Practice (ongoing): Do a Redis health check for your project: hit rate, big key scan, slowlog TOP10 ( SLOWLOG GET), memory composition analysis ( INFO memory). Health report beats memorizing source code.
6.2 Answer Framework: Redis Four-Part Argument
Any Redis question answered in four parts: ① Structure — underlying encoding, when it switches; ② Memory — how much this design costs, how much it saves; ③ Thread — main thread execution or background thread, will it block; ④ Persistence — what this operation leaves in RDB/AOF. Four parts done, depth emerges.
6.3 Twenty Interview Questions (with Answers)
Q1 · Why is Redis fast?
Answer: Four aces: data in memory (random read 3 orders faster than SSD); command execution single-threaded (no locks, no context switches, no contention overhead); I/O multiplexing (epoll event-driven single thread handles 10k+ connections); custom data structures for access patterns (SDS, dict, skiplist). Note: memory is primary; other three prevent memory advantage being wasted by locks and I/O.
Q2 · What did Redis 6.0's multi-threading change? Is command execution still single-threaded?
Answer: Only outsourced network read/write (read/write syscalls and partial protocol parsing) to I/O threads; command parsing and execution remain serialized on main thread. Atomicity guarantees identical to 5.0. Enable condition: large values (10KB+) and CPU bottleneck; small-value scenarios default off is faster because thread sync overhead > gains.
Q3 · Why no lock contention in single-threaded? What happens with slow command?
Answer: All commands execute serially in one thread, naturally no locks needed. Cost: commands fully queue; one 100ms slow command delays all subsequent requests by at least 100ms. So KEYS type O(full DB) commands banned in production; slowlog ( SLOWLOG ) is first debugging entry; any O(N) command must evaluate real N scale.
Q4 · How to choose between Redis and Memcached?
Answer: Memcached only multi-threaded KV, no persistence, no rich structures, wins in simple scenarios with huge values and ultra-high concurrency. Redis has five structures + persistence + replication + cluster + Lua, comprehensive ecosystem. Default choose Redis unless pure KV, large values, and need multi-threading to squeeze CPU.
Q5 · What goes in local cache, Redis, database three layers?
Answer: Local cache: extremely hot, tolerates brief inconsistency (config, switches, category trees)—prevents hot keys and saves one network RTT; Redis: cross-instance shared hot data; Database: sole authority source. Criteria two questions: Do multiple instances need same data? Can data change tolerate seconds of inconsistency? Both "no" → local cache.
Q6 · SDS advantages over C strings?
Answer: O(1) length (len field ready); binary safe (no \0 dependency, stores arbitrary bytes); overflow prevention (append checks alloc-len remaining, auto-expands); pre-allocation and lazy free reduce reallocs; length-based 8/16/32/64-bit headers, short strings save memory. C strings' four sins (O(N) length, not binary safe, overflow prone, frequent realloc) each solved.
Q7 · What is incremental rehash? Why designed this way?
Answer: Expansion doesn't move all at once; ht[0] and ht[1] dual tables online, each dict operation moves one bucket opportunistically ( _dictRehashStep ), background timer batches 1ms moves, empty_visits rate-limits empty bucket scans. Motive: single-threaded system one O(N) full migration = N ms pause; spread into N O(1) keeps service running. Cost: double memory during migration, reads check two tables.
Q8 · Skiplist query complexity? Why not red-black tree?
Answer: Average O(logN), multi-level linked lists index by probability (each level p=1/4). Three reasons for skiplist over RB-tree: implementation far simpler; range queries naturally efficient (bottom layer ordered linked list, ZRANGE walks directly); probabilistic level promotion avoids rotation rebalancing, concurrency and debugging friendlier. Author's words: skiplist constants and tuning space more controllable.
Q9 · What does a SET command go through inside Redis?
Answer: RESP bytes into kernel → epoll notify (6.0 I/O thread may read) → main thread parses command → lookup command table → sequential gate checks (memory water/read-only/cluster slot) → dict locate bucket write entry (opportunistic rehash one bucket) → write AOF buffer and expiry dict → write replication buffer, I/O thread writes back. Full single-threaded execution, network I/O optionally multi-threaded.
Q10 · Which common commands are O(N)? How to avoid?
Answer: KEYS (O full DB), SMEMBERS/HGETALL full (O elements), LRANGE 0 -1 , DEL large collection, ZUNIONSTORE large collection. Avoidance four moves: SCAN replaces KEYS; batch collections ( SSCAN /COUNT); DEL → UNLINK async release; large collections shard (hash by field into 100 sub-keys). Core: evaluate real N scale, not just syntax.
Q11 · Big key standard? How to detect and govern?
Answer: Rule of thumb: string value >10KB, collection elements >5000, or single key memory >1MB. Detection three ways: redis-cli --bigkeys sampling scan, MEMORY USAGE precise measure, RDB offline analysis tools most accurate. Governance: large string objects split hash by field; collections hash-shard; store references (URL/ID) not full content; delete with UNLINK.
Q12 · What happens when maxmemory full?
Answer: Depends on policy: noeviction (default) write commands return OOM error, reads unaffected; allkeys-lru etc evict per rule to free space. Both are accidents: former business errors, latter hot data without TTL bulk-evicted triggers backsource avalanche. Correct posture: 70% water level alert, choose policy by data importance (critical data volatile-ttl or isolate on separate instance).
Q13 · How are expired keys deleted?
Answer: Lazy + periodic dual strategy: delete on access if expired (maybe long-unaccessed expired keys occupy memory); background every 100ms randomly samples 20 TTL keys, if expired ratio >25% repeat round. So expiry ≠ immediate release; when write speed > cleanup speed memory still grows. Monitor must watch expired_keys and memory water level two lines, not just trust TTL.
Q14 · How to choose between RDB and AOF?
Answer: RDB = fork child process full snapshot: small file, fast recovery, loses data between two snapshots; AOF = append write commands everysec: loses at most 1 sec, large file, slow recovery, needs rewrite compression. Production commonly uses 4.0+ mixed persistence (RDB full + AOF incremental), balances recovery speed and loss window. Pure cache can disable all; stateful data (counters, queues) must use AOF everysec.
Q15 · Basic master-replica replication flow?
Answer: First full sync: replica sends PSYNC → master bgsave generates RDB transmits → replica flushes loads, meanwhile master new writes go to replication buffer then catch-up. After incremental: commands pushed by offset continuously; replica reconnects if offset still in replication backlog (default 1MB ring) then partial resync, else fallback full sync. Master-replica lag and backlog size are two tuning points, article 04 expands.
Q16 · How to calculate hit rate? What's healthy?
Answer: hits/(hits+misses), INFO shows. Reference: detail-type read-heavy >90% healthy, <80% rework key design. Boost three moves: key normalization (param sort prevents duplicate keys), TTL stratification + random jitter prevents simultaneous expiry, launch pre-warm injects hotspots. Sudden hit rate drop usually batch expiry or eviction misfire; check expired/evicted curves first then code.
Q17 · How much can single instance handle? When to shard?
Answer: Experience boundaries: QPS 100k (large value drops), memory 10GB (fork and recovery time spiral), key count tens of millions. Approaching boundary: first read-write separation (replicas serve reads), writes still overwhelmed then shard (client sharding or Cluster). Before sharding confirm bottleneck type: can't compute (CPU), can't move (NIC/large value), can't fit (memory)—three bottlenecks need three different medicines.
Q18 · How to solve hot key problem?
Answer: Single key QPS >25% instance = hotspot, reads and writes blast single shard. By priority: ① Local cache fallback (first spillway, tolerates seconds inconsistency); ② Read scenario key suffix replicate N copies distribute (write must sync N copies); ③ Client randomize access paths; ④ Ultimately expand cluster and migrate hot key to dedicated instance. Monitor with redis-cli --hotkeys or client instrumentation.
Q19 · Fork persistence principle and risks?
Answer: bgSave calls fork copies parent page tables (not memory), child process uses copy-on-write (COW) to snapshot to disk. Three risks: fork instant page table copy on large memory instances takes dozens to hundreds ms (main thread stalls); COW period parent writes more → memory bloats more (extreme case doubles, triggers OOM); so instance memory must leave 50% headroom, large instances avoid frequent RDB.
Q20 · How to troubleshoot Redis slowdown?
Answer: Check five things in order: ① SLOWLOG slow command TOP (complexity and big keys); ② INFO memory near maxmemory and swap; ③ INFO stats evicted_keys (eviction misfire); ④ Host level fork duration and COW memory ( latest_fork_usec ); ⑤ Large value scenario evaluate enabling I/O multi-threading. Mantra: single-threaded system, find who occupies main thread, you find the answer.
6.4 One-Page Cheat Sheet
Four aces: Memory · Single-thread · epoll · Structures Single-thread = No locks = Commands fully queued Slow command 100ms = Site-wide latency 100ms KEYS banned, SCAN cursor batch Big key: 10KB / 5000 elements red line Hot key: Local cache is spillway Expiry ≠ Immediate release (lazy+periodic) Full ≠ Auto free space (watch policy) Boundaries: 10GB / 100k QPS larger then shard One iron law: In single-threaded system, find who occupies main thread, you find the answer
Summary
Three sentences summary.
First, Redis's speed is multiplication of four aces —memory solves "compute fast", single-thread solves "no internal friction", multiplexing solves "handle connections", data structures solve "no memory waste"; missing any is crippled.
Second, single-threaded is its character : all atomicity from serialization, all latency accidents from serialization; understanding "command queue" means understanding half of Redis production issues.
Third, memory and hit rate are two lifelines —former decides if it survives, latter decides if it's worth living; this article's three incidents each died from breach of these two lines.
Summary: Panorama map: five object types multiple encodings (article 02 each), RDB/AOF (article 03), master-replica & sentinel (article 04), Cluster (article 05), expiry & eviction (article 06), penetration/breakthrough/avalanche (article 07), cache consistency (article 08), distributed lock (article 09), big key hot key & monitoring (article 10 finale). Next article enters foundation of foundations: Data Structures and Object Encodings — The Memory Thrift Hidden in SDS, listpack, skiplist , explaining every byte behind OBJECT ENCODING.
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.
