DEV Community

Cover image for A Complete Guide to Redis Hashes
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

A Complete Guide to Redis Hashes

Redis hashes are a type of record stored in a Redis database. They are a little like JSON objects, and store data as key value pairs. They are mutable, so can be easily changed and updated depending on your needs. As such, they are a great way to store certain typeso f data in Redis. If you are new to Redis, make sure you install it first before trying this tutorial.

Redis hashes are flat in structure, so we can't have multiple levels like we do in JSON. If we want to add a new hash, we use the terminal command HSET. Start redis by running redis-cli in terminal, and then try running the following to set a new key:

HSET user:1 keyOne valueOne keyTwo valueTwo
Enter fullscreen mode Exit fullscreen mode

The naming convention of a redis hash is typed as hash:key, so here we have user:1, to represent user number 1. The syntax above may seem confusing, but it follows this convention:

HSET hash:key key value key value key value .... 
Enter fullscreen mode Exit fullscreen mode

So, when we wrote HSET user:1 keyOne valueOne keyTwo valueTwo, we created a new hash called user:1, and then we created a key called keyOne with a value valueOne, and a key called keyTwo with a value of valueTwo. You can continue this pattern forever, meaning your hash can have as many key value pairs as you like.

Updating and Adding New Keys in Redis Hashes

We can use the HSET command to create a hash, and also update or add to it. For example, to add a new key and value to user:1, we simply run HSET again with the new key and value:

HSET user:1 keyThree valueThree
Enter fullscreen mode Exit fullscreen mode

If we later want to update keyThree to have a value of valueFour, we would run HSET again to overwrite the value of keyThree:

HSET user:1 keyThree valueFour
Enter fullscreen mode Exit fullscreen mode

Getting Hash Key Values and Hashes in Redis

If you want to get all keys and values in a specific hash, you use HGETALL. This will return all keys and values within the hash specified. For example:

HGETALL user:1
Enter fullscreen mode Exit fullscreen mode

Will return:

1) "keyOne"
2) "valueOne"
3) "keyTwo"
4) "valueTwo"
Enter fullscreen mode Exit fullscreen mode

Meanwhile, if you want to get the value of one specific key within a hash, we use HGET. For example, to get the value of keyOne, we run:

HGET user:1 keyOne
Enter fullscreen mode Exit fullscreen mode

Which will return:

"valueOne"
Enter fullscreen mode Exit fullscreen mode

Increasing Hash Key Values by an amount

A common use case for hashes is storing user scores on a scoreboard. In this case, it's pretty common that we'd want to increase the user's score by a certain amount if it is a number. For example, suppose we have this scoreboard:

HSET scoreboard:1 userNameOne 200
Enter fullscreen mode Exit fullscreen mode

If we need to update the user's score, we can easily increase the user's score by a certain amount using HINCRBY. Let's say we want to increase the user's score by 200. Instead of using HSET, we could do this:

HINCRBY scoreboard:1 userNameOne 200
Enter fullscreen mode Exit fullscreen mode

Deleting Hash Keys and Values in Redis

Finally, if you want to delete hash keys for a specific hash, we use HDEL. Taking our first example of user:1, if we wanted to delete keyOne, we could do so by running the following command:

HDEL user:1 keyOne
Enter fullscreen mode Exit fullscreen mode

If you didn't want to have the hash at all, and wanted to remove user:1 entirely, then you can simply use del instead:

del user:1
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

I liked it, thanks. What I wished you could have included is why hashes need to follow the pattern hash:key. Thinking in practical terms in the context of microservices, I imagine I could set the key part to the microservice's major.minor version number, or even just the major version number. Just an initial thought.

Collapse
 
smpnjn profile image
Johnny Simpson

Good idea - something I missed but useful information.