By Kevin McAleer, 5 Minutes
Page last updated November 24, 2026

By the end of this lesson you will be able to install Redis, store and read back a value with redis-cli, and do the same from a Python program.
Redis stores keys and values. The key is a name, like robot:1:distance, and the value is whatever you want to remember under that name. Because Redis lives in memory it is fast, but it also means you are limited by how much RAM the machine has.
Let’s get a server running, poke it by hand, then talk to it from Python.
flowchart LR
A["redis-cli<br/>(type commands)"] -->|"port 6379"| S[("Redis server")]
B["Python program<br/>(redis library)"] -->|"port 6379"| S
Both tools are just clients. They connect to the server on port 6379, send a command and print the reply.
Pick whichever option suits your computer.
Raspberry Pi OS, Debian or Ubuntu
sudo apt update
sudo apt install redis-server
The server starts automatically and runs in the background. By default it only accepts connections from the same machine, which is exactly what we want for learning.
macOS (with Homebrew)
brew install redis
brew services start redis
Any computer with Docker (including Windows)
docker run -d --name redis -p 127.0.0.1:6379:6379 redis
The 127.0.0.1: part keeps the port private to your own computer. See the Docker course if you’re new to Docker.
redis-cli is a command-line client that comes with Redis. (With Docker, run docker exec -it redis redis-cli to open it inside the container.) Type a command and press Enter:
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set robot:1:distance 42.5
OK
127.0.0.1:6379> get robot:1:distance
"42.5"
127.0.0.1:6379> incr robot:1:boots
(integer) 1
127.0.0.1:6379> incr robot:1:boots
(integer) 2
127.0.0.1:6379> get nothing
(nil)
A few things to notice:
PONG means the server is aliveset stores a value and get reads it back. The quotes in "42.5" are just how redis-cli prints textincr adds one to a number, creating it at 1 if it does not exist yet. It is great for counting boots, laps and button presses(nil)Redis stores text
The value
42.5went in as text and came back as text. Redis does not know it is a number until you ask it toincrit. When you read values in Python you convert them yourself, for example withfloat().
Install the library, ideally in a virtual environment:
pip install redis
On Raspberry Pi OS, create and activate a virtual environment first (python3 -m venv ~/redis-env then source ~/redis-env/bin/activate).
Save this as hello_redis.py:
import redis
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.set('robot:1:distance', 42.5)
print(r.get('robot:1:distance'))
distance = float(r.get('robot:1:distance'))
print(distance + 10)
print(r.get('nothing'))
Run it and you will see:
42.5
52.5
None
redis.Redis(...) creates a client. set() and get() have the same names as the commands. A missing key gives None in Python, where redis-cli printed (nil).
Bytes or strings?
Redis stores bytes. Without
decode_responses=True,get()returnsb'42.5'instead of'42.5'. We usedecode_responses=Truethroughout this course so you get normal Python strings back.
redis-cli, store your robot’s name under robot:1:name and read it backincr three times on a counter called laps, then get it. What type does Python give you if you r.get('laps')?float and prints “Battery low” if it is below 6.5redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused.sudo systemctl start redis-server on Debian, brew services start redis on macOS, or docker start redis).Why: Nothing is listening on port 6379, so the connection is refused.
get() returns b'42.5' with a b in front.decode_responses=True when you create the client.Why: Without it, the library hands you raw bytes.
TypeError when adding to a value you read.float(r.get('robot:1:distance')).Quick quiz 1 of 3
Which port does Redis listen on by default?
Quick quiz 2 of 3
Why do we pass decode_responses=True when connecting?
b'42.5'.Quick quiz 3 of 3
What does get return for a key that does not exist?
Teachers: installing software needs admin rights, which locked-down school computers often deny. A shared Redis on one Pi, or Docker on a teacher machine, avoids this. If you share one server, ask each learner to prefix their keys with their own name (for example
sam:robot:1:distance) so they don’t overwrite each other, and never publish port 6379 beyond the classroom network, because a default Redis has no password.
Comments