Redis installed since Lesson 00 (
redis-server);pip install redisin your venv. Do not look at solutions.md before submitting.
Exercise 1 — Availability cache
- Wrap your availability query (09) with Redis
setex/get(TTL 10s, keyavail:{event_id}). Measure with\timing/queries: first call vs second. - Reserve a seat and check that the cache serves stale data for ≤10s. Is that acceptable for your business? Which TTL would you pick and why?
- Add invalidation: after a confirmed reservation,
DELETEthe key. Which problem does it solve? Which one does it introduce (stampede)?
Exercise 2 — Atomic rate limit
- Implement the "20 requests per IP per minute" limit with
INCR+EX 60on a middleware or a test view. - Demonstrate it: 25 requests from the same IP → the 21st receives 429.
- Discuss: why does
GET counter, if < 20: SET counter+1(without atomicity) let 35 requests through under concurrency?
Exercise 3 — Distributed gift-card lock
- With
r.lock("gift:TEST", timeout=5, blocking_timeout=2), two threads try to redeem the same card (a 0.2s sleep inside). What does the loser observe? - Remove the lock's
timeout=5and kill the process inside thewith(Ctrl+C in the thread complicates it; simulate withlock.release = crash). What happens to the card? Why is the TTL what prevents the eternal block? - Connect with Lesson 10: inside the lock, what else do you need for the redemption to be atomic against the DB?
Exercise 4 — JSONB with GIN
- Add
metadata JSONB DEFAULT '{}'to your Event model (a Lesson 11 migration, a safe one!) and the GIN index. - Insert 3 events with different
metadata({"venue_type": "stadium"}, etc.) and query with@>. Verify in EXPLAIN that it uses the GIN index. - Reflect: of those fields, which deserves a real column and which stays in JSONB?
Exercise 5 — Simulated stampede
- Empty the cache and fire 50 threads requesting availability at once. Count
len(connection.queries): how many hit the DB? - Simple fix: TTL with jitter (
10 + random(0,5)). Repeat and compare. (The regeneration lock gets formalized with Celery in Lesson 29.)
Submit
Paste numbers and trimmed code. With this you close the whole data module; next Lesson 13 — REST done right (APIs module).