How to use Redis HSET

A hash in Redis is a data type used to store a mapping between a key and a value. Redis hashes are suitable for holding complex data objects as they can hold multiple key-value pairs with little space.

Example usage of a Redis hash is to store information about a post. A hash can be used to store information such as post_id, author_name, publish_data, category, and more.

Hashes are one of the fundamental data types, and whether you are new to Redis or a seasoned pro, knowing how to work with Hashes can be very beneficial.

The essence of this tutorial is to provide you with the fundamental knowledge to work with Hashes in Redis.

In this guide, we have used the latest version of the Redis Server running on a Debian 11 system. Although you do not have to replicate this environment, we recommend using the native Redis CLI. Using the Redis CLI will ensure you get similar outputs as this guide.

How to create a Hash

The first thing we will learn is how to create a hash in Redis.

Open the Redis CLI and use the HSET command to create a hash. The command takes the name of the hash key, the field, and the value as its arguments.

Take the example shown below:> HSET post title "Hello world title."

(integer) 1

The above command creates a hash with the key of post and a field of title and its corresponding value.

The command returns an integer 1 if the hash is created successfully.

It is good to ensure you provide all the arguments in the HSET command to avoid any errors. For example, the following command returns an error as one argument is missing from the command:> HSET post post_id

(error) ERR wrong number of arguments for ‘hset’ command

NOTE: If you run the HSET command where a hash already exists, Redis will overwrite the content of the old hash with the new one.

For example, take a look at the command below:> HSET post title "Overwritten title."

(integer) 0

In this case, the HSET command returns an integer 0 if the specified value is updated successfully.

To create multiple fields and their corresponding values in a single command, use the HMSET command.

For example:> HMSET post post_id 1 title "Hello world title." author_name "Linuxhint" publish_date "02/02/2022" categpgry "linux"


The command returns the string “OK” if the hash is created successfully.

How to Fetch Info from a Hash

Let us discuss the various commands you can use to fetch information from a Redis hash.

To get the value associated with a specific field, use the HGET command. The command takes the field name as the argument.

For example:> HGET post title

"Hello world title."

The command will restore the associated value of the specified key.

To get all keys in a hash, use the HGETALL command. This command takes the hash key as the argument.

Consider the example shown below:> HGETALL post

1) "title"

2) "Hello world title."

3) "post_id"

4) "1"

5) "author_name"

6) "Linuxhint"

7) "publish_date"

8) "02/02/2022"

9) "categogry"

10) "linux"

As shown in the example output above, the HGETALL command returns the fields and their associated values.

If you want to fetch values from multiple fields simultaneously, you can use the command HMGET. The command takes the key and the fields you wish to retrieve as the arguments.

Take a look at the example shown below:> HMGET post post_id title author_name

1) "1"

2) "Hello world title."

3) "Linuxhint"

The example above specifies the key and multiple fields, and the command returns the corresponding values.

Ensure the specified field exists; otherwise, Redis will return a nil value.

Suppose you only want to view the fields in a specific hash but not their corresponding values? For that, you can use the HKEYS command:> HKEYS post

1) "title"

2) "post_id"

3) "author_name"

4) "publish_date"

5) "categogry"

In the example above, the HKEY command takes the key as the argument and returns all the fields in the hash.

Similarly, you can use the HVALS command to retrieve the values in the hash.> HVALS post

1) "Hello world title."

2) "1"

3) "Linuxhint"

4) "02/02/2022"

5) "linux"

The command will only return the values and not their fields.

To get an integer value of the total number of fields in the hash, use the HVAL command.> HLEN post

(integer) 5

The example command above shows that the hash contains five fields.

Deleting Keys in a Hash

To remove a field from a specific hash, you can use the HDEL command. The command takes single or multiple fields as arguments.

Example:> HDEL post post_id title

(integer) 2

The command returns an integer value indicating the number of fields removed from the hash.

If the field does not exist, the command ignores it and only removes the existing ones.

To check if a field exists in the hash, use the HEXISTS command.> HEXISTS post author_name

(integer) 1

The command returns integer 1 if the key exists and 0 if not.


The tutorial covers commands and examples to use when creating and working with Hashes in Redis. There are other commands beyond the scope of this tutorial. Check the documentation to learn more.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list