Understanding Redis Ziplist: Memory Layout, Fields, and Encoding
This article explains Redis's ziplist—a compact list implementation used for small integers and short strings—detailing its memory layout, header fields, entry encoding, previous_entry handling, and the cascade update mechanism that occurs when node sizes change.
Redis uses a special data structure called ziplist, also known as a compressed list, to store small integers or short strings efficiently in a contiguous memory block.
The ziplist header consists of four fields: zlbytes (total bytes), zltail (offset of the tail entry), zllen (number of entries), and a variable‑length entry area ending with the zlend marker (0xFF).
Each entry stores three parts: previous_entry , which records the length of the preceding node (using 1 byte for lengths ≤254 and 5 bytes with a 0xFE prefix for larger lengths); encoding , a byte (or few bytes) whose leading two bits indicate the data type (string or integer) and the remaining bits encode the length; and p , the actual payload.
The encoding scheme defines several patterns, for example 00xxxxxx for strings up to 63 bytes, 01pppppp|qqqqqqqq for strings up to 16383 bytes, and various 11xxxxxx patterns for signed integers of different sizes.
When an entry’s length grows beyond 254 bytes, previous_entry expands from 1 to 5 bytes, which can trigger a chain reaction—called a cascade update—where subsequent entries must also adjust their previous_entry fields. This effect is rare because ziplist is intended for small data.
Overall, ziplist provides a memory‑compact representation suitable for lists with few elements, small integers, or short strings, and its design illustrates Redis’s careful balance between performance and memory efficiency.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
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.