paint-brush
What Are Redis Hashesby@smpnjn
632 reads
632 reads

What Are Redis Hashes

by Johnny SimpsonOctober 1st, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Redis hashes are a type of record stored in a Redis database. They are mutable, so can be easily changed and updated depending on your needs. If you are new to Redis, make sure you install it first before trying this tutorial. Redis is 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` to create a hash, and update or add to it.
featured image - What Are Redis Hashes
Johnny Simpson HackerNoon profile picture


Redis hashes are a type of record stored in a Redis database. They are 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 types of 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 as 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 the terminal, and then try running the following to set a new key:


HSET user:1 keyOne valueOne keyTwo valueTwo


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


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


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


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


Will return:


1) "keyOne"
2) "valueOne"
3) "keyTwo"
4) "valueTwo"


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


Which will return:


"valueOne"


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


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


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


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



Also published here.