Redis Hashes

A Redis Hash stores a collection of field-value pairs under one key. Instead of creating a separate key for every property of an object, you group all properties together in a single Hash. This keeps related data together and reduces key clutter.

The Contact Card Analogy

A business contact card has one name on the front and several details inside — phone, email, company, city. A Redis Hash works exactly the same way. One key (the card's name) holds many field-value pairs (the card's details).

Without Hash (messy — one key per field):
  user:1001:name    → "Arjun Mehta"
  user:1001:email   → "arjun@email.com"
  user:1001:age     → "28"
  user:1001:city    → "Bengaluru"

With Hash (clean — all fields under one key):
  Key: user:1001
  ┌─────────────────────────────────┐
  │  name   →  "Arjun Mehta"        │
  │  email  →  "arjun@email.com"    │
  │  age    →  "28"                 │
  │  city   →  "Bengaluru"          │
  └─────────────────────────────────┘

Creating a Hash with HSET

127.0.0.1:6379> HSET user:1001 name "Arjun Mehta" email "arjun@email.com" age "28" city "Bengaluru"
(integer) 4    ← 4 new fields created

Reading from a Hash

HGET – Read One Field

127.0.0.1:6379> HGET user:1001 name
"Arjun Mehta"

HMGET – Read Several Fields at Once

127.0.0.1:6379> HMGET user:1001 name city
1) "Arjun Mehta"
2) "Bengaluru"

HGETALL – Read Every Field and Value

127.0.0.1:6379> HGETALL user:1001
1) "name"
2) "Arjun Mehta"
3) "email"
4) "arjun@email.com"
5) "age"
6) "28"
7) "city"
8) "Bengaluru"

Updating and Deleting Fields

Update a Field

127.0.0.1:6379> HSET user:1001 city "Mumbai"
(integer) 0    ← 0 means field existed; 1 means new field created
127.0.0.1:6379> HGET user:1001 city
"Mumbai"

Delete a Field

127.0.0.1:6379> HDEL user:1001 age
(integer) 1
127.0.0.1:6379> HGET user:1001 age
(nil)

Check if a Field Exists

127.0.0.1:6379> HEXISTS user:1001 email
(integer) 1    ← exists

127.0.0.1:6379> HEXISTS user:1001 phone
(integer) 0    ← does not exist

Counting and Listing Fields

127.0.0.1:6379> HLEN user:1001
(integer) 3    ← name, email, city (age was deleted)

127.0.0.1:6379> HKEYS user:1001
1) "name"
2) "email"
3) "city"

127.0.0.1:6379> HVALS user:1001
1) "Arjun Mehta"
2) "arjun@email.com"
3) "Mumbai"

Numeric Fields in Hashes

Hash field values that contain numbers work with HINCRBY. This lets you update individual counters inside an object without loading and rewriting the entire Hash.

127.0.0.1:6379> HSET product:55 name "Wireless Mouse" stock "200" price "499"
(integer) 3

127.0.0.1:6379> HINCRBY product:55 stock -5
(integer) 195    ← someone bought 5 units

127.0.0.1:6379> HGETALL product:55
1) "name"
2) "Wireless Mouse"
3) "stock"
4) "195"
5) "price"
6) "499"

Comparing Hash vs Multiple Strings

Scenario: Store and retrieve a user profile.

Multiple String approach:
  SET user:1001:name  "Arjun"     (3 round trips to write)
  SET user:1001:email "..."
  SET user:1001:city  "Mumbai"
  GET user:1001:name              (3 round trips to read)
  GET user:1001:email
  GET user:1001:city

Hash approach:
  HSET user:1001 name "Arjun" email "..." city "Mumbai"   (1 round trip)
  HGETALL user:1001                                        (1 round trip)

  Hash: 2 round trips total
  Strings: 6 round trips total
  Hash is 3x more efficient for this pattern.

Key Points

  • A Redis Hash stores many field-value pairs under one key — ideal for object-like data.
  • HSET creates or updates fields; HGET and HGETALL read them; HDEL removes a specific field.
  • HEXISTS checks whether a field is present without fetching its value.
  • HINCRBY increments a numeric field atomically — no need to read-modify-write manually.
  • Using a Hash instead of separate String keys reduces round trips and keeps related data grouped.

Leave a Comment