Store copies of hot data in a fast layer to reduce latency and database load.
READ : client -> app -> cache? yes: return
no: app -> DB, populate cache, return
WRITE : client -> app -> DB -> (invalidate cache or write-through)
asyncio.Lock, Redis SETNX) on the miss path.key:<i> for i in 0..3).async def get_user(uid):
cached = await redis.get(f"user:{uid}")
if cached: return json.loads(cached)
user = await db.fetch("SELECT * FROM users WHERE id=$1", uid)
await redis.set(f"user:{uid}", json.dumps(user), ex=300)
return user
Post-it notes on your desk vs the filing cabinet: a single source of truth (cabinet) with a fast cache (post-it).
Interview tip: State the write strategy by name (read-through, write-behind, etc.) and the invalidation policy.