Redis Data Types and Their Internal Implementations
This article provides a comprehensive overview of Redis's ten data types—including strings, hashes, lists, sets, sorted sets, streams, hyperloglog, geospatial, bitmap, and bitfield—detailing their structures, encoding mechanisms, application scenarios, and underlying implementation details with code examples.
Redis is an open‑source, high‑performance key‑value store that supports a variety of data types. This guide introduces the ten core Redis data types and explains their internal representations, typical use‑cases, and how Redis chooses the most efficient encoding.
String
The basic string type can store up to 512 MB. Internally it uses SDS (simple dynamic string) with three encodings: embstr for short strings (≤44 bytes), raw for larger strings, and int for integer values. The article shows how Redis decides between embstr and raw and provides a CLI example of object encoding.
Hash
Hashes store field‑value pairs similar to a map. Redis can encode a hash as ziplist (or listpack in newer versions) for small hashes, or as a hashtable when the number of entries or field/value size exceeds configurable thresholds ( hash-max-ziplist-entries, hash-max-ziplist-value). The article includes the C struct definitions for each encoding and explains the conversion process.
List
Lists are ordered collections of strings. Their underlying implementation evolved from linkedlist + ziplist (pre‑Redis 3.2) to quicklist (a hybrid of linked list nodes containing ziplist or listpack) and finally to a pure listpack in Redis 7.0+. Configuration parameters such as list-max-ziplist-size and list-compress-depth control memory usage and compression.
Set
Sets are unordered collections of unique strings. Small sets are stored as intset (compact integer array) while larger or non‑integer sets use a hashtable. The article details the thresholds ( set-max-intset-entries) and shows how Redis switches encodings when integer and size limits are exceeded.
Sorted Set (ZSet)
ZSets associate each member with a floating‑point score and maintain order. For small collections Redis uses ziplist (or listpack in newer versions); otherwise it falls back to a skiplist paired with a hash for O(log N) operations. The article explains the conditions ( zset_max_listpack_entries, zset_max_listpack_value) that trigger the switch and describes the skiplist structure.
Stream
Streams implement an append‑only log using a radix tree (Rax) to store IDs and listpack entries for the actual field‑value pairs. The guide walks through Rax node fields ( iskey, isnull, iscompr, size, data) and illustrates insertion scenarios with diagrams.
HyperLogLog
HyperLogLog provides cardinality estimation with a fixed 12 KB memory footprint and ~0.81 % error. It is implemented as a special string type using the SDS structure. The article shows basic PFADD and PFCOUNT commands.
Geospatial (GEO)
Geospatial data is stored in a sorted set where the score is a geohash of latitude/longitude. The article lists the main GEO commands ( GEOADD, GEOPOS, GEODIST, GEORADIUS, GEORADIUSBYMEMBER) and typical use‑cases such as location‑based queries.
Bitmap
Bitmaps are strings interpreted as arrays of bits, allowing operations like SETBIT, GETBIT, and BITCOUNT. They are useful for tracking binary states (e.g., daily user activity) and can be combined to build Bloom filters.
Bitfield
Bitfield extends the bitmap concept by allowing arbitrary‑width signed or unsigned integers (1‑63 bits) to be stored and atomically manipulated ( SET, GET, INCRBY). The article compares bitmap and bitfield capabilities and provides an example of encoding user profile attributes in a single key.
Overall, the article equips developers with a deep understanding of how Redis optimizes each data type for memory efficiency and performance, and how to choose the appropriate type and configuration for specific application scenarios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.
