0
0
FastAPIframework~10 mins

Caching strategies in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Caching strategies
Client Request
Check Cache
Return
Store in Cache
Return Data
When a client requests data, the system first checks if the data is in cache (Hit). If yes, it returns cached data immediately. If not (Miss), it fetches fresh data, stores it in cache, then returns it.
Execution Sample
FastAPI
from fastapi import FastAPI
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache

app = FastAPI()

@app.on_event("startup")
async def startup():
    backend = InMemoryBackend()
    FastAPICache.init(backend, prefix="fastapi-cache")

@app.get("/items/{item_id}")
@cache(expire=10)
async def read_item(item_id: int):
    return {"item_id": item_id, "value": "fresh data"}
This FastAPI endpoint caches responses for 10 seconds to speed up repeated requests for the same item.
Execution Table
StepActionCache StateResultNotes
1Client requests /items/1EmptyCache missNo cached data for item 1
2Fetch fresh data for item 1Empty{"item_id":1,"value":"fresh data"}Data fetched from source
3Store data in cache with 10s expiry{"1": cached}Data cachedCache now holds item 1 data
4Return data to client{"1": cached}Response sentClient receives fresh data
5Client requests /items/1 again{"1": cached}Cache hitData served from cache
6Return cached data{"1": cached}Response sentFaster response from cache
7Wait 11 seconds (cache expires)EmptyCache expiredCached data removed after expiry
8Client requests /items/1 againEmptyCache missCache empty, fetch fresh data again
💡 Cache expires after 10 seconds, causing next request to fetch fresh data again
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 7After Step 8
cache{}{"1": cached}{"1": cached}{}{}
Key Moments - 3 Insights
Why does the first request cause a cache miss?
Because the cache is empty at the start (see execution_table step 1), no data is stored yet for the requested item.
How does the cache improve response time on repeated requests?
On repeated requests (step 5), the cache has stored data, so the system returns cached data immediately without fetching fresh data (step 6).
What happens when the cache expires?
After the expiry time (step 7), cached data is removed, so the next request (step 8) results in a cache miss and fresh data fetch.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache state at step 5?
A{"1": cached}
B{}
CEmpty
DExpired
💡 Hint
Check the 'Cache State' column at step 5 in the execution_table
At which step does the cache expire?
AStep 3
BStep 7
CStep 6
DStep 8
💡 Hint
Look for the step where 'Cache expired' is noted in the 'Result' column
If the cache expiry was set to 20 seconds instead of 10, what would change in the execution table?
AStep 7 would show cache still valid, no expiry
BStep 1 would be a cache hit
CStep 8 would be a cache hit instead of miss
DStep 3 would not store data
💡 Hint
Consider when the cache expires and how it affects step 8's cache miss/hit
Concept Snapshot
Caching strategies in FastAPI:
- Check cache before fetching data
- If cache hit, return cached data immediately
- If cache miss, fetch fresh data, store in cache
- Use decorators like @cache(expire=seconds) to set expiry
- Cache expiry removes old data to keep cache fresh
Full Transcript
This visual execution shows how caching works in FastAPI. When a client requests data, the system first checks if the data is in cache. If not found (cache miss), it fetches fresh data, stores it in cache with an expiry time, then returns it. Subsequent requests within the expiry time get data instantly from cache (cache hit). After expiry, cached data is removed, causing the next request to fetch fresh data again. This speeds up responses and reduces repeated work.