Caching: speed up without serving stale data
Where to put it
In the browser, in the delivery network, in the app server, and in a shared key-value store. Each layer is faster and harder to invalidate. Most pain comes from layers piled up with no single deciding policy.
Three strategies
Time expiry. Simple and enough for most cases. Choose a short time for data that changes and a logical one for stable data.
Event invalidation. On write, delete the key. More precise, and requires remembering every place that writes.
Background refresh. Serve an existing copy and refresh in parallel — a smooth experience, in exchange for a slightly stale value.
Pitfalls
A cache key that doesn't include the user's identity or permissions — so one person's data is served to another. Mass expiry at the same moment that brings down the database. And a cache that papers over a slow query instead of fixing it.
Going deeper
Measure cache hit ratio and also the response time without it. A cache that hides a problem instead of solving it will blow up at the worst moment — when it empties, and the system is exposed to full load it was never tested against.