Redis Hash Data Structure

In this Redis tutorial we learn about a dictionary-like data structure in Redis called the Hash.

We learn how to create Hashes (Hash keys) and how to use the various Hash commands that Redis provides for us.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

Redis Hash Data Structure

Hashes in Redis are a lot like dictionaries in Python . They allow us to create a collection of field:value pairs associated with a single key.

How to create a Hash

To create a hash we use the HMSET command. First, we specify the name of the hash as the key, followed by one or more field:value pairs.

Syntax:
HMSET key_name field_1 value_1 field_2 value_2
Example:
HMSET emp-1 fname John lname Doe age 30 department IT

Now everything is contained within the ‘emp-1’ key. We can now have multiple different entities, each with their own set of field:value pairs.

Example:
HMSET emp-2 fname Jane lname Doe age 28 department Legal

How to return a value from a Hash key

To get a value from a hash key we use the HGET command with both the key name and field name as arguments.

Syntax:
HGET key_name field_name
Example:
HGET emp-1 fname

The example above will return the value that’s associated with the ‘fname’ field from the ‘emp-1’ key.

Output:
"John"

Note that we can only return a single field with the HGET command.

How to return multiple values from a Hash key

To get more than one value from a hash key, we use the HMGET command. The first argument is the key name, followed by two or more field names.

Syntax:
HMGET key_name field_name-1 field_name-2
Example:
HMGET emp-1 fname age

The example above will return the values for the ‘fname’ and ‘age’ fields.

Output:
1) "John"
2) "30"

If we try to get the value of a non-existing field, the value will be show as nil.

Example:
HMGET emp-1 fname age gender

In the example above we try to get the value of the ‘gender’ field, which doesn’t exist in our hash.

Output:
1) "John"
2) "30"
3) (nil)

How to return all the fields and values from a Hash key

To get all the fields and values from a hash, we use the HGETALL command with the key name as the argument.

Syntax:
HGETALL key_name
Example:
HGETALL emp-1

This will return all the fields with their values from the specified key.

Output:
1) "fname"
2) "John"
3) "lname"
4) "Doe"
5) "age"
6) "30"
7) "department"
8) "IT"

How to check if a field exists in a Hash key

To check if a field exists in a hash, we use the HEXISTS command with the key and field name as the arguments.

Syntax:
HEXISTS key_name field_name
Example:
HEXISTS emp-1 age

If the field does exist, it will return 1, indicating true. If the field does not exist, it will return 0, indicating false.

Output:
(integer) 1

Because the ‘age’ key does exist in our example, Redis will return 1.

How to create a field only if it doesn’t exist

To create a field only if it doesn’t exist already, we use the HSETNX command. First, we specify the key where we want to add the field, followed by the field name and its value.

Syntax:
HSETNX key_name field_name value
Example:
HSETNX emp-1 birthday "25 Jul"

Because the birthday field doesn’t exist in our ‘emp-1’ key, it will be added.

If we use the HGETALL command now on the ‘emp-1’ key, it will show the added field and its value.

Output:
1) "fname"
2) "John"
3) "lname"
4) "Doe"
5) "age"
6) "30"
7) "department"
8) "IT"
9) "birthday"
10) "25 Jul"

If we try to use the same command again, Redis will return a 0, indicating that the field already exists and wasn’t added.

How to delete a field from a Hash key

To delete a field and its value from a key, we use the HDEL command. First, we specify the key name where we want to delete the fields from, followed by one or more fields that we want to delete.

Syntax:
HDEL key_name field_name-1 field_name-2
Example:
HDEL emp-1 birthday

If we use the HGETALL command, we can see the field was deleted.

Output:
1) "fname"
2) "John"
3) "lname"
4) "Doe"
5) "age"
6) "30"
7) "department"
8) "IT"

How to return only the field names from a Hash key

If we want to see all the fields of a hash key, we use the HKEYS command with the key name as the argument.

Syntax:
HKEYS key_name
Example:
HKEYS emp-1

This will return only the field names that are in the specified hash key.

Output:
1) "fname"
2) "lname"
3) "age"
4) "department"

How to return only the values from a Hash key

If we want to see all the values of the fields in a hash key, we use the HVALS command with the key name as the argument.

Syntax:
HVALS key_name
Example:
HVALS emp-1

This will return only the values that are in the specified hash key.

Output:
1) "John"
2) "Doe"
3) "30"
4) "IT"

How to see the amount of fields in a Hash key

If we want to see the number of fields in a hash key, we use the HLEN command with the key name as the argument.

Syntax:
HLEN key_name
Example:
HLEN emp-1

This will return only the amount of fields in the specified hash key.

Output:
(integer) 4

How to increment and decrement numerical values

To increment a numerical value, we use the HINCRBY command. The first argument is the key name, followed by the field name we want to increment and then the number we want to increment by.

Syntax:
HINCRBY key_name field_name number
Example:
HINCRBY emp-1 age 1

In the example above we increment the ‘age’ field’s value by 1.

Output:
(integer) 31

To decrement the number, we simply pass in a negative value as the argument.

Syntax:
HINCRBY key_name field_name -number
Example:
HINCRBY emp-1 age -3

In the example above we decrement the ‘age’ field’s value by 3.

Output:
(integer) 28