Learn Labs
Data Structures

Hashes

Objects as field-value maps, per-field TTLs, and memory savings

A hash maps field names to string values inside one key. It's the natural shape for an object: a product, a user profile, a session.

Commands

HSET app:product:1001 name "Mechanical keyboard" price 89 stock 12set fields (creates the key)HGET app:product:1001 price"89"HMGET app:product:1001 name priceseveral fieldsHGETALL app:product:1001every field and value: O(N)HINCRBY app:product:1001 stock -1atomic per-field counterHINCRBYFLOAT app:product:1001 price 0.99HEXISTS app:product:1001 stock1HDEL app:product:1001 stockremove one fieldHLEN app:product:1001field countHKEYS / HVALSall field names / values: O(N)HSCAN app:product:1001 0iterate a big hash safelyHSETNX app:product:1001 sku "KB-01"set a field only if it's absentHRANDFIELD app:product:1001 2 WITHVALUESrandom fields

Hash vs JSON string

HashJSON in a string
Read one fieldHGET, O(1), returns just that fieldGET the whole value, parse it
Update one fieldHSET/HINCRBY, atomicRead-modify-write, racy without WATCH or Lua
Nested dataNo (values are flat strings)Yes
Memory for small objectsVery compact (listpack)One string, similar

Use a hash when fields change independently, like a stock count or a session's last_seen. Use a JSON string for opaque cache values that are always read and written whole. Use the built-in JSON type when you need nesting and field-level updates (see Specialized Types).

Per-field expiration

Since Redis 7.4, individual fields can have their own TTL:

HSET session:abc user 42 csrf x9f2 otp 123456HEXPIRE session:abc 300 FIELDS 1 otponly the one-time code expires in 5 minutesHTTL session:abc FIELDS 2 otp user300, -1 (user has no TTL)HPERSIST session:abc FIELDS 1 otpremove the field's TTLHGETEX / HSETEXread or write fields and set their TTL in one call (8.0+)

Before this existed, the only option was a whole-key TTL or a separate key per expiring field.

The memory trick

Small hashes use the compact listpack encoding: fields and values in one contiguous block, no per-field pointers or hash table. That can be several times smaller than storing each field as its own key.

CONFIG GET hash-max-listpack-*
1) "hash-max-listpack-entries"  2) "512"   # convert to a hash table above 512 fields...
3) "hash-max-listpack-value"    4) "64"    # ...or when any value exceeds 64 bytes

A classic optimisation for millions of tiny values is bucketing: instead of SET user:1234567:score 42 (one key each), store HSET user:scores:1234 567 42, grouping 1,000 IDs per hash so every hash stays a listpack. Instagram famously cut memory for 300 million photo → user mappings from ~21 GB to ~5 GB this way.

Watch the conversion

HSET app:demo f1 v1
OBJECT ENCODING app:demo                   # "listpack"
MEMORY USAGE app:demo

# add a field with a 100-byte value: one value over 64 bytes is enough
HSET app:demo big "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
OBJECT ENCODING app:demo                   # "hashtable"
HDEL app:demo big
OBJECT ENCODING app:demo                   # still "hashtable": conversion is one-way

Lab

  1. HGETALL app:product:1001 (from seed.py).
  2. Decrement stock atomically with HINCRBY ... stock -1 and read it back.
  3. Run the conversion experiment above and compare MEMORY USAGE before and after.
  4. Store 1,000 values as separate keys and the same 1,000 as fields in one hash. Compare total memory with INFO memory (used_memory) before and after each.

On this page