0
0
FastAPIframework~20 mins

Caching strategies in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI endpoint with cache?

Consider this FastAPI endpoint using a simple in-memory cache dictionary. What will be the response when calling /data twice?

FastAPI
from fastapi import FastAPI
app = FastAPI()
cache = {}

@app.get('/data')
async def get_data():
    if 'value' in cache:
        return {'data': cache['value']}
    cache['value'] = 'cached result'
    return {'data': 'cached result'}
A500 Internal Server Error on both calls
B{"data": "cached result"} on first call, then same on second call
C{"data": null} on first call, then {"data": "cached result"} on second call
D{"data": "cached result"} on first call, then KeyError on second call
Attempts:
2 left
💡 Hint

Think about how the cache dictionary stores and returns the value.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses FastAPI's Depends for caching?

FastAPI allows dependency injection for caching. Which code snippet correctly uses Depends to inject a cache dependency?

A
from fastapi import Depends
cache = {}
async def get_cache():
    return cache

@app.get('/item')
async def read_item(cache=Depends(get_cache)):
    return {'cache_size': len(cache)}
B
from fastapi import Depends
cache = {}
async def get_cache():
    return cache

@app.get('/item')
async def read_item(cache=get_cache()):
    return {'cache_size': len(cache)}
C
from fastapi import Depends
cache = {}
async def get_cache():
    return cache

@app.get('/item')
async def read_item(cache=Depends(get_cache())):
    return {'cache_size': len(cache)}
D
from fastapi import Depends
cache = {}
async def get_cache():
    return cache

@app.get('/item')
async def read_item(cache=Depends):
    return {'cache_size': len(cache)}
Attempts:
2 left
💡 Hint

Remember that Depends takes a callable without calling it.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI cache implementation cause stale data?

Given this code snippet, why might the cache return outdated data?

FastAPI
from fastapi import FastAPI
app = FastAPI()
cache = {'data': 'old'}

@app.get('/update')
async def update_data():
    cache['data'] = 'new'
    return {'status': 'updated'}

@app.get('/data')
async def get_data():
    return {'data': cache['data']}
ABecause the cache dictionary is global and shared, updates affect all requests immediately, so no stale data occurs
BBecause the cache is not thread-safe, concurrent requests may see stale or inconsistent data
CBecause the cache is updated only on /update, if /data is called before /update, it returns old data
DBecause FastAPI automatically clears cache dictionaries after each request, causing stale data
Attempts:
2 left
💡 Hint

Think about when the cache value changes and when it is read.

🧠 Conceptual
advanced
2:00remaining
Which caching strategy best fits FastAPI for data that changes every minute?

You have an API endpoint that returns data updated every minute. Which caching strategy is best to reduce load but keep data fresh?

AUse client-side caching only, relying on browser cache headers
BUse no caching to always return fresh data, ignoring performance
CUse a cache that never expires and manually clear it when data changes
DUse in-memory cache with a time-to-live (TTL) of 60 seconds to expire cached data automatically
Attempts:
2 left
💡 Hint

Think about balancing freshness and performance with automatic expiration.

state_output
expert
2:00remaining
What is the output after these FastAPI cache updates?

Given this FastAPI app, what is the output of calling /get after calling /set/5 and then /set/10?

FastAPI
from fastapi import FastAPI
app = FastAPI()
cache = {}

@app.get('/set/{value}')
async def set_value(value: int):
    cache['num'] = value
    return {'status': 'set', 'value': value}

@app.get('/get')
async def get_value():
    return {'num': cache.get('num', 0)}
A{"num": 10}
B{"num": 5}
C{"num": 0}
DKeyError exception
Attempts:
2 left
💡 Hint

Consider the order of calls and how the cache dictionary updates.