<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Internals on Zhuifeng Notes</title><link>https://4f0a9a3b.wangpeng.pages.dev/en/tags/internals/</link><description>Recent content in Internals 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/tags/internals/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></channel></rss>