Redis is an open-source, in-memory key-value data store. A Redis hash is a data type that represents a mapping between a string field and a string value. Hashes can hold many field-value pairs and are designed not to take up much space, making them ideal for representing data objects. For example, a hash might represent a customer and include fields like
This tutorial will discuss how to manage hashes in Redis, from creating them to retrieving and deleting the data held within a hash.
This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.
The commands shown in this guide were tested on an Ubuntu 22.04 server running Redis version 6.0.16. To set up a similar environment, you can follow Step 1 of our guide on How To Install and Secure Redis on Ubuntu 22.04. We will demonstrate how these commands behave by running them with
redis-cli, the Redis command line interface. If you’re using a different Redis interface — Redli, for example — the exact output of certain commands may differ.
Alternatively, you can provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our Managed Databases product documentation. Then, you must either install Redli or set up a TLS tunnel in order to connect to the Managed Database over TLS.
To create a hash, run the
hset command. This command accepts the name of the hash key, the field string, and the corresponding value string as arguments:
- hset poet:Verlaine nationality French
Note: In this example and the following ones,
poet:Verlaine is the hash key. Dots, dashes, and colons are commonly used to make multi-word keys and fields more readable. It’s helpful to ensure that your keys follow a consistent and human-readable format.
(integer) 1 if the field specified is a new field and the value was set correctly:
If, however, you fail to include a value, field, or name for the hash key,
hset will return an error.
Also, note that
hset will overwrite the contents of the hash if it already exists:
- hset poet:Verlaine nationality Francais
If the field already exists and its value was updated successfully,
hset will return
You can also use
hsetnx to add fields to hashes, but it will only work if the field does not yet exist. If the specified field does already exist, the
hsetnx won’t have any effect and will return
- hsetnx poet:Verlaine nationality French
To set multiple field/value pairs to a given set, use the
hmset command followed by the corresponding field/value strings:
- hmset poet:Verlaine born 1844 died 1896 genre Decadent
hmset will return
OK if it is successful.
You can determine if a field exists for a given hash with the
- hexists poet:Verlaine nationality
hexists will return
(integer) 1 if the field does exist, and
(integer) 0 if it doesn’t.
To return a field’s value, run the
hget command followed by the hash key and the field whose value you want to retrieve:
- hget poet:Verlaine nationality
hmget uses the same syntax, but can return the values of multiple fields:
- hmget poet:Verlaine born died
Output1) "1844" 2) "1896"
If the hash you pass to
hmget does not exist, both commands will return
- hmget poet:Dickinson born died
Output1) (nil) 2) (nil)
To obtain a list of all the fields held within a certain hash, run the
- hkeys poet:Verlaine
Output1) "nationality" 2) "born" 3) "died" 4) "genre"
hvals to retrieve a list of values held within a hash:
- hvals poet:Verlaine
Output1) "French" 2) "1844" 3) "1896" 4) "Decadent"
To return a list of every field held by a hash and their associated values, run
- hgetall poet:Verlaine
Output1) "nationality" 2) "French" 3) "born" 4) "1844" 5) "died" 6) "1896" 7) "genre" 8) "Decadent"
You can find the number of fields in a hash by running
hlen, which stands for “hash length”:
- hlen poet:Verlaine
You can find the length of the value string associated with a field with
hstrlen, which stands for “hash string length”:
- hstrlen poet:Verlaine nationality
hlen will return
(integer) 0 if the hash does not exist.
To delete a field from a hash, run the
hdel can accept multiple fields as arguments, and will return an integer indicating how many fields were removed from the hash:
- hdel poet:Verlaine born died
If you pass a field that does not exist to
hdel, it will ignore that field but delete any other existing fields you specify.
This guide details a number of commands used to create and manage hashes in Redis. If there are other related commands, arguments, or procedures you’d like to learn about in this guide, please ask or make suggestions in the comments.
For more information on Redis commands, check out our tutorial series on How to Manage a Redis Database.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Redis is an open-source, in-memory key-value data store. A NoSQL database, Redis doesn’t use structured query language, otherwise known as SQL. Redis instead comes with its own set of commands for managing and accessing data.
The tutorials included in this series cover a broad range of Redis commands, but they generally focus on connecting to a Redis database, managing a variety of data types, and troubleshooting and debugging problems, along with a few other more specific functions. They are written in cheat sheet format with self-contained examples. We encourage you to jump to whichever guide is relevant to the task you’re trying to complete.
Click here to Sign up and get $200 of credit to try our products over 60 days!