Databases 31 min read

Redis Data Types: Overview, Use Cases, and Internal Implementations

This article provides a comprehensive English guide to Redis’s ten core data types—including strings, hashes, lists, sets, sorted sets, streams, hyperloglog, bitmap, and bitfield—detailing their definitions, typical application scenarios, underlying storage mechanisms, conversion rules, and sample Redis commands with code examples.

Architecture Digest
Architecture Digest
Architecture Digest
Redis Data Types: Overview, Use Cases, and Internal Implementations

String

String is the most basic Redis data type that can store up to 512 MB of any binary data (text, numbers, images, serialized objects). 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.

Typical use cases include caching, counters (using INCR/DECR ), distributed locks ( SETNX ), and rate limiting ( EXPIRE ).

127.0.0.1:6379> SET mykey "hello"
OK
127.0.0.1:6379> OBJECT ENCODING mykey
"embstr"

Hash

Hash stores a collection of field‑value pairs, similar to a map. It can be encoded as ziplist (or listpack in newer versions) for small hashes and as a hashtable for larger ones. Conversion thresholds are controlled by hash-max-ziplist-entries and hash-max-ziplist-value .

Common scenarios are user profiles, shopping carts, and configuration storage.

127.0.0.1:6379> HSET user:1 name "Alice" age 30
2
127.0.0.1:6379> OBJECT ENCODING user:1
"ziplist"

List

List is an ordered collection of strings. Before Redis 3.2 it used a linked list; from 3.2 to 7.0 it used a hybrid quicklist (linked list + ziplist); from 7.0 onward it uses quicklist with listpack instead of ziplist. Lists are ideal for queues, timelines, and history logs.

Example commands:

LPUSH mylist "msg1"
LPUSH mylist "msg2"
LRANGE mylist 0 -1

Set

Set is an unordered collection of unique strings. Internally it can be an intset (for small integer‑only sets) or a hashtable . Conversion occurs when the set exceeds set-max-intset-entries (default 512) or contains non‑integer values.

Typical uses are deduplication, membership testing, and performing union/intersection/difference operations.

SADD myset 123
SADD myset "abc"
OBJECT ENCODING myset
"hashtable"

ZSet (Sorted Set)

ZSet stores unique strings with a floating‑point score that determines ordering. For small collections it uses a ziplist (Redis 7.0) or listpack ; for larger collections it switches to a skiplist paired with a hash table. Thresholds are zset-max-listpack-entries (default 128) and zset-max-listpack-value (default 64 bytes).

Common applications include leaderboards, delayed queues, and ranking systems.

ZADD leaderboard 100 "player1"
ZADD leaderboard 150 "player2"
ZRANGE leaderboard 0 -1 WITHSCORES

Stream

Stream is a log‑like data structure that stores entries identified by a monotonically increasing ID. It is implemented with a radix tree ( rax ) for ID indexing and listpack for the entry payload. Streams support consumer groups, reliable delivery, and range queries.

Typical use cases are message queues, event sourcing, and change logs.

XADD mystream * sensor-id 42 temperature 23.5
XREAD COUNT 10 STREAMS mystream 0-0

HyperLogLog

HyperLogLog is a probabilistic data structure that estimates the cardinality of a set using a fixed 12 KB memory footprint, with an error rate of about 0.81 %.

It is useful for counting unique visitors, active users, or distinct events.

PFADD uv_counter "user1" "user2" "user3"
PFCOUNT uv_counter
3

Bitmap

Bitmap is a special string where each bit can be set or cleared, allowing storage of up to 2³² ‑ 1 bits. It is often used for user activity tracking, Bloom filters, or bitwise indexing.

Example:

SETBIT user:2024:100 1 1   # mark login at second 1
SETBIT user:2024:100 10240 1   # mark login at second 10240
BITCOUNT user:2024:100
3

Bitfield

Bitfield extends the bitmap concept by allowing arbitrary‑width signed or unsigned integers to be stored and atomically incremented within a string. Offsets are counted from the most‑significant bit.

Typical usage is compact storage of multiple attributes (e.g., gender, age, height, weight) in a single key.

BITFIELD user:1:info SET u8 #0 1   # gender (1 = female)
SET u8 #1 25                     # age
SET u16 #2 165                    # height (cm)
SET u16 #3 50000                  # weight (g)
BITFIELD user:1:info GET u8 #0 GET u8 #1 GET u16 #2 GET u16 #3
1
25
165
50000

The article also includes detailed diagrams of internal structures (SDS, ziplist, listpack, quicklist, hash table, skiplist, radix tree) and explains conversion rules, memory trade‑offs, and performance considerations for each Redis data type.

DatabaseRedisData TypesstringhashZsetlistset
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.