Redis设计与实现 (黄健宏)(Z-Library) (1)
sql
系统而全面地描述了 Redis 内部运行机制 图示丰富,描述清晰,并给出大量参考信息,是NoSQL数据库开发人员案头必备 包括大部分Redis单机特征,以及所有多机特性
167
Views
0
Downloads
0.00
Total Donations
AI Guide
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# Redis设计与实现 — Reading Guide
## 【One-Line Pitch】
A systematic, source-code-level walkthrough of Redis's internal architecture—covering data structures, object system, database engine, persistence, and all multi-machine features—for backend engineers and NoSQL practitioners who want to understand not just *how* to use Redis but *why* it works the way it does.
## 【Book Arc】
- **Opening (~0%–3%)**: Author's origin story—building a "mutual follow" feature with SQL joins led him to Redis's native set operations. Sets up the book's mission: no comprehensive Chinese/English resource on Redis internals existed, so he annotated the source code himself, starting with Redis 2.6 and rewriting for 3.0.
- **Early (~3%–13%)**: Foundational data structures—linked lists (doubly-linked, acyclic, type-specific functions), dictionaries (MurmurHash2, index calculation via sizemask, and the crucial *progressive rehash* mechanism that spreads O(N) work across operations to avoid server stalls), and skiplists (O(log N) average lookup, used only for sorted sets and cluster internals).
- **Early (~13%–23%)**: Memory-optimized structures—integer sets with type *upgrade* logic, and compressed lists (ziplists) with their byte-level layout (zlbytes, zltail, zllen) and the O(N²) worst-case *cascade update* problem. Introduces the object system: every key/value is a `redisObject` with type, encoding, and pointer attributes.
- **Early (~23%–32%)**: The polymorphic command dispatch system—type checking via `redisObject.type` (e.g., LLEN rejects non-list keys with WRONGTYPE), then encoding-based dispatch (ziplist vs. linkedlist implementations). Covers the LRU idle-time tracking and its role in maxmemory eviction policies.
- **Middle (~32%–48%)**: Database internals—the `redisDb` structure with `dict` (key space) and `expires` (TTL) dictionaries; the dual deletion strategy (lazy `expireIfNeeded` on access + periodic `activeExpireCycle` with random sampling); master-slave expiration coordination; and RDB persistence (SAVE blocking vs. BGSAVE non-blocking, `dirty` counter + `lastsave` timestamp driving automatic saves via `serverCron`).
- **Middle (~48%–52%+)**: AOF persistence—command append to `aof_buf` in protocol format, file write and sync steps, and (per chapter summaries) rewrite mechanisms that exclude expired keys.
## 【Key Takeaways】
- **Progressive rehash prevents latency spikes** (Early): Instead of one massive rehash, Redis migrates key-value pairs incrementally across normal operations using a `rehashidx` counter—new writes go to ht[1] while reads check both tables, ensuring ht[0] shrinks to empty without blocking the server.
- **Encoding flexibility is the core optimization strategy** (Early): Every object type supports multiple encodings (e.g., list as ziplist or linkedlist; hash as ziplist or hashtable). Redis switches based on element count/size to balance memory efficiency (compact contiguous storage) against operational complexity—checkable via `OBJECT ENCODING`.
- **Type checking + encoding dispatch = polymorphic commands** (Early): Commands like LLEN first verify the key's `type` attribute (rejecting wrong types with WRONGTYPE), then select the correct implementation based on `encoding`—a two-level polymorphism that lets one command serve multiple underlying structures.
- **Expired-key deletion is a hybrid of lazy and active strategies** (Middle): `expireIfNeeded` filters expired keys on every access, while `activeExpireCycle` periodically samples random keys from the `expires` dictionary across databases—balancing CPU usage against memory waste without a dedicated timer thread.
- **Master-slave expiration is centralized for consistency** (Middle): Masters delete expired keys and propagate DEL commands to replicas; slaves never delete on their own, even if they detect expiration—ensuring data consistency across the replication topology.
- **RDB persistence uses a dirty-counter + timestamp trigger** (Middle): The `serverCron` function (every 100ms) checks save conditions—if `dirty` (modifications since last save) exceeds a threshold within a time window, it fires BGSAVE via a child process, avoiding the blocking behavior of the synchronous SAVE command.
- **AOF persistence records every write in protocol format** (Middle): After executing a write command, Redis appends the command in RESP protocol to `aof_buf`, then handles file write and sync—enabling exact reconstruction of the dataset through command replay.
## 【Reading Tips】
- **Skim the opening chapters if you know basic data structures**: The linked-list and dictionary chapters are standard CS material; focus instead on Redis-specific twists like progressive rehash and the exact conditions for encoding upgrades.
- **Deep-read the object system chapter (Ch. 8)**: This is the conceptual heart—understanding `redisObject` (type, encoding, ptr, lru) explains why commands behave polymorphically and how memory optimization actually works. Use `OBJECT ENCODING` on a live Redis to verify.
- **Pay attention to the "重点回顾" (key review) sections**: Each chapter ends with a bulleted summary—these are excellent for revision and for identifying which details matter most for interviews or production debugging.
- **Treat the pseudocode seriously**: The book uses Python pseudocode for complex algorithms (like `activeExpireCycle`); trace through these carefully—they reveal the actual control flow better than prose descriptions.
- **Note the version context**: The book targets Redis 2.9/3.0 (2014). Core single-machine mechanics remain valid, but verify multi-machine features (Sentinel, clustering) against current documentation before relying on them in production.
## 【Coverage Limits】
This guide covers the book's single-machine internals (data structures, object system, database engine, expiration, RDB/AOF persistence) in detail. The excerpts do not cover the multi-machine chapters (replication, Sentinel, clustering), Lua scripting, transactions, or the event model—consult the full book for those.
##
Passage locations
Excerpt 1
代码边写的,如果有足够时间让我先完整地注释一遍Redis的源代码,然后再进行写作的话,那么书本在内容方面应该会更为全面。 ·又比如说,第一版只介绍了Redis的内部机制和单机特性,但并没有介绍Redis多机特性,而我认为只有将关于多机特性的介绍也包含进来,这本《Redis设计与实现》才算是真正的完成了。 就在我考...
View in text
Excerpt 2
类型比整数集合现有所有元素的类型都要长时,整数集合需要先进行升级(upgrade),然后才能将新元素添加到整数集合里面。 升级整数集合并添加新元素共分为三步进行: 1)根据新元素的类型,扩展整数集合底层数组的空间大小,并为新元素分配空间。 2)将底层数组现有的所有元素都转换成与新元素相同的类型,并将类型转换后的元...
View in text
Excerpt 3
上,我们可以将DEL、EXPIRE、TYPE等命令也称为多态命令,因为无论输入的键是什么类型,这些命令都可以正确地执行。 DEL、EXPIRE等命令和LLEN等命令的区别在于,前者是基于类型的多态——一个命令可以同时用于处理多种不同类型的键,而后者是基于编码的多态——一个命令可以同时用于处理多种不同编码。 图8-...
View in text
Excerpt 4
的情况下执行,所以Redis允许用户通过设置服务器配置的save选项,让服务器每隔一段时间自动执行一次BGSAVE命令。 用户可以通过save选项设置多个保存条件,但只要其中任意一个条件被满足,服务器就会执行BGSAVE命令。 举个例子,如果我们向服务器提供以下配置: save 900 1 save 300 10...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay