Redis is a simple but powerful message broker that You can use as a communication medium for a distributed micro-service environment with multiple replicas. In this tutorial, we will use its “key value” and “task queue” features to store and access data.

Redis server

Install and execute Docker Desktop.

Use the following command to start a single Redis 7 instance within a Docker container and make it listen to port 9999 on localhost.

docker run -d -p 9999:6379 redis:7-alpine

It will require approximatly 8 Mi of RAM when its empty. Of course this will increase as You store more data.

Python client

  • Install Python
  • Install Redis for Python
pip install redis==4.3.4

Redis key value client

You can use key value pairs as a shared memory for Your replicas/nodes. For example to store the configuration of a programm.


This creates the key “keyname” and gives it the value “Value123”.

import redis
r = redis.Redis(host="localhost", port=9999, db=0)


This reads the key “keyname” and prints its value.

import redis
r = redis.Redis(host="localhost", port=9999, db=0)
key = r.get("keyname")

Note that the variable “key” will be a bytes class object. Therefore we need to convert it to a str class object (utf-8 string) with the decode() method in order to print it.

Redis task queue client

A queue is a list where entries disappear when once they are read. This means that any entry can be read just once. You can use task queues to distribute workload between workers.

By pushing to the right and reading from the left we will do FIFO (first-in-first-out) scheduling. However, You could switch this to LIFO (last-in-first-out) scheduling by reading from the same direction where You push (lpop() and lpush() or rpop() and rpush()).


This creates the queue “queuename” and adds the entry “value123” to the right end.

import redis
r = redis.Redis(host="localhost", port=9999, db=0)

FYI: The rpush() method will return the current amount of entries in the queue.


This will read a value from the left of the queue.

import redis
r = redis.Redis(host="localhost", port=9999, db=0)
entry1 = r.lpop("queuename")
entry2 = r.lpop("queuename")

Note: If a queue is empty and You read from it, the lpop() and rpop() methods will return None. So be sure to catch that AttributeError.

Could I help? Buy me a drink ! 💙