Redis Data Structures
The core types, their complexity and memory behaviour, and a key naming scheme that stays manageable at scale.
Choosing the right structure is most of Redis modelling. The wrong one turns an O(1) operation into an O(N) one that blocks the server.
Small structures are encoded compactly
Redis stores small collections in packed encodings — listpack for small hashes, lists and sorted
sets; intset for small all-integer sets. These use far less memory but have O(N) access, which is
cheap only while N is small.
hash-max-listpack-entries 128
hash-max-listpack-value 64
zset-max-listpack-entries 128
set-max-intset-entries 512Exceeding a threshold converts the structure permanently to the general encoding, and memory use jumps. This is why a hash with 100 small fields can use a fraction of the memory of one with 200.
> OBJECT ENCODING user:1001
"listpack"Key Naming
Keys are the only index Redis has, so the naming scheme is the schema.
{app}:{entity}:{id}:{attribute}
shop:user:1001:profile
shop:user:1001:sessions
shop:cart:9f3a2c
shop:ratelimit:login:203.0.113.7Rules that pay off:
- Use a consistent separator and order. Colons are conventional; consistency is what lets you
reason about a
SCAN MATCHpattern later. - Include the entity type.
1001alone tells you nothing when debugging. - Prefix by application or environment when instances are shared, so one team's
FLUSHDBis not everyone's incident. - Keep keys short but readable. Key strings are stored in memory for every key; on hundreds of millions of keys the difference is real. Do not compress them into unreadability to save bytes.
- Never embed unbounded values in the key, such as a full URL or a serialised filter.
Hash tags control cluster placement: {shop:user:1001}:profile and {shop:user:1001}:sessions
hash to the same slot, so multi-key operations on them are legal in
Redis Cluster.