Use Hashes in Redis Databases
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Redis, the open-source NoSQL database, is frequently used for caching, messaging, and other storage needs where speed and low latency are required. Redis supports the hash data type which enables you to store field-value pairs of simple to highly complex data. This tutorial breakdown what Redis hashes are and walks you through examples of how to create, access, and modify hashes. This tutorial provides you with the tools to start using hashes in your Redis databases.
Familiarize yourself with our Getting Started with Linode guide and complete the steps for setting your Linode’s hostname and timezone.
This guide uses
sudowherever possible. Complete the sections of our How to Secure Your Server guide to create a standard user account, harden SSH access, and remove unnecessary network services.
Update your system.
On Debian and Ubuntu, use the following command:
sudo apt update && sudo apt upgrade
On AlmaLinux, CentOS (8 or later), or Fedora, use the following command:
sudo dnf upgrade
Follow the instructions in our How to Install and Configure Redis guide to installing a Redis server and command-line interface (CLI). Be sure to use the drop-down menu at the top of that page to select your Linux distribution and get the appropriate steps.
sudo. If you’re not familiar with the
sudocommand, see the Linux Users and Groups guide.
With hashes, you can have a single Redis entry (called a “key”) with numerous field-value pairs. This essentially allows you to associate properties with a given item in Redis.
Hashes are especially useful for storing objects and similar values consisting of numerous properties. In fact, because of Redis’s effectiveness for caching web applications, hashes are often used for storing data from JSON objects.
So, as an example, you might see hashes used for caching a user’s web session. That can include everything from username to the user’s current location in a document.
The following sections introduce you to using hashes in your Redis databases, highlighting some of the most useful commands. They include everything from creating hashes to accessing their values to modifying hash entries.
You can use the
HMSET command to set field-value pairs on a hash. You can use this command to create a hash that does not yet exist.
HMSET example_user_hash username example_user password example_password other_data example_other_data
In the command above,
example_user_hash is the name of the hash. Following that is a series of paired field names and values:
password example_password, and
HMSET command can also be used to modify hash entries. Calling the command for one or more existing fields changes their values to the newly-provided ones.
For fields with integer values, you can use the
HINCRBY command to increase the value by a given amount, which can be a negative number. Below is a full example:
HMSET example_hash first_field 10 HINCRBY example_hash first_field 1
The same can be done for float values by using the
HMSET example_hash second_field 5.5 HINCRBYFLOAT example_hash second_field -0.9
HMGET command to fetch the values of fields in a hash. For instance, the following command gets the
other_data fields’ values from the
example_user_hash that was created using the
HMSET command in the section above.
HMGET example_user_hash username other_data
1) "example_user" 2) "example_other_data"
You can even use the
HRANDFIELD to get one or more random field names from a hash. The command comes with a
WITHVALUES option to view the returned fields with their values, or, alternatively, you can get the values using
HRANDFIELD example_user_hash 2 WITHVALUES
1) "other_data" 2) "example_other_data" 3) "password" 4) "example_password"
Redis has several commands for listing all items in a hash, depending on what information you want about each item.
HKEYScommand to get all of the fields in a Redis hash.
1) "username" 2) "password" 3) "other_data"
HVALScommand to get all of the values for all of the fields in a hash.
1) "example_user" 2) "example_password" 3) "example_other_data"
HGETALLcommand to list all of the field-value pairs. Field names are given with values on the following line (so field names are on odd numbers and values on even):
1) "username" 2) "example_user" 3) "password" 4) "example_password" 5) "other_data" 6) "example_other_data"
To verify whether a field exists in a hash, you can use the
HEXISTS command. Provide the command with a hash and a field name. The command returns
1 for true (the field exists in the hash) or
0 for false (the field does not exist in the hash).
HEXISTS example_user_hash username
HEXISTS example_user_hash test_field_name
Delete one or more specific fields from a hash using the
HDEL command. The command takes the hash name followed by the names of the fields to be deleted.
HDEL example_user_hash other_data password HGETALL example_user_hash
1) "username" 2) "example_user"
You can also delete an entire hash using the
With this tutorial, you should be prepared to start making use of hashes in your Redis databases. These can significantly improve many storage tasks, especially object storage for web application caching. Check out other Redis guides and tutorials. They cover topics like, Using Sorted Sets in Redis Databases and Using Lists and Sets in Redis Databases.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on