What you will learn
- What Redis is and the problems it’s good at.
- Why a mostly single-threaded server can be so fast.
- How to install Redis, run your first commands and connect from Python.
Redis in one paragraph
Redis is an in-memory data structure server. Clients send commands over TCP; the server keeps keys in RAM and maps each key to a data structure: a string, list, hash, set, sorted set, stream and a few more. Because data lives in memory and commands work directly on these structures, typical operations take microseconds on the server. It can persist to disk and replicate, but memory is the primary home of the data.
Common uses:
- Caching database results, API responses and rendered pages.
- Sessions, tokens and OTPs with automatic expiry.
- Counters, rate limiters and leaderboards using atomic commands.
- Queues and event streams between services.
- Distributed locks and coordination, with care.
Poor fits: data much larger than RAM, complex ad-hoc queries and joins, or a system of record where losing the last second of writes is unacceptable (unless configured and tested for it).
Why it’s fast
- Memory, not disk, for every read and write.
- One main thread executes commands, so there are no locks around data structures and each command is naturally atomic. Network I/O can use extra threads in modern versions, but command execution stays sequential.
- An event loop (epoll/kqueue) serves thousands of connections without a thread per client.
- Compact encodings for small values (small hashes and lists are stored as packed arrays).
The flip side: one slow command (like KEYS * on millions of keys) blocks everyone. You’ll learn the safe alternatives as we go.
Install and run
On Debian/Ubuntu:
sudo apt-get install -y redis-server
redis-server --port 6379 --daemonize yes
redis-cli PING
On macOS use brew install redis; anywhere with Docker, docker run -p 6379:6379 redis:8. This course was tested with Redis 8.0.2 and the Python client redis-py 6.1 (pip install redis).
First commands
Each line runs one command with redis-cli and shows what it printed:
redis-cli PING # PONG
redis-cli SET greeting hello # OK
redis-cli GET greeting # hello
redis-cli EXISTS greeting # 1
redis-cli DEL greeting # 1
redis-cli GET greeting # (empty: the key is gone)
redis-cli SET session:abc u42 EX 60 # OK, expires in 60 s
redis-cli TTL session:abc # 60
redis-cli TYPE session:abc # string
Run redis-cli with no arguments for an interactive prompt with tab completion and HELP <command>.
From Python
import redis
r = redis.Redis(host="localhost", port=6379, decode_responses=True)
r.set("greeting", "hello")
print(r.get("greeting")) # hello
decode_responses=True returns str instead of bytes. The code in this course reads the port from REDIS_PORT (default 6379) and calls flushdb() at the start, so run it against a throwaway instance, never a shared one.
How fast on a laptop-class machine?
redis-benchmark ships with Redis. On the 8-core test machine used for this course:
redis-benchmark -t set,get -n 100000 -q
SET: 152671.77 requests per second, p50=0.167 msec
GET: 144092.22 requests per second, p50=0.175 msec
That’s without pipelining, so most of the time is network round trips. Batching commands (pipelines, MGET) raises throughput a lot, which you’ll use in later lessons.
Key takeaways
- Redis is an in-memory server of data structures, accessed with simple commands over TCP.
- Single-threaded command execution makes each command atomic, but one slow command blocks all clients.
- Great for caches, counters, rate limits, leaderboards, queues and sessions; not for data bigger than RAM or complex queries.
- Round trips dominate latency; batch when you can.
Exercises
- Install Redis, then use
SET,GET,EXPIREandTTLto create a key that disappears after 10 seconds. - Run
redis-benchmark -t get -n 100000 -q -P 16(16-command pipelines) and compare with the result above. - Run
INFO memoryand find how much memory an empty instance uses.
Quiz
- Why are individual Redis commands atomic?
- Name two workloads Redis is a poor fit for.
- Why can
KEYS *be dangerous in production?
Show answers
- One main thread executes commands one at a time, so no other command can run in the middle of one.
- Any two of: datasets much larger than RAM, complex ad-hoc queries or joins, a primary system of record that can’t tolerate losing recent writes.
- It scans every key in one command, blocking all other clients until it finishes; use
SCANinstead.