Module 1: Redis Fundamentals
Introduction to Module 1: Redis Fundamentals
Overview
You just wrapped up the FastAPI Advanced Features guide. Your Task Manager API works — it has full CRUD, WebSockets, background tasks, and decent middleware. In the next guide (PostgreSQL & SQLAlchemy) you'll give it a real database so the data doesn't vanish on restart. And in guide #9 (Authentication) you'll add JWT to protect the endpoints. All good — but there's a problem none of those guides solves, and you'll feel it the day your API gets real traffic: every request queries PostgreSQL directly, whether or not the data changed.
When your API has 10 concurrent users, nothing happens. When it has 10,000, PostgreSQL becomes the bottleneck. A product page that loads in 50 ms with 10 users takes 800 ms when 1,000 people request it at the same time. The reason isn't that PostgreSQL is slow — it's that it's answering the same question over and over, unnecessarily. This is where Redis comes in: an in-memory store that sits between your API and PostgreSQL, serving responses in milliseconds when the data hasn't changed.
But Redis isn't just a cache. It's a versatile tool with 5 different data types, each optimized for a specific use case: atomic counters for rate limiting, hashes for user sessions, lists for task queues, sets for unique tags, sorted sets for rankings and leaderboards. If you leave this module thinking "Redis = SET and GET on strings," you'll miss 80% of its power. This module is the entry point to Redis: installing it, mastering the CLI, understanding the 5 data types with judgment, and connecting to it from Python. Without this, module 2's caching patterns and module 3's rate limiting would be recipes memorized without understanding.
Where are we in the guide?
You're in Module 1 of 5 of the Redis & Caching Strategies guide:
Module 1: Redis Fundamentals ← YOU ARE HERE
→ Installation, CLI, the 5 data types, redis-py
Module 2: Caching Patterns & TTL
→ Cache-aside, write-through, write-behind, TTL strategies, invalidation
Module 3: Rate Limiting & Session Storage
→ Token bucket, sliding window, Redis sessions complementing JWT
Module 4: Pub/Sub & FastAPI Integration
→ Pub/Sub, redis.asyncio, dependency injection, middleware, connection pooling
Module 5: Project — Production Cached API
→ The full stack: FastAPI + PostgreSQL + Redis in Docker Compose
How this module connects with the next ones
Every data type you learn here has a specific destination:
- Strings → caching simple responses (module 2), basic rate limiting counters (module 3)
- Hashes → caching objects (module 2), user sessions (module 3)
- Lists → event logs (module 4), write-behind buffers (module 2)
- Sets → cached tags and categories (module 2), tracking online users
- Sorted sets → sliding window rate limiting (module 3), leaderboards, priority queues
If you master the 5 data types now, the next modules flow. If you only master strings, module 3 turns into a headache.
Why Redis and not something else?
Before installing anything, it's worth understanding what makes Redis special and why the entire industry uses it.
Redis vs PostgreSQL
PostgreSQL stores data on disk with ACID guarantees — perfect for critical data (users, orders, payments). But disk is slow compared to RAM: a simple query to PostgreSQL takes 5-50 ms. An equivalent operation in Redis takes 0.5-2 ms.
Redis stores data in memory (RAM) — extremely fast, but with a trade-off: if Redis restarts without persistence configured, you lose the data. That sounds terrible until you understand that Redis isn't the source of truth — it's an intermediate layer. The source of truth is still PostgreSQL. If Redis goes down, the data is safe in the database.
Redis vs Memcached
Memcached is another popular in-memory store, but it only supports strings. Redis supports 5 data types (strings, hashes, lists, sets, sorted sets) and more sophisticated operations. Memcached was dominant in 2010; Redis displaced it because flexibility matters more than simplicity when you're building complex systems.
Redis vs a Python dict
A Python dict in memory is fast — faster than Redis, even. But there are 3 reasons a dict doesn't replace Redis in production:
- It isn't shared between processes. If your API runs with 4 uvicorn workers, each worker has its own dict. One worker's cache is invisible to the others.
- It's lost on restart. Every deploy wipes the entire cache. With Redis, the cache survives deploys.
- It has no safe concurrent operations. An INCR in Python with multiple threads requires manual locks. Redis makes INCR atomic by design.
Redis fills exactly this gap: fast like memory, shared between processes, survives restarts (with persistence), atomic on concurrent operations.
What you'll learn in this module
By the end of this module's 5 capsules, you'll be able to:
Operational
- Install Redis locally or with Docker (
docker run -d -p 6379:6379 redis) in under 30 seconds - Connect with
redis-cli, runping, and get backPONG - Navigate redis-cli fluently:
SET,GET,DEL,KEYS,EXISTS,TYPE,TTL,EXPIRE,PERSIST - Use professional exploration commands:
MONITOR,INFO,DBSIZE,FLUSHDB
The 5 data types, with judgment
- Strings: atomic counters with
INCR/DECR, simple locks withSETNX, batch operations withMSET/MGET - Hashes: structured objects with
HSET/HGET/HGETALL, counters inside objects withHINCRBY - Lists: FIFO queues with
RPUSH+LPOP, LIFO stacks withLPUSH+LPOP, reading without extracting withLRANGE - Sets: unique collections with
SADD/SREM, set operations withSINTER/SUNION/SDIFF - Sorted sets: ordered rankings with
ZADD+score, range queries withZRANGEBYSCORE, cleanup withZREMRANGEBYSCORE
Python integration
- Install
redis-pywithpip install redis - Connect from Python:
r = redis.Redis(host='localhost', port=6379, decode_responses=True) - Run the same CLI operations from Python code
- Use pipelines for efficient batch operations
- Build a Redis Explorer mini-project that exercises all 5 data types
Connection with the capstone project
Module 5's capstone project (Production Cached API) uses all 5 data types with a specific purpose:
| Data type | Use in the capstone project |
|---|---|
| Strings | Caching endpoint responses with a TTL (/products, /categories) |
| Hashes | User sessions complementing JWT (session:{user_id}) |
| Lists | Event log of cache invalidation events (debugging) |
| Sets | Set of active sessions per user (active_sessions:{user_id}) |
| Sorted sets | Sliding window rate limiting (rate_limit:{user_id} with timestamps as scores) |
If all you learn in this module is strings, you won't be able to implement professional rate limiting or session management in module 5. That's why this module gives proportional time to each data type instead of leaning disproportionately on strings.
This module's 5 capsules
Capsule 01: Module introduction (you are here)
→ Context, objectives, progression, expectations
Capsule 02: Installation and redis-cli
→ Docker setup, ping, navigation commands, professional exploration
Capsule 03: Strings and Hashes
→ The two most-used data types: counters, object caching, sessions
Capsule 04: Lists, Sets, and Sorted Sets
→ Queues, unique collections, rankings — the foundation for rate limiting
Capsule 05: redis-py + Redis Explorer mini-project
→ Python connection, pipelines, exhaustive exploration of the 5 data types
Estimated time: 1.5-2.0 hours of active study (reading + running commands + completing the mini-project).
What you will NOT learn in this module
This guide has a clear scope. These topics are intentionally left out:
- ❌ Redis Cluster (high availability, sharding) — out of scope, requires distributed systems knowledge
- ❌ Redis Sentinel (automatic failover) — out of scope, that's infrastructure/DevOps territory
- ❌ Redis Streams (a Pub/Sub replacement with persistence) — an advanced feature; we mention Pub/Sub's limitations in module 4 but we don't build with Streams
- ❌ Lua scripting in Redis — useful for complex atomic operations, but rarely needed for typical backend use cases
- ❌ Advanced persistence (RDB vs AOF, configuring snapshot intervals) — we'll use reasonable defaults; going deeper is DevOps's responsibility
- ❌ Caching patterns (cache-aside, write-through, write-behind) — module 2 covers them completely
- ❌ Rate limiting — module 3 covers it in detail
- ❌ Pub/Sub — module 4 covers it
If after this guide you need to go deeper into Redis Cluster or Sentinel, there are resources at the end of each capsule pointing to the official documentation.
Prerequisites to get started
Before moving on to capsule 02, make sure you have:
Software installed
- Python 3.10+ installed (
python --version) - Docker Desktop (Mac/Windows) or Docker Engine (Linux) installed and running (
docker --versionanddocker psshould respond without an error) - A terminal you're comfortable with (Terminal on Mac, Windows Terminal, iTerm, Warp — any of them works)
Assumed prior knowledge
- Basic-to-intermediate FastAPI (Guides #6-7 of the Backend Python Developer path)
- Basic Python: functions, dicts, list comprehensions, exception handling
- Command line:
cd,ls,mkdir, basic shell commands - Basic Docker:
docker run,docker ps,docker stop,docker logs— you don't need Docker Compose yet (that comes in module 5)
What you do NOT need
- Prior experience with Redis (this guide starts from zero)
- Distributed systems knowledge
- DevOps or server administration experience
- Experience with message queues (RabbitMQ, Kafka — those are a different tool)
- Experience with NoSQL databases (MongoDB, DynamoDB — Redis isn't exactly NoSQL, it's in-memory)
If you're missing something from "assumed prior knowledge," go back to the corresponding guide in the path before continuing. This guide builds on those prerequisites — it doesn't re-teach them.
Recommended workspace setup
Before starting capsule 02, organize your workspace like this:
~/projects/
└── redis-guide/
├── module-01-fundamentals/
│ ├── exploration.py # Practice scripts
│ └── redis-explorer/ # Module 1's mini-project
├── module-02-patterns/
├── module-03-rate-limiting/
├── module-04-fastapi/
└── module-05-final-project/ # Production Cached API
Create the structure now:
mkdir -p ~/projects/redis-guide/module-01-fundamentals/redis-explorer
cd ~/projects/redis-guide/module-01-fundamentals
Each module gets its own folder. Keep your work separated by module — it makes it easier to review what you learned when you move on to the next one.
Virtual environment
Even though capsule 05 is the first one where you use Python, create the venv now so it's ready:
cd ~/projects/redis-guide/module-01-fundamentals/redis-explorer
python -m venv .venv
source .venv/bin/activate # Linux/macOS
# .venv\Scripts\activate # Windows
In capsule 05 you'll install redis-py right here.
This module's teaching philosophy
Three principles guide how you'll learn Redis here:
1. CLI first, Python after
Every concept is experienced first in redis-cli (where the feedback is immediate and visual) and then in Python code (where you apply it in production). If you go straight to Python, you lose the CLI's fast learning sandbox.
Example: You'll learn HSET user:1 name "Alex" in redis-cli before writing r.hset("user:1", "name", "Alex") in Python. By the time you get to the code, you already understand what's happening.
2. Data types with purpose, not by taxonomy
You're not going to memorize "these are Redis's data types" like a Wikipedia list. You'll learn the problem → the data type → why it fits:
- "I want to count requests per user" → strings with
INCR - "I want to cache a user profile with editable fields" → hashes with
HSET/HGET - "I want the top 10 most-viewed products" → sorted sets with
ZADDandZRANGE
3. Real backend use cases
No example will be "suppose you have a collection of Pokémon." Every data type is taught with a real backend use case: caching API responses, user sessions, rate limiting, leaderboards, event logs. The examples come from the capstone project and from real applications you'll build.
Evidence of success
You'll know you've mastered this module when you can, without consulting references:
- Bring up Redis with Docker in under a minute
- Run
redis-cli pingand get backPONG - Create, read, update, and delete data across the 5 data types from the CLI
- Connect
redis-pyfrom Python and do the same operations - Articulate when to use each data type with a real example (not just "strings are for X")
- Complete the Redis Explorer mini-project exercising the 5 data types
If by the end of the 5 capsules you're still hesitating over "do I use a hash or a set for this?", redo capsule 04. It's the foundation the rest of the guide assumes.
How to use the next capsules
Capsules 02-05 are written to be executed, not just read. That means:
-
Keep a terminal with
redis-cliopen while you read. Paste every command you see. Look at the output. Modify it. Break it on purpose to see what errors Redis returns. -
Don't copy the code from the capsules — type it. There's a huge difference between copying
HSET user:1 name Alexand typing it. Your brain retains far more when your fingers move. -
Solve the exercises without looking at the solution first. Every technical capsule (02-04) has 4-6 exercises with solutions inside
<details>. Try the exercises first. If you're stuck for more than 15 minutes, open the solution, understand it, close the<details>, and redo the exercise without looking. -
Complete the mini-project before moving on to module 2. The Redis Explorer isn't optional. It's where you consolidate the 5 data types in real code.
Resources to get started
Official documentation (keep these tabs open during the module)
- Redis Documentation — Complete official docs. When you're unsure of a command's exact syntax, look here
- Redis Commands Reference — Full reference for ALL the commands, organized by data type
- Redis Data Types Tutorial — The official tutorial for the 5 data types with examples
- redis-py Documentation — The official Python client. We'll use this in capsule 05
To understand Redis from another angle
- Redis University: RU101 (Introduction to Redis) — Free official course, 4-6 hrs. It complements this guide with the Redis team's perspective
- Redis in Action (Josiah Carlson) — A free HTML book from the Redis team. Deep but dense
For your workspace
- Docker Desktop — If you don't have Docker yet
- TablePlus or RedisInsight — GUI clients for Redis (optional, but useful for visualizing data while you learn)
Before moving on to capsule 02
Take 10 minutes to do these three things:
-
Verify Docker: Open your terminal and run
docker run hello-world. If you see "Hello from Docker!" you're ready. If not, install Docker Desktop before continuing. -
Create the folder structure:
mkdir -p ~/projects/redis-guide/module-01-fundamentals/redis-explorer cd ~/projects/redis-guide/module-01-fundamentals -
Verify Python: Run
python --version(orpython3 --versionon Mac/Linux). It should be 3.10 or higher. If not, update Python or use pyenv to get a modern version.
Once these three steps pass, you're ready to start Redis for real.
Summary
In this capsule you understood:
- Why Redis matters: it keeps your API from repeating unnecessary queries, it supports atomic concurrency, and it survives restarts where a Python dict doesn't
- Redis isn't just a cache: it's a versatile tool with 5 data types, each optimized for specific use cases
- Redis doesn't replace PostgreSQL: it's an intermediate layer that absorbs load; PostgreSQL is still the source of truth
- Every data type has a destination in this path: strings for simple caching and counters, hashes for objects and sessions, lists for queues, sets for unique collections, sorted sets for rate limiting and rankings
- The module's philosophy: CLI first (a fast sandbox), Python after (production), always with real backend use cases
In capsule 02 you install Redis with Docker, connect with redis-cli, and learn the fundamental navigation and exploration commands. It's the first time you'll touch Redis with your own hands.
What's next?
Capsule 02: Installation and redis-cli — Bringing up Redis with Docker, connecting redis-cli, exploration commands (PING, INFO, KEYS, TYPE, MONITOR), and understanding Redis's client-server model.
If you have Docker working and the folder structure created, you're ready. Let's go.