So far, we have been exploring how to store simple key-value pairs in Redis. This works well for individual values, but real-world applications often require storing groups of fields that belong to a single object. For example, a user object might have multiple attributes like name
, age
, city
, etc.
Redis provides hashes to handle such structured data efficiently.
What is a Redis Hash?
A hash in Redis is similar to a hash map in programming languages like Java. It allows you to store field-value pairs under a single key. This is ideal for representing objects with multiple attributes.
Example structure for a user object:
Field | Value |
---|---|
id | 1 |
name | Sam |
age | 28 |
city | Atlanta |
In Redis, this can be stored as a hash with the key user:1
.
Creating a Hash
To create a hash, Redis provides the HSET
command. Let’s start with a clean database:
FLUSHDB
Now, we can create our first user object:
HSET user:1 name "Sam" age 28 city "Atlanta"
Output:
(integer) 3
The output 3
indicates that three fields were successfully added.
Accessing Hashes
Unlike simple key-value pairs, you cannot use GET
to access a hash key. You must use hash-specific commands.
Check the type of the key:
TYPE user:1
Output:
hash
Access a specific field:
HGET user:1 name
Output:
"Sam"
This is similar to SQL:
SELECT name FROM user WHERE id = 1;
Access all fields:
HGETALL user:1
Output:
1) "name" 2) "Sam" 3) "age" 4) "28" 5) "city" 6) "Atlanta"
This retrieves all field-value pairs, although the formatting may appear a little confusing. Libraries in programming languages (e.g., Java, Python) usually format this neatly.
Creating Another Hash
You can create additional user objects with different fields:
HSET user:2 name "Alice" age 30 status "active"
Now we have two separate objects (user:1
and user:2
) stored as hashes. Redis does not enforce a schema, so different objects can have different fields.
Checking Existence of Fields
To check if a specific field exists within a hash:
HEXISTS user:2 status
Output:
(integer) 1
1
means the field exists.0
means it does not exist.
Retrieving All Keys or Values
- Retrieve all fields (keys) of a hash:
HKEYS user:1
Output:
1) "name" 2) "age" 3) "city"
- Retrieve all values of a hash:
HVALS user:1
Output:
1) "Sam" 2) "28" 3) "Atlanta"
Deleting Fields
You can delete a specific field from a hash using HDEL
:
HDEL user:1 age
Now, if you check all fields:
HGETALL user:1
Output:
1) "name" 2) "Sam" 3) "city" 4) "Atlanta"
The age
field has been removed.
Deleting the Entire Hash
To remove the whole object:
DEL user:1
After deletion, the hash no longer exists in Redis:
EXISTS user:1
Output:
(integer) 0
Expiring Hashes
Unlike simple keys, you cannot set an expiry for a single field in a hash. You can only set an expiration for the entire hash object:
SETEX user:2 10
After 10 seconds, the hash will automatically be removed.
Summary of Common Hash Commands
Command | Description |
---|---|
HSET key field value | Set a field in a hash |
HGET key field | Get the value of a field |
HGETALL key | Get all fields and values |
HKEYS key | Get all field names |
HVALS key | Get all field values |
HEXISTS key field | Check if a field exists |
HDEL key field | Delete a specific field |
DEL key | Delete the entire hash |
EXISTS key | Check if the hash exists |
Hashes allow you to efficiently store and manage structured objects in Redis, making them ideal for user profiles, product details, or any object with multiple attributes.