Tutorial

How To Manage Hashes in Redis

Updated on October 29, 2022

Manager, Developer Education

English
How To Manage Hashes in Redis

Introduction

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 name, address, email, or customer_id.

This tutorial will discuss how to manage hashes in Redis, from creating them to retrieving and deleting the data held within a hash.

How To Use This Guide

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.

Creating Hashes

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:

  1. 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.

hset returns (integer) 1 if the field specified is a new field and the value was set correctly:

Output
(integer) 1

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:

  1. hset poet:Verlaine nationality Francais

If the field already exists and its value was updated successfully, hset will return (integer) 0:

Output
(integer) 0

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 (integer) 0:

  1. hsetnx poet:Verlaine nationality French
Output
(integer) 0

To set multiple field/value pairs to a given set, use the hmset command followed by the corresponding field/value strings:

  1. hmset poet:Verlaine born 1844 died 1896 genre Decadent

hmset will return OK if it is successful.

Retrieving Information from Hashes

You can determine if a field exists for a given hash with the hexists command:

  1. 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:

  1. hget poet:Verlaine nationality
Output
"Francais"

hmget uses the same syntax, but can return the values of multiple fields:

  1. hmget poet:Verlaine born died
Output
1) "1844" 2) "1896"

If the hash you pass to hget or hmget does not exist, both commands will return (nil):

  1. hmget poet:Dickinson born died
Output
1) (nil) 2) (nil)

To obtain a list of all the fields held within a certain hash, run the hkeys command:

  1. hkeys poet:Verlaine
Output
1) "nationality" 2) "born" 3) "died" 4) "genre"

Conversely, run hvals to retrieve a list of values held within a hash:

  1. hvals poet:Verlaine
Output
1) "French" 2) "1844" 3) "1896" 4) "Decadent"

To return a list of every field held by a hash and their associated values, run hgetall:

  1. hgetall poet:Verlaine
Output
1) "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”:

  1. hlen poet:Verlaine
Output
(integer) 4

You can find the length of the value string associated with a field with hstrlen, which stands for “hash string length”:

  1. hstrlen poet:Verlaine nationality
Output
(integer) 8

hlen will return (integer) 0 if the hash does not exist.

Removing Fields from Hashes

To delete a field from a hash, run the hdel command. hdel can accept multiple fields as arguments, and will return an integer indicating how many fields were removed from the hash:

  1. hdel poet:Verlaine born died
Output
(integer) 2

If you pass a field that does not exist to hdel, it will ignore that field but delete any other existing fields you specify.

Conclusion

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.

Learn more about our products


Tutorial Series: How To Manage a Redis Database

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.

About the authors
Default avatar

Manager, Developer Education

Technical Writer @ DigitalOcean

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.