Redis Caching
See the impact of Redis caching with an interactive dashboard that compares database vs cached response times.
Time: ~15 minutes Difficulty: Intermediate
What You Will Learn
Section titled “What You Will Learn”- The cache-aside (lazy-loading) pattern
- Redis as a Kubernetes-native cache with LRU eviction
- Measuring cache hit rates and speedup factors
- How TTL and eviction policies work in practice
Architecture
Section titled “Architecture”Browser --> FastAPI App --> Redis (cache layer) | v PostgreSQL (data layer)The app checks Redis first. On a cache miss, it queries PostgreSQL, stores the result in Redis with a 60-second TTL, and returns it. Subsequent requests hit the cache and return in under 1ms.
Deploy
Section titled “Deploy”Step 1: Build the application image
Section titled “Step 1: Build the application image”eval $(minikube docker-env)docker build -t cache-demo-app:latest -f demos/redis/Containerfile demos/redis/If using Podman:
podman build -t cache-demo-app:latest -f demos/redis/Containerfile demos/redis/
Step 2: Apply the manifests
Section titled “Step 2: Apply the manifests”kubectl apply -f demos/redis/manifests/namespace.yamlkubectl apply -f demos/redis/manifests/postgres.yamlkubectl apply -f demos/redis/manifests/redis.yamlkubectl apply -f demos/redis/manifests/app.yamlStep 3: Wait for pods
Section titled “Step 3: Wait for pods”kubectl get pods -n redis-demo -wWait until all three pods (postgres, redis, app) show Running and 1/1 ready.
Step 4: Access the dashboard
Section titled “Step 4: Access the dashboard”minikube service cache-demo-app -n redis-demoOr use port-forwarding:
kubectl port-forward svc/cache-demo-app 8000:8000 -n redis-demoOpen http://localhost:8000 in your browser.
Using the Dashboard
Section titled “Using the Dashboard”The dashboard provides three actions:
| Button | What It Does |
|---|---|
| Run Single Comparison | Queries the database directly, then queries with cache. Shows both response times side by side. |
| Run Load Test (10x) | Runs 10 rounds of database vs cache comparisons. Builds a bar chart of response times. |
| Flush Cache | Clears Redis and resets stats. Use this to start fresh. |
What to look for:
- First request (cache miss): ~300-400ms (database query + artificial delay)
- Cached request (cache hit): ~0.5-2ms
- Typical speedup: 100-900x
The stats bar at the top shows cache hit rate, Redis memory usage, and number of cached keys.
What is Happening
Section titled “What is Happening”manifests/ namespace.yaml # redis-demo namespace postgres.yaml # PostgreSQL with 5,510 rows of seed data redis.yaml # Redis 7 with 64MB memory, LRU eviction app.yaml # FastAPI app (NodePort service)
src/ main.py # Cache-aside logic, 5 API endpoints requirements.txt # Python dependencies templates/ dashboard.html # Interactive single-page dashboard
Containerfile # UBI9 + Python 3.11init.sql # Database schema: categories, products, reviewsAPI endpoints:
| Endpoint | Description |
|---|---|
GET /api/categories | Category stats with COUNT, AVG, MIN, MAX aggregations |
GET /api/products?q=... | Text search across product names and descriptions |
GET /api/top-products | Top-rated products using window functions |
POST /api/flush-cache | Clear Redis and reset stats |
GET /api/stats | Cache hit/miss rates, Redis memory, key count |
Experiment
Section titled “Experiment”-
Run a load test and observe the bar chart. Red bars (database) should tower over green bars (cache).
-
Wait 60 seconds after a load test. The TTL expires, and the next request will be a cache miss again.
-
Check Redis directly:
Terminal window kubectl exec -it deploy/redis -n redis-demo -- redis-cli> KEYS *> INFO memory> TTL "categories:stats" -
Watch application logs:
Terminal window kubectl logs -f deploy/cache-demo-app -n redis-demo
Cleanup
Section titled “Cleanup”kubectl delete namespace redis-demoFurther Reading
Section titled “Further Reading”See docs/deep-dive.md for a detailed explanation of the cache-aside pattern, cache key design, TTL strategy, connection pooling, Redis LRU eviction, the database queries, and when to use (or not use) Redis caching.
Next Step
Section titled “Next Step”Move on to YAKD to visualize your cluster with a lightweight dashboard.