Skip to content
Navigation

Type at least two characters. Search covers page titles, headings, tags and database names.

↑ ↓ to navigateEnter to openEsc to close0 pages
Redis and Valkeybeginner

Redis Data Structures

The core types, their complexity and memory behaviour, and a key naming scheme that stays manageable at scale.

3 min readBeginnerUpdated Edit this page

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.

TypeTypical commandsComplexityUse for
StringGET, SET, INCRO(1)Cached values, counters, flags
HashHGET, HSET, HDELO(1) per fieldObjects with independently updated fields
ListLPUSH, RPOP, LRANGEO(1) at ends, O(N) in middleSimple queues, recent-items lists
SetSADD, SISMEMBERO(1)Membership, deduplication
Sorted setZADD, ZRANGE, ZRANGEBYSCOREO(log N)Leaderboards, priority queues, time indexes
StreamXADD, XREADGROUPO(1) appendEvent logs with consumer groups
BitmapSETBIT, BITCOUNTO(1) / O(N)Dense boolean flags per id
HyperLogLogPFADD, PFCOUNTO(1)Approximate cardinality in ~12 KB

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 512

Exceeding 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.7

Rules that pay off:

  • Use a consistent separator and order. Colons are conventional; consistency is what lets you reason about a SCAN MATCH pattern later.
  • Include the entity type. 1001 alone tells you nothing when debugging.
  • Prefix by application or environment when instances are shared, so one team's FLUSHDB is 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.