🔴

Redis Complete Guide: From Data Structure Internals to Cloud-Native Production

Master Redis end-to-end: SDS/skiplist/listpack/hashtable source code, RDB/AOF persistence, replication and Sentinel/Cluster HA, ae event loop internals, memory eviction, Lua/transactions/Pub-Sub, RedisSearch/JSON modules, Java/Python/Go clients, Aerospike/Dragonfly/Valkey comparisons, cloud vendor forks (Tair/MemoryDB), cache design patterns, distributed locks, 10 real use cases, and 10 production incident post-mortems. 43 chapters, completely free.

43
Chapters
Free
Forever
Start Reading →
Table of Contents
Ch01
Redis Is More Than a Cache
Six Redis use cases, why single-thread beats multi-thread, event-driven design philosophy, boundaries with MySQL/MongoDB, when to use Redis and when not to
Ch02
Open-Source Competitors: Memcached, Aerospike, Dragonfly, Valkey, KeyDB
Feature matrix and benchmarks for six competitors, Aerospike hybrid memory architecture deep dive, Dragonfly multi-thread Actor model, Valkey fork story, selection decision tree
Ch03
Cloud Managed Redis: ElastiCache, MemoryDB, Alibaba Tair
ElastiCache vs MemoryDB (Raft-backed durability), Alibaba Tair six extended types, Redis license controversy and Valkey fork, cloud vendor comparison
Ch04
redisObject: 12 Encodings and Memory Layout
robj struct byte-by-byte, 12 OBJ_ENCODING transitions, 0-9999 shared integer pool, embstr vs raw threshold, LRU/LFU 24-bit field sharing design
Ch05
SDS Dynamic String: Why Not C String
Five sdshdr variants, packed attribute memory alignment, space pre-allocation and lazy free, binary safety, O(1) length, SDS and C string interop
Ch06
listpack and ziplist: The Cascade Update Disaster and Fix
ziplist byte layout, prevlensize cascade update O(n) worst case, how listpack fixes it by removing prevlen, encoding field reuse design
Ch07
Skiplist: Why Not Red-Black Tree
Why skiplist over red-black tree (range queries, simpler impl, concurrency), zskiplistNode structure, random level algorithm, ZRANGE source walkthrough
Ch08
quicklist: listpack + Doubly Linked List Hybrid
quicklistNode and quicklist structures, fill parameter, LZF compression for cold nodes, config parameters, List encoding upgrade triggers
Ch09
Hash Table and Incremental Rehash
dictht/dictEntry/dict layers, two-ht incremental rehash timing, load factor triggers, safe iterator design, listpack→hashtable encoding upgrade
Ch10
Bitmap, HyperLogLog and GEO: Three Special Data Types
Bitmap SDS storage, HyperLogLog cardinality estimation (LogLog + dense/sparse encoding), GEO GeoHash 52-bit encoding, GEORADIUS range query
Ch11
Stream: Kafka-Style Message Stream Inside Redis
Radix Tree + listpack dual storage, Stream Entry ID design, Consumer Group internals, XADD/XREAD/XACK/XPENDING semantics, PEL pending list, vs Kafka differences
Ch12
Command Execution Pipeline: From TCP Bytes to +OK Response
TCP bytes → RESP parse → readQueryFromClient → processCommand → lookupCommand → setCommand → encoding → TTL → AOF propagation → replica propagation → addReply full source
Ch13
RDB Snapshot: fork, COW and File Format
BGSAVE fork+COW, rdbSave encoding rules, RDB file header/EOF/CRC64 byte-by-byte, BGSAVE triggers, RDB file repair
Ch14
AOF Persistence: Write-After-Log and Rewrite
Three fsync strategies performance vs safety, AOF rewrite fork+cow+delta buffer, mixed RDB+AOF persistence, AOF file structure and manual repair
Ch15
Persistence Strategy and Disaster Recovery
RDB vs AOF vs mixed safety matrix, use-case selection guide, redis-check-rdb/aof tools, production backup strategy, disaster recovery drills
Ch16
Replication: PSYNC2 Protocol and Replication Backlog
Full handshake (PING→AUTH→REPLCONF→PSYNC), full vs partial resync, replication offset, repl_backlog ring buffer sizing, replid and replid2
Ch17
Replication Lag, Consistency and Data Loss Scenarios
Four async replication data loss scenarios, min-replicas protection, WAIT command semantics, backlog overflow full resync, lag monitoring
Ch18
Sentinel: Raft Leader Election and Failover State Machine
Three timers, sdown vs odown, Raft simplified in Sentinel, nine-step failover state machine, config epoch, client connection switching
Ch19
Redis Cluster: 16384 Slots and Gossip Protocol
Consistent hashing vs hash slots rationale, why 16384, MOVED vs ASK redirect difference, Gossip message structure, cluster_node state fields, clusterCron timer
Ch20
Cluster Failover, Scaling and Data Migration
pfail/fail determination, replica election algorithm, manual vs auto failover, online scale-out/in runbook, MIGRATE during reshard, ASK redirect handling
Ch21
Source Code Setup: Build, Debug and File Map
Source directory layout, GDB breakpoint tracing a SET command, core data structure relationships, main() to aeMain() startup chain
Ch22
Network Layer Source: ae Event Loop and I/O Multiplexing
aeEventLoop struct, file/time event lists, epoll/kqueue/select abstraction, aeProcessEvents main loop, multi-thread I/O thread coordination
Ch23
Cluster Gossip Source: Node State Machine and Message Propagation
clusterCron execution, PING message construction (random 1/10 nodes), clusterProcessGossipSection, pfail→fail determination source, clusterSendUpdate slot propagation
Ch24
Memory Allocation and jemalloc: Defragmentation Internals
zmalloc wrapper, jemalloc arena/bin/chunk layers, fragmentation ratio formula, activedefrag internals, OBJECT subcommands
Ch25
Expiry and Eviction: 8 Policies Deep Dive
Lazy delete + periodic sampling dual mechanism, expires dict design, 8 maxmemory-policy comparison, LRU approximation (random sample pool), LFU Morris counter decay
Ch26
Multi-Thread I/O: Redis 6+ Architecture Evolution
Why command execution stays single-thread, I/O threads read/write only, io-threads config and gains, main/IO thread coordination lock, vs KeyDB/Dragonfly full multi-thread
Ch27
MULTI/EXEC, Lua and Function
QUEUED command queue, WATCH optimistic CAS, MULTI/EXEC atomicity boundary (no rollback), Lua atomicity guarantee, EVALSHA cache, Redis 7 Function improvements over Lua
Ch28
Pub/Sub and Sharded Pub/Sub
SUBSCRIBE/PUBLISH/PSUBSCRIBE dict internals, no-persistence tradeoff, Cluster full-broadcast bottleneck, Redis 7 Sharded Pub/Sub slot routing
Ch29
Redis Modules: RedisSearch, RedisJSON and RedisTimeSeries
Module API extension, RedisSearch full-text and vector KNN search, RedisJSON native JSON path query, RedisTimeSeries compressed storage, RedisBloom scalable filter, vs Elasticsearch
Ch30
Key Design and Data Modeling: Avoiding All Common Pitfalls
Naming conventions, serialization comparison (JSON vs Protobuf vs MessagePack), hot key causes and sharding, big key definition and prevention, TTL design principles
Ch31
Java Clients: Jedis, Lettuce and Redisson
Jedis pool (GenericObjectPool internals), Lettuce Netty async reactive model, Redisson distributed objects (RLock/RMap/RQueue), performance comparison, Spring Data Redis integration
Ch32
Multi-Language Clients: redis-py, ioredis and go-redis
redis-py connection pool and async aioredis, ioredis cluster auto-redirect and Pipeline, go-redis v9 generic API and Hook interceptors, per-language gotchas
Ch33
RESP Protocol, Pipeline and Connection Pool Tuning
RESP2 vs RESP3 (push messages, rich types), Pipeline RTT optimization, Pipeline vs MULTI, pool parameter tuning, connection leak detection
Ch34
Cache Penetration, Breakdown and Avalanche
Root causes and simulations, Bloom filter for penetration (false positive rate), mutex vs logical expiry for breakdown, random TTL for avalanche, decision flowchart
Ch35
Distributed Locks: From SETNX to Redisson
SET NX PX atomicity, lock timeout vs business timeout conflict, Redisson WatchDog auto-renewal, Redlock algorithm and Martin Kleppmann's critique, when Redlock is overkill
Ch36
10 Real Use Cases: Leaderboards to Real-Time Rate Limiting
Leaderboard pagination, sign-in Bitmap, UV HyperLogLog, nearby GEO, flash sale atomic Lua, social graph SADD/SINTER, message queue List vs Stream, session sharing, token bucket Lua, real-time counter sliding window
Ch37
Pipeline, Batch Operations and Performance Optimization
Pipeline RTT reduction, bulk write best practices, SCAN vs KEYS safety, Cluster Pipeline limitations (HashTag solution), redis-benchmark methodology
Ch38
Slow Query and Big Key Analysis
slowlog config, SLOWLOG GET interpretation, --bigkeys/--hotkeys internals, OBJECT subcommands, memory analysis tools, big key splitting in practice
Ch39
Monitoring: Prometheus + Grafana + Alert Rules
INFO section key metrics, redis_exporter config, Grafana dashboard, five golden signal alert rules (memory/connections/latency/hit rate/replication lag)
Ch40
Production Configuration and Security Hardening
bind/requirepass/ACL user system, rename-command for dangerous commands, mTLS, maxmemory calculation, kernel params, 25-item production security checklist
Ch41
Redis 7: Function, Sharded Pub/Sub and listpack
Function replacing Lua (persistence + library management), Sharded Pub/Sub slot routing, listpack replacing ziplist everywhere, ACL improvements, Multi-Part AOF, new LMPOP/ZMPOP commands
Ch42
Redis in Kubernetes: StatefulSet and Persistent Storage
Why StatefulSet (stable network identity + PVC), Redis Operator comparison, PVC dynamic provisioning, ConfigMap management, K8s Service and Cluster routing, resource limits and OOM prevention
Ch43
Production Incident Post-Mortems: 10 Real Cases
10 incidents: bigkey blocks main thread, repl backlog overflow full resync, hot key DB penetration, Lua deadlock, cluster split-brain double write, fragmentation OOM, KEYS block, connection exhaustion, AOF rewrite disk full, K8s Deployment data loss

💬 Comments