Python Redis HSET and other Hash Commands



Hash data types are used in many alogrithms to increase speed. They usually take more memory but improved the processing speed. This makes them an asset in the redis database which takes the same approach. In this article, we will learn how to use hashes in r.

Setting up Redis

For setting up Redis, I would recommend using a service for you in prod. Azure for example, has a great redis service that scales easily. However, you will want to learn redis and eventually how to scale it yourself. This will help with debugging cloud services or eventually, saving money and not using them.

We will start our intro to redis via using docker compose. Create a docker-compose.yml file and add the following.

version: "3.2"
    image: "redis:alpine"
    command: redis-server
      - "6379:6379"
      - $PWD/redis-data:/var/lib/redis
      - $PWD/r.conf:/usr/local/etc/redis/r.conf

Ensure you have docker installed and run

docker-compose up

Installing Redis Modules

In python, the main used redis module is called redis-py and can be installed using the follows.

pip install redis

Writing the Code

Let's open up a new file, and go through many of the common commands you will used with lists in r.

Hash Set

We can create a hash and set a key using the following. The example below is similar to creating a hash/object/dictionary like so:

person1Hash = {
  "name": 'jane'
# Hash Set
result = r.hset("person1-hash", "name", "jane")

For each of the examples below, I will use the following template to run all the commands. Here is my full index file. We will just replace the commands each time.

import redis

r = redis.Redis(host='localhost', port=6379, db=0)

# Hash Set
result = r.hset("person1-hash", "name", "jane")

Hash Multiple Set

We can set multiple keys using the hmset function. Below we set the name and hage of a new hash.

# Hash Multiple Set
result = r.hmset("person1-hash", {
  "name": "jane",
  "age": 20

Hash Get

We can get a field on a hash using the hget function. The example below gets the name value of our hash.

# Hash Get
result = r.hget("person1-hash", "name")

Hash Multiple Get

If we want to access multiple fields from a single hash, we can use hmget function. Below we retrieve the name and age values from our hash.

# Hash Multiple Get
result = r.hmget("person1-hash", "name", "age")

Hash Get All

If we want to get all keys from a hash, we can use hget.

# Hash Get All
result = r.hget("person1-hash")

Hash Exists

If we use hexists we can check if a hash or a property on a hash exists. Here are some examples of checking fields and a hash.

# Hash Exists
result = r.hexists("person1-hash", "name")

const result2 = await r.hexists("person1-hash", "age")

const result3 = await r.hexists("person2-hash", "name")

Hash Delete

To delete a hash field we use the hdel function. This will remove fields from our hash.

# Hash Del
result = r.hdel("person1-hash", "name")

const result2 = await r.hdel("person1-hash", "name", "age")

Hash Increment By

Hashes also support Increment By if the field values are numeric. For example, we can increment the money a hash has using the following.

await r.hset("person1-hash", "money", 100)

# Hash increment by
result = r.hincrby("person1-hash", "money", 10)

const result2 = await r.hincrby("person1-hash", "money", -20)

Hash Keys

We get gey all keys for a hash using the hkeys function.

# Hash keys
result = r.hkeys("person1-hash")

Hash Length

To get the length of number of keys in a hash, we can use hlen.

# Hash length
result = r.hlen("person1-hash")