Practice & Assessment
Test your understanding of Caching Patterns
Multiple Choice Questions
5In cache-aside, what happens on a cache miss?
Which eviction policy should you use when all your cached data also lives in a database?
What is a cache stampede?
In write-through caching, when is the database updated?
Which command creates a Redis lock atomically, preventing a race condition?
Coding Challenges
1Cache-Aside API Endpoint
Implement a getArticle(id) function using cache-aside. On a cache miss, simulate a 200 ms database call (setTimeout). Cache the result in Redis for 2 minutes under the key article:<id>. Log 'CACHE HIT' or 'CACHE MISS' on each call. Call getArticle with the same id 5 times in sequence and verify only the first call triggers a miss. Input: integer article id. Output: article object and cache status log. Estimated time: 20 minutes.
Mini Project
Multi-Layer Caching API
Build a REST API (Express.js or FastAPI) with three endpoints: GET /products/:id (cache-aside, TTL 5 min), PUT /products/:id (write-through โ update DB mock and Redis simultaneously, then return updated product), DELETE /products/:id (invalidate cache key on delete). Add a GET /products/:id/stats endpoint that returns cache_hits and cache_misses counters stored as Redis Strings with INCR. Use tag-based invalidation: each product key is also tracked in a tag Set product-category:<categoryId>. Add a POST /admin/invalidate-category/:id endpoint that deletes all product cache keys in that category. Demonstrate all five endpoints working together.
