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
Hash vs JSON string
| Hash | JSON in a string | |
|---|---|---|
| Read one field | HGET, O(1), returns just that field | GET the whole value, parse it |
| Update one field | HSET/HINCRBY, atomic | Read-modify-write, racy without WATCH or Lua |
| Nested data | No (values are flat strings) | Yes |
| Memory for small objects | Very 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:
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 bytesA 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.
Listpack operations are O(N) in the number of fields, because Redis
scans the block linearly. That's why the threshold is low: up to a
few hundred small entries, a linear scan over contiguous memory is
as fast as a hash lookup and far more cache-friendly. Past it, Redis
converts the key to a real hash table (OBJECT ENCODING becomes
hashtable) and never converts back.
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-wayHGETALL on a hash with a million fields is O(N): it blocks Redis and
builds a huge reply. Big hashes need HSCAN, or a redesign. A hash
per object is good; one hash holding every object is not.
Lab
HGETALL app:product:1001(fromseed.py).- Decrement stock atomically with
HINCRBY ... stock -1and read it back. - Run the conversion experiment above and compare
MEMORY USAGEbefore and after. - 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.