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 , 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. before trying this tutorial. mutable If you are new to Redis, make sure you install it first 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 . Start Redis by running in the terminal, and then try running the following to set a new key: HSET redis-cli HSET user:1 keyOne valueOne keyTwo valueTwo The naming convention of a Redis hash is typed as , so here we have , to represent user number 1. The syntax above may seem confusing, but it follows this convention: hash:key user:1 HSET hash:key key value key value key value .... So, when we wrote , we , and then we created a key called with a value , and a key called with a value of . You can continue this pattern forever, meaning your hash can have as many key value pairs as you like. HSET user:1 keyOne valueOne keyTwo valueTwo created a new hash called user:1 keyOne valueOne keyTwo valueTwo Updating and Adding New Keys in Redis Hashes We can use the command to create a hash, and also update or add to it. For example, to add a new key and value to , we simply run again with the new key and value: HSET user:1 HSET HSET user:1 keyThree valueThree If we later want to update to have a value of , we would run again to overwrite the value of : keyThree valueFour HSET 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 . This will return all keys and values within the hash specified. For example: HGETALL 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 . For example, to get the value of , we run: HGET keyOne 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 . Let's say we want to increase the user's score by . Instead of using , we could do this: HINCRBY 200 HSET 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 . Taking our first example of , if we wanted to delete , we could do so by running the following command: HDEL user:1 keyOne HDEL user:1 keyOne If you didn't want to have the hash at all and wanted to remove entirely, then you can simply use instead: user:1 del del user:1 Also published . here