Redis Data Types Overview and Implementation Details
This article provides a comprehensive overview of Redis data types—including strings, hashes, lists, sets, sorted sets, streams, hyperloglog, geospatial indexes, bitmaps, and bitfields—detailing their structures, encoding mechanisms, application scenarios, and underlying implementation details with code examples.
Redis is an open‑source, high‑performance key‑value database that supports a variety of data types. This article introduces the ten core Redis data structures and explains their internal representations, typical use cases, and how Redis chooses the most efficient encoding based on size and content.
String
The basic string type can store up to 512 MB. Internally it uses the SDS (simple dynamic string) structure, which can be encoded as embstr , raw , or int depending on the length of the value.
127.0.0.1:6379> object encoding str
"embstr"
127.0.0.1:6379> set str 1234567890123456789012345678901234567890abcd
OK
127.0.0.1:6379> object encoding str
"raw"Hash
A hash stores a map of fields to values and can hold up to 2^32‑1 fields. Redis uses three possible encodings: ziplist (or listpack in newer versions), and hashtable . The switch is driven by hash-max-ziplist-entries and hash-max-ziplist-value thresholds.
127.0.0.1:6379> hset h1 id 1234567890...
1
127.0.0.1:6379> object encoding h1
"ziplist"List
Lists are ordered collections of strings. Before Redis 3.2 they were implemented with linkedlist or ziplist ; from 3.2 onward they use quicklist , a hybrid of linked lists and ziplists (later replaced by listpack ).
list-max-ziplist-size -2
# –2 means each listpack is at most 8 KBSet
Sets store unique, unordered strings. Small integer‑only sets use the intset encoding; larger or mixed‑type sets use a hashtable . The switch occurs when the number of elements exceeds set-max-intset-entries (default 512) or when an element’s size exceeds set-max-intset-value .
127.0.0.1:6379> sadd myset 123
1
127.0.0.1:6379> object encoding myset
"intset"ZSet (Sorted Set)
Sorted sets associate each member with a floating‑point score and keep elements ordered by score. For small collections Redis uses ziplist (or listpack after 7.0); otherwise it falls back to a skiplist paired with a hash table.
typedef struct zskiplistNode {
sds ele;
double score;
struct zskiplistNode *backward;
struct zskiplistLevel {
struct zskiplistNode *forward;
unsigned long span;
} level[];
} zskiplistNode;Stream
Streams implement a log‑like data structure using a radix tree ( rax ) for IDs and listpack for the entry payloads, enabling efficient insertion, range queries, and consumer‑group processing.
typedef struct raxNode {
uint32_t iskey:1;
uint32_t isnull:1;
uint32_t iscompr:1;
uint32_t size:29;
unsigned char data[];
} raxNode;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.
PFADD goodA "1"
PFADD goodA "2"
PFCOUNT goodA
3Geospatial (GEO)
Geospatial indexes are built on top of sorted sets, encoding latitude/longitude into a 52‑bit geohash used as the score. Commands such as GEOADD , GEOPOS , and GEORADIUS enable location‑based queries.
Bitmap
Bitmaps treat a string as an array of bits, allowing SETBIT , GETBIT , and BITCOUNT operations. They are useful for tracking flags, daily active users, or implementing Bloom filters.
SETBIT user:login:2024:100 1 1
BITCOUNT user:login:2024:100
3Bitfield
Bitfield extends the string type to manipulate arbitrary‑width signed or unsigned integers at any offset, supporting atomic GET , SET , and INCRBY operations.
BITFIELD user:1:info SET u8 #0 1 SET u8 #1 25 SET u16 #2 165 SET u16 #3 50000Overall, Redis dynamically selects the most space‑efficient encoding for each data type based on element count and size, providing a balance between memory usage and operation speed.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
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.