Learnitweb

Working with Hashes in Redis

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:

FieldValue
id1
nameSam
age28
cityAtlanta

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

CommandDescription
HSET key field valueSet a field in a hash
HGET key fieldGet the value of a field
HGETALL keyGet all fields and values
HKEYS keyGet all field names
HVALS keyGet all field values
HEXISTS key fieldCheck if a field exists
HDEL key fieldDelete a specific field
DEL keyDelete the entire hash
EXISTS keyCheck 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.