<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Redis on Zhuifeng Notes</title><link>https://4f0a9a3b.wangpeng.pages.dev/en/tech/redis/</link><description>Recent content in Redis on Zhuifeng Notes</description><generator>Hugo</generator><language>en</language><lastBuildDate>Tue, 25 Aug 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://4f0a9a3b.wangpeng.pages.dev/en/tech/redis/index.xml" rel="self" type="application/rss+xml"/><item><title>Redis Data Structures and Underlying Implementation</title><link>https://4f0a9a3b.wangpeng.pages.dev/en/tech/redis/data-structures/</link><pubDate>Tue, 25 Aug 2026 00:00:00 +0000</pubDate><guid>https://4f0a9a3b.wangpeng.pages.dev/en/tech/redis/data-structures/</guid><description>&lt;p&gt;Redis exposes five common types to users, but underneath there are multiple encodings (encoding). Automatically switching encodings based on data size is the key to balancing memory and performance.&lt;/p&gt;&#10;&lt;h2 id="core-underlying-structures"&gt;Core Underlying Structures&#10;&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;SDS (Simple Dynamic String)&lt;/strong&gt;: records length and pre-allocates space compared to C strings, avoiding buffer overflow and supporting binary safety.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;ziplist / listpack&lt;/strong&gt;: compact contiguous-memory lists that save space and suit small data volumes.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;linkedlist&lt;/strong&gt;: a doubly-linked list that replaces ziplist when there are many elements.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;dict (dictionary)&lt;/strong&gt;: a hash table using chaining for collisions, with progressive rehash to avoid blocking.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;skiplist (skip list)&lt;/strong&gt;: a multi-level ordered linked list, combined with dict to implement zset for efficient range queries.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="encodings-and-conversions-per-type"&gt;Encodings and Conversions per Type&#10;&lt;/h2&gt;&#10;&lt;div class="td-table-scroll td-table-scroll--static"&gt;&#10;&lt;table&gt;&#10; &lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th scope="col"&gt;Type&lt;/th&gt;&#10; &lt;th scope="col"&gt;Small-data encoding&lt;/th&gt;&#10; &lt;th scope="col"&gt;Large-data encoding&lt;/th&gt;&#10; &lt;th scope="col"&gt;Conversion threshold (illustrative)&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;string&lt;/td&gt;&#10; &lt;td&gt;int / embstr&lt;/td&gt;&#10; &lt;td&gt;raw&lt;/td&gt;&#10; &lt;td&gt;length &amp;gt; 44 bytes becomes raw&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;hash&lt;/td&gt;&#10; &lt;td&gt;ziplist/listpack&lt;/td&gt;&#10; &lt;td&gt;hashtable&lt;/td&gt;&#10; &lt;td&gt;element count or single value exceeds threshold&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;list&lt;/td&gt;&#10; &lt;td&gt;quicklist (ziplist segments)&lt;/td&gt;&#10; &lt;td&gt;quicklist&lt;/td&gt;&#10; &lt;td&gt;—&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;set&lt;/td&gt;&#10; &lt;td&gt;intset&lt;/td&gt;&#10; &lt;td&gt;hashtable&lt;/td&gt;&#10; &lt;td&gt;contains non-integers or too many elements&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td&gt;zset&lt;/td&gt;&#10; &lt;td&gt;ziplist&lt;/td&gt;&#10; &lt;td&gt;skiplist+dict&lt;/td&gt;&#10; &lt;td&gt;element count or value length exceeds threshold&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;/div&gt;&#10;&#10;&lt;p&gt;Taking &lt;code&gt;zset&lt;/code&gt; as an example, small data uses a compact ziplist (member and score adjacent); when the element count or a single member length exceeds the threshold it converts to &lt;code&gt;skiplist + dict&lt;/code&gt;, ensuring ordered traversal and member lookup are O(1)/O(log n).&lt;/p&gt;</description></item><item><title>Redis Cache Design</title><link>https://4f0a9a3b.wangpeng.pages.dev/en/tech/redis/cache-design/</link><pubDate>Tue, 25 Aug 2026 00:00:00 +0000</pubDate><guid>https://4f0a9a3b.wangpeng.pages.dev/en/tech/redis/cache-design/</guid><description>&lt;p&gt;Introducing a cache dramatically reduces database pressure, but poor design introduces the classic problems of penetration, breakdown and avalanche. We break down the causes and countermeasures below.&lt;/p&gt;&#10;&lt;h2 id="the-three-classic-problems"&gt;The Three Classic Problems&#10;&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Cache penetration&lt;/strong&gt;: querying data that does not exist, so neither cache nor DB returns it and every request hits the DB. Mitigate with caching empty values (short TTL) or a Bloom filter to block illegal keys.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Cache breakdown&lt;/strong&gt;: the moment a hot key expires, many concurrent requests all fall back to the DB at once. Mitigate with a mutex (only one thread rebuilds), logical expiration (async refresh), or never-expire hot keys.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Cache avalanche&lt;/strong&gt;: a large number of keys expire simultaneously, or Redis becomes entirely unavailable, overwhelming the DB. Mitigate with random jitter on expiration times, multi-level caching, and Redis high availability (Sentinel/Cluster).&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="cache-aside-and-consistency"&gt;Cache-Aside and Consistency&#10;&lt;/h2&gt;&#10;&lt;p&gt;The most common pattern is &lt;strong&gt;Cache-Aside (旁路缓存)&lt;/strong&gt;:&lt;/p&gt;</description></item></channel></rss>