Python Redis Set Commands

08.30.2021

Intro

Sets are lists filled with unique items. The set data type is helpfuly when you want to work with unique data types, thus they help with features where you want to easily deuplicate values. In this article, we will learn how to work with sets in Redis and Python.

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"
services:
  redis:
    image: "redis:alpine"
    command: redis-server
    ports:
      - "6379:6379"
    volumes:
      - $PWD/redis-data:/var/lib/redis
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf
    environment:
      - REDIS_REPLICATION_MODE=master

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, index.py and go through many of the common commands you will used with lists in redis.

Writing the Code

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

Set Add

We can push items to a list using lpush. The first param is the name of the key and then we can pass in as many items as we want for the list.

# Set Add
res = r.sadd("users", "user1")
print(res)

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

import redis

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

# Set Add
res = r.sadd("users", "user1")
print(res)

Set Cardinality (Size)

If we want to get the size of our set, we can use scard.

# Set Cardinaility
const result = r.scard("users")
print(result) # output: 2

Set Difference

We can get the difference or the values that are not in both sets using the sdiff command. This can be helpful for tracking usage metrics.

r.sadd("users-feature1", "user1", "user2")
r.sadd("users-feature2", "user1")

# Set Difference
const result = r.sdiff("users-feature1", "users-feature2")
print(result) # output: ["user2"]

Set Intersection

Similar to above, we can get the intersection between two sets, the values that are in both sets, using the SINTER command.

r.sadd("users-feature1", "user1", "user2")
r.sadd("users-feature2", "user1")

# Set Intersection
const result = r.sinter("users-feature1", "users-feature2")
print(result) # output: ["user1"]

Set Union

Keeping with the theme of set operations, we can take the union of two sets, returning a combination of sets, using the SUNION command.

r.sadd("users-feature1", "user1", "user2")
r.sadd("users-feature2", "user1", "user3")

# Set Union
const result = r.sunion("users-feature1", "users-feature2")
print(result) # output: ["user1", "user2", "user3"]

Set Is Member and Multiple is Members

We can check if a key is a member of a set using the SISMEMBER command.

r.sadd("users", "user1", "user2")

# Set Is Member
const result = r.sismember("users", "users1")
print(result) # output: true

We can check multiple members (if you use redis 6 or later) with the SMISMEMBER which means Set Multiple Is Member.

r.sadd("users", "user1", "user2")

# Set Multiple Is Member
const result = r.sismember("users", "users1", "user2", "user3")
print(result) # output: [true, true, false]

Set Members

To get a list of all members, we can use the SMEMBERS command.

r.sadd("users", "user1", "user2")

# Set Members
const result = r.smembers("users")
print(result) # output: ["user1", "user2"]

Set Remove

We can remove a member from one set to another using the SREM command.

r.sadd("users", "user1", "user2")

# Set Remove
r.srem("users", "user1")
const result = r.smembers("users")
print(result) # output: ["user2"]