Difficulty: Intermediate
You need to cache a user profile with fields like name, email and city. Why would you store it as a single Redis Hash instead of one key per field (user:42:name, user:42:email, ...)?
A Hash keeps all fields of one object under a single key, which saves a lot of memory and keeps related data together. Every top-level Redis key carries fixed overhead (a dictionary entry, a redisObject header, the key string and possibly an expiry entry), while small hashes are stored in a compact listpack encoding that packs fields and values into one contiguous block. You also get atomic multi-field reads and writes (HSET, HMGET, HGETALL) and a single place to set the TTL.
With individual keys, user:42:name, user:42:email and user:42:city are three separate entries in the main keyspace. Each one pays for:
redisObject header plus the SDS key string, which repeats the user:42: prefix every timeA Hash pays that cost once. As long as it stays small it uses the listpack encoding (called ziplist before Redis 7.0), a flat byte array with no per-field pointers. The thresholds are controlled by hash-max-listpack-entries (default 128) and hash-max-listpack-value (default 64 bytes). Cross either one and Redis converts the hash to a real hash table, which is faster for large sizes but uses noticeably more memory. You can check the encoding with OBJECT ENCODING user:42.
This is the idea behind the well-known bucketing trick: instead of millions of tiny keys like media:1155315, store them as fields in hashes such as media:1155 with field 315, keeping each bucket under the listpack limit.
| Aspect | One Hash per object | One key per field |
|---|---|---|
| Memory | Compact listpack while small | Full per-key overhead for every field |
| Reading the whole object | One HGETALL or HMGET | MGET of many keys |
| TTL | One EXPIRE for the object; per-field HEXPIRE since Redis 7.4 | Per key, always supported |
| Cluster placement | All fields on one slot automatically | Needs hash tags like {user:42} to co-locate |
| Atomic partial update | HSET user:42 city Pune | SET user:42:city Pune |
| Nested data | Flat field/value strings only | Same, but each key can be any type |
otp must vanish in 5 minutes but name stays for a day, separate keys were the only option. Redis 7.4 added HEXPIRE, HPEXPIRE, HTTL and HPERSIST for this.HGETALL on a 100,000-field hash is O(N) and blocks the server.SET. You then have to read and rewrite the entire blob to change one field, which also invites lost-update races. A Hash lets you change one field atomically with HSET or HINCRBY.hash-max-listpack-entries to thousands. Listpack lookups are linear, so very large listpacks trade CPU for memory.HINCRBY works on numeric strings, but everything comes back as a string to the client.Session stores, product catalog caches and per-user feature flags are classic Hash use cases. A food delivery app might keep restaurant:881 as a hash with name, rating, is_open and avg_prep_mins, flip is_open with a single HSET, and expire the whole record with one EXPIRE when the menu cache refreshes.
| Hash user:42 | Keys user:42:* | |
|---|---|---|
| Keyspace entries | 1 | 3 (one per field) |
| Encoding when small | listpack, one byte array | 3 separate string objects |
| Key prefix stored | Once | Repeated in every key |
| TTL entries | 1 for the whole object | Up to 3 |
# Store the whole object under one key
HSET user:42 name "Asha" email "asha@example.com" city "Bengaluru"
EXPIRE user:42 3600
# Read some or all fields
HMGET user:42 name city
HGETALL user:42
# Update one field atomically, no read-modify-write
HSET user:42 city "Pune"
# Check how Redis stored it
OBJECT ENCODING user:42
MEMORY USAGE user:42
The small hash uses listpack encoding. Compare MEMORY USAGE against the sum of three separate string keys to see the overhead difference on your own Redis version.
# Keep the profile, but drop the OTP field after 300 seconds
HSET user:42 otp "482913"
HEXPIRE user:42 300 FIELDS 1 otp
# Remaining TTL of that field
HTTL user:42 FIELDS 1 otp
Hash field expiration arrived in Redis 7.4. On older versions you would move the OTP into its own key with SET ... EX 300.
Hash, Listpack encoding, Per-key overhead, Data modelling, Field-level TTL