Redis Basics

Install Redis, try it out with redis-cli, then connect to it from Python and store a robot sensor reading.

By Kevin McAleer,    5 Minutes

Page last updated November 24, 2026


Course Cover Image


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.


Overview

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.

Installing Redis

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.

Say hello with redis-cli

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 alive
  • set stores a value and get reads it back. The quotes in "42.5" are just how redis-cli prints text
  • incr 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
  • A key that does not exist gives (nil)

Redis stores text

The value 42.5 went in as text and came back as text. Redis does not know it is a number until you ask it to incr it. When you read values in Python you convert them yourself, for example with float().

Connect from Python

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() returns b'42.5' instead of '42.5'. We use decode_responses=True throughout this course so you get normal Python strings back.

Try it Yourself

  1. In redis-cli, store your robot’s name under robot:1:name and read it back
  2. Use incr three times on a counter called laps, then get it. What type does Python give you if you r.get('laps')?
  3. Challenge: write a program that stores a pretend battery voltage, reads it back, converts it to a float and prints “Battery low” if it is below 6.5

Common Issues

  • Problem: redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused.
  • Solution: Start the server (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.

  • Problem: get() returns b'42.5' with a b in front.
  • Solution: Add decode_responses=True when you create the client.
  • Why: Without it, the library hands you raw bytes.

  • Problem: TypeError when adding to a value you read.
  • Solution: Convert it first, e.g. float(r.get('robot:1:distance')).
  • Why: Redis returns text, and Python will not add a number to a string.

Check your understanding

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?

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.

< Previous Next >

Was this lesson helpful?

Course complete

Congratulations, you finished Using Redis with Python!

That's all 12 lessons done. Let people know, it's a great way to inspire someone else to start.

Share card: I completed Using Redis with Python on KevsRobots
Post Bluesky LinkedIn Facebook


What are you looking for?
Watch Videos Get Ideas Learn Something Read a Review Read the Blog Search
... Z Z z