Redis Hashes
A hash maps fields to values inside one key, a bit like a small dictionary or a row in a table. Hashes are the natural way to store objects: a user with a name, an email and a login count, or a product with a title, price and stock. You can read or change one field without touching the others.
HSET and HGET
HSET key field value [field value ...] sets one or more fields and returns how many fields were new. HGET reads one field, HMGET several, and HGETALL returns every field and value.
HSET user:1001 name "Ada Lovelace" email ada@example.com country UK
HGET user:1001 name
HMGET user:1001 email phone country
HSET user:1001 country GB city London
HGETALL user:1001redis> HSET user:1001 name "Ada Lovelace" email ada@example.com country UK
(integer) 3
redis> HGET user:1001 name
"Ada Lovelace"
redis> HMGET user:1001 email phone country
1) "ada@example.com"
2) (nil)
3) "UK"
redis> HSET user:1001 country GB city London
(integer) 1
redis> HGETALL user:1001
1) "name"
2) "Ada Lovelace"
3) "email"
4) "ada@example.com"
5) "country"
6) "GB"
7) "city"
8) "London"
HGETALL returns a flat array that alternates field, value, field, value. Client libraries turn it into a dictionary or map for you. A missing field reads as (nil), just like a missing key.
Exploring a hash
HSET product:42 title "Ceramic mug" price 8.50 stock 120
HLEN product:42
HKEYS product:42
HVALS product:42
HEXISTS product:42 stock
HEXISTS product:42 color
HSTRLEN product:42 titleredis> HSET product:42 title "Ceramic mug" price 8.50 stock 120
(integer) 3
redis> HLEN product:42
(integer) 3
redis> HKEYS product:42
1) "title"
2) "price"
3) "stock"
redis> HVALS product:42
1) "Ceramic mug"
2) "8.50"
3) "120"
redis> HEXISTS product:42 stock
(integer) 1
redis> HEXISTS product:42 color
(integer) 0
redis> HSTRLEN product:42 title
(integer) 11
Counters inside a hash
HINCRBY and HINCRBYFLOAT work like INCRBY and INCRBYFLOAT, but on a field. Many related counters can live in one hash:
HINCRBY stats:article:7 views 1
HINCRBY stats:article:7 views 1
HINCRBY stats:article:7 likes 1
HINCRBY product:42 stock -3
HINCRBYFLOAT product:42 price 0.25
HGETALL stats:article:7redis> HINCRBY stats:article:7 views 1
(integer) 1
redis> HINCRBY stats:article:7 views 1
(integer) 2
redis> HINCRBY stats:article:7 likes 1
(integer) 1
redis> HINCRBY product:42 stock -3
(integer) -3
redis> HINCRBYFLOAT product:42 price 0.25
"0.25"
redis> HGETALL stats:article:7
1) "views"
2) "2"
3) "likes"
4) "1"
The product:42 key does not exist in this example (each example starts with an empty server), so HINCRBY created it with stock = 0 − 3.
Deleting fields and conditional writes
HDEL removes fields; when the last field goes, the key disappears. HSETNX sets a field only if it does not exist yet:
HSET cart:77 product:1 2 product:5 1
HSETNX cart:77 product:1 10
HSETNX cart:77 product:9 1
HDEL cart:77 product:5 product:404
HGETALL cart:77
HDEL cart:77 product:1 product:9
EXISTS cart:77redis> HSET cart:77 product:1 2 product:5 1
(integer) 2
redis> HSETNX cart:77 product:1 10
(integer) 0
redis> HSETNX cart:77 product:9 1
(integer) 1
redis> HDEL cart:77 product:5 product:404
(integer) 1
redis> HGETALL cart:77
1) "product:1"
2) "2"
3) "product:9"
4) "1"
redis> HDEL cart:77 product:1 product:9
(integer) 2
redis> EXISTS cart:77
(integer) 0
Field expiration (Redis 7.4+)
Normally a TTL belongs to the whole key. Since Redis 7.4 each field of a hash can expire on its own with HEXPIRE, and HTTL reports the remaining time. Redis 8 adds HSETEX (set fields with an expiry) and HGETEX (read fields and set or remove their expiry), plus HGETDEL (read and delete). These commands take FIELDS count field ... and return one reply per field:
HSET session:9 user_id 7 csrf abc123 theme dark
HEXPIRE session:9 900 FIELDS 1 csrf
HTTL session:9 FIELDS 3 csrf theme missing
HSETEX session:9 EX 60 FIELDS 1 otp 552211
HTTL session:9 FIELDS 1 otp
HGETDEL session:9 FIELDS 1 otp
HPERSIST session:9 FIELDS 1 csrf
HTTL session:9 FIELDS 1 csrfredis> HSET session:9 user_id 7 csrf abc123 theme dark
(integer) 3
redis> HEXPIRE session:9 900 FIELDS 1 csrf
1) (integer) 1
redis> HTTL session:9 FIELDS 3 csrf theme missing
1) (integer) 900
2) (integer) -1
3) (integer) -2
redis> HSETEX session:9 EX 60 FIELDS 1 otp 552211
(integer) 1
redis> HTTL session:9 FIELDS 1 otp
1) (integer) 60
redis> HGETDEL session:9 FIELDS 1 otp
1) "552211"
redis> HPERSIST session:9 FIELDS 1 csrf
1) (integer) 1
redis> HTTL session:9 FIELDS 1 csrf
1) (integer) -1
The HTTL codes follow the key-level TTL: -1 means the field has no expiry and -2 means the field does not exist. HEXPIRE returns 1 for each field whose expiry was set.
Hash or JSON string?
There are two common ways to store an object:
| One hash per object | One JSON string per object | |
|---|---|---|
| Read one field | HGET (cheap) |
GET the whole object and parse |
| Update one field | HSET (atomic) |
read, modify, write back (needs care) |
| Nested data | not supported: values are flat strings | fully supported |
| Counters | HINCRBY on a field |
not atomic |
| Memory | very compact for small hashes | one string |
Use hashes for flat objects with independent fields, and a JSON string (or the JSON data type, see the last lesson) for deeply nested documents.
Tip: Small hashes (in Redis 8.8 by default up to 512 fields with values up to 64 bytes) are stored in a compact
listpackthat uses much less memory than separate string keys. Grouping many tiny values into hashes is a well-known memory optimisation.
Test yourself
What does HSET user:1 name Ada age 36 return on a new key?
- OK
- (integer) 1
- (integer) 2
- (integer) 4
Which command returns every field and value of a hash?
- HKEYS
- HVALS
- HGETALL
- HMGET
Which Redis feature lets a single field of a hash expire on its own?
- EXPIRE with a field name
- HEXPIRE
- HDEL with a timeout
- It is not possible