Application patterns with Redis
You use Redis in Python applications as a cache, session store, counter, queue and leaderboard, and learn each pattern’s pitfalls and proper key design.
Level 201 · 16 lessons · 15 practice labs
Lessons
Caching
- Connecting to Redis from Python
- The cache-aside pattern
- Invalidating the cache
- Cache stampedes and how to prevent them
- How to store values: JSON, hashes, compression
- Short task: add a cache to a slow API
State and counters
- A session store
- Counters and simple analytics
- Counting unique visitors
- Distributed locks and their pitfalls
- Short task: feature flags in Redis
Queues and rankings
- A simple job queue
- Delayed jobs with sorted sets
- A game leaderboard
- Key design and namespaces
- Capstone: the Redis layer of a ticket sales site
Practice labs
- Put a cache in front of a slow query: Write a Python function that looks up product details in Redis first and falls back to the database, then caches them.
- Stop showing stale prices after an update: Find the bug that leaves the old price cached after an update and add invalidation on write.
- Prevent a cache stampede: Show hundreds of queries hitting the database when a popular key expires and fix it with a lock or early refresh.
- Store an object as JSON: Write a user profile as JSON text, read it back and note the cost of updating one field.
- Choose between JSON and a hash: Store the same data as JSON and as a hash, compare memory and partial-update speed and decide.
- Halve memory by compressing large values: Store large report caches compressed and measure the memory saving against the CPU cost.
- Keep sessions in Redis: Keep a small web app's sessions in expiring hashes and confirm they are deleted on logout.
- Count unique visitors with HyperLogLog: Count daily unique visitors with both a set and HyperLogLog and compare memory and error margin.
- Add a log-out-of-all-devices feature: Keep an index of each user's sessions and write a feature that ends them all with one command.
- Toggle a feature flag from Redis: Write code that switches a new checkout screen on and off with a flag in Redis and try it without restarting.
- Stop a job running twice with a lock: Make an invoicing job that starts on two servers at once run only once with a SET NX PX lock.
- Fix a bug caused by an expiring lock: Fix code that deletes someone else's lock after its own expires by using token-based locks and safe release.
- Build a simple job queue: Write a producer that pushes e-mail jobs to a list and a worker that takes them with BRPOP.
- Schedule delayed jobs with a sorted set: Put reminder jobs in a sorted set by timestamp and write a loop that moves due ones to the queue.
- Show neighbours on a game leaderboard: Write a leaderboard service that returns a player's rank plus the five above and below in one go.
About this course
You use Redis in Python applications as a cache, session store, counter, queue and leaderboard, and learn each pattern’s pitfalls and proper key design.
What you will learn
- Caching
- State and counters
- Queues and rankings
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal in the browser, connected to a private lab network. Nothing to install.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.