Build a Faster Flask API with Redis Caching
Resumen
Pixel shares Part 3 of a series on building a Flask REST API that filters Rick & Morty character data and caches results in Redis. The guide explains deterministic cache keys, avoiding duplicate fetches, reducing latency, and serving stale data when the upstream API is unavailable.
DEV Community
Part 3: Building the App & Caching for Data Latency
Part 3: Building the App & Caching for Data Latency Navigation: ← Part 2: Optimizing Dockerfiles with Multi-Stage Builds | Part 4: Full-Cycle CI/CD — From Local Hooks to AWS ECR → Key Takeaways Platform engineers: Standardise your cache connection patterns. Always use a middleware to automatically generate deterministic cache keys for GET requests. SREs on-call: Stop silent cascading failures. If your upstream provider goes down, a stale cache fallback prevents your app from crashing. First-timers: Understand the app flow first. Don't fetch the same data twice. If a user asks for character data, fetch it once, store it in Redis, and serve subsequent requests instantly. How Our Application Works Our application is a Python Flask REST API. Its primary job is to aggregate data from the external Rick & Morty API (https://rickandmortyapi.com/api). Specifically, our business logic filters the data to return characters matching strict criteria: species=human, status=alive, and origin=earth. Here is the core structure you will find in app.py: from flask import Flask
from src.cache.detection import CacheMiddleware
from config import get_cache_backend
app = Flask(name)
Initialize our custom cache middleware
cache_backend = get_cache_backend()
cache_middleware = CacheMiddleware(app, cache_backend)
We keep it simple, but we rely heavily on an external upstream API. This introduces a major operational risk: Data Latency. The Mental Model When a user requests data, the naive flow is: User -> Our Flask App -> Upstream Rick & Morty API -> Our Flask App -> User. This has two severe problems: High Latency: We pay the network penalty of reaching out to the public internet on every single request (often 300ms+). Rate Limiting & Outages: If the upstream API rate-limits us or goes offline, our application fails immediately. To solve this, we introduced the CacheMiddleware backed by Redis. Here is the architectural flow of a request: sequenceDiagram
participant U as User (curl)
participant F as Flask API (Local)
participant R as Redis Cache (Local)
participant API as Rick & Morty API (Public)
U->>F: GET /api/v1/characters
F->>R: Check Cache
alt Cache Hit (Latency: ~2ms)
R-->>F: Return Cached JSON
F-->>U: HTTP 200 (Header: X-Cache: HIT)
else Cache Miss (Latency: ~300ms+)
R-->>F: Null
F->>API: Fetch data over Internet
API-->>F: JSON Response
F->>R: Save data to Redis (Hydrate with TTL)
F-->>U: HTTP 200 (Header: X-Cache: MISS)
end The new mental model is a layered check: The Cache Check — The middleware asks Redis: "Do we have this data?" (Latency: ~2ms). The Upstream Fetch (Cache Miss) — If Redis says no, we fetch from the public API (Latency: ~300ms). The Cache Hydration — We store the upstream response in Redis with a Time-To-Live (TTL). The Client Response — We return the data to the user and inject headers showing X-Cache: HIT or MISS. Walkthrough: Implementing the Cache Detector If you look at src/cache/detection.py in our repository, you will see how we intercept requests before they hit our business logic. 1. Generating Deterministic Cache Keys We must uniquely identify every request to store it correctly. We generate a cache key based on the HTTP method, endpoint, and query parameters: class CacheDetector:
def generate_cache_key(self, endpoint, method, query_params=None):
"""Generate deterministic cache key from request"""
# Example output: "GET:get_characters:species=human&status=alive"
key_base = f"{method}:{endpoint}"
if query_params:
sorted_params = "&".join(f"{k}={v}" for k, v in sorted(query_params.items()))
key_base = f"{key_base}:{sorted_params}"
# We hash the key to ensure it is always a safe length for Redis
return hashlib.md5(key_base.encode()).hexdigest()
- The Middleware Interception The CacheMiddleware hooks into Flask's before_request and after_...

Powered by Readybot.io