Hash vs. Individual Keys

Difficulty: Intermediate

Question

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, ...)?

Answer

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.

Where the memory saving comes from

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:

  • a hash table entry in the global keyspace dictionary
  • a redisObject header plus the SDS key string, which repeats the user:42: prefix every time
  • an entry in the expires dictionary if you set a TTL on each

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

Hash vs individual keys at a glance

AspectOne Hash per objectOne key per field
MemoryCompact listpack while smallFull per-key overhead for every field
Reading the whole objectOne HGETALL or HMGETMGET of many keys
TTLOne EXPIRE for the object; per-field HEXPIRE since Redis 7.4Per key, always supported
Cluster placementAll fields on one slot automaticallyNeeds hash tags like {user:42} to co-locate
Atomic partial updateHSET user:42 city PuneSET user:42:city Pune
Nested dataFlat field/value strings onlySame, but each key can be any type

When individual keys are still the right choice

  • Different expiry per field on older Redis. Before 7.4 you could not expire a single hash field. If 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.
  • A field needs a richer type. A hash field is just a string. If one attribute is a list or sorted set (recent orders, a leaderboard score history), give it its own key.
  • Very hot single fields. Huge hashes accessed by many clients can become a big key; HGETALL on a 100,000-field hash is O(N) and blocks the server.

Common mistakes

  • Storing the whole object as a JSON string with 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.
  • Raising hash-max-listpack-entries to thousands. Listpack lookups are linear, so very large listpacks trade CPU for memory.
  • Assuming the hash keeps types. HINCRBY works on numeric strings, but everything comes back as a string to the client.

How it shows up in real systems

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.

What Redis stores for one user object. Three separate keys repeat the per-key overhead three times; a small hash pays it once.
Hash user:42Keys user:42:*
Keyspace entries13 (one per field)
Encoding when smalllistpack, one byte array3 separate string objects
Key prefix storedOnceRepeated in every key
TTL entries1 for the whole objectUp to 3

Code examples

Storing and inspecting a user profile hash

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

Per-field expiry on Redis 7.4+

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

Key points

  • Every top-level key has fixed overhead; a hash pays it once for many fields
  • Small hashes use listpack (ziplist before Redis 7.0) until hash-max-listpack-entries (128) or hash-max-listpack-value (64 bytes) is exceeded
  • Per-field TTL exists only from Redis 7.4 via HEXPIRE/HTTL/HPERSIST
  • Hash fields are flat strings; use separate keys for nested lists or sets
  • HGETALL on a very large hash is O(N) and can block the server

Common follow-up questions

  • What happens internally when a hash grows past hash-max-listpack-entries?
  • How would you store 50 million small objects to minimise memory?
  • Hash vs storing JSON with SET vs the JSON data type: when would you pick each?
  • How do you keep all keys of one user on the same Redis Cluster slot?

Interview tips

  • Name the actual encoding (listpack, formerly ziplist) and its two config thresholds; it shows you know why the saving exists
  • Mention the one real drawback of hashes (field-level TTL) and that Redis 7.4 fixed it
  • Contrast with JSON strings: hashes allow atomic single-field updates without read-modify-write

Concepts covered

Hash, Listpack encoding, Per-key overhead, Data modelling, Field-level TTL

More Redis & Caching questions