Demonstrates advanced server-side caching using Redis to dramatically speed up repeated API queries. When a client requests data, the server first checks Redis. If a cached result exists (cache HIT), it returns instantly. If not (cache MISS), it simulates a slow database query, stores the result in Redis with a TTL, and returns it. Write operations (POST) automatically invalidate the relevant cache keys.
71-redis-caching/
main.py # FastAPI entry point with cached endpoints
database.py # Simulated slow database layer
redis_client.py # Redis connection and cache helper functions
config.py # Configuration constants (TTL, Redis URL)
requirements.txt # Dependencies
index.html # Unified frontend dashboard
README.md # This file
- Redis server running locally on port 6379 (default)
pip install -r requirements.txtredis-serveruvicorn main:app --reloadOpen index.html in your browser to test cached vs uncached queries.
// First request (CACHE MISS) - takes ~2 seconds
{
"source": "database",
"response_time_ms": 2003,
"data": [
{"id": 1, "name": "Laptop", "price": 999.99},
{"id": 2, "name": "Phone", "price": 699.99}
]
}
// Second request (CACHE HIT) - returns instantly
{
"source": "redis_cache",
"response_time_ms": 2,
"data": [
{"id": 1, "name": "Laptop", "price": 999.99},
{"id": 2, "name": "Phone", "price": 699.99}
]
}- Cache-Aside Pattern: Application checks cache before querying the database.
- TTL (Time-To-Live): Cached data automatically expires after a configurable duration.
- Cache Invalidation: Write operations flush stale cache keys to maintain data consistency.
- Serialization: Python dicts are serialized to JSON strings for Redis storage.