Skip to Content

Store copies of hot data in a fast layer to reduce latency and database load.

Data Flow (cache-aside, the most common)

READ  : client -> app -> cache? yes: return
                              no:  app -> DB, populate cache, return
WRITE : client -> app -> DB  -> (invalidate cache or write-through)

Four write strategies

Pitfalls

Code (Python cache-aside)

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

Analogy

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.

Advertisement