Backend caching: choose cache-aside, expiry, and invalidation rules
A cache keeps frequently read values in faster storage to reduce repeat-read latency or load on the source. In cache-aside, the application checks the cache, loads misses from the source, and invalidates the related entry after a write. A cache is not the source of truth, and TTL or invalidation policy can allow stale values. This guide covers application caches in front of a database, not vendor-specific setup.
How does cache-aside work?
The app looks up a key in the cache.
On a hit, it returns the value.
On a miss or expiry, it reads the source datastore.
It stores the result in the cache and returns it.
On writes, update the source first, then delete the corresponding cache key; the next read repopulates it.
The app manages this coordination, and only requested items are loaded. A reader can briefly see old data before invalidation, or a miss after deletion. Microsoft documents the source-first, then invalidate order. (Microsoft Cache-Aside, AWS Caching strategies)
What should you cache and how should you set TTL?
Consider data read often, changed less often, and allowed a bounded period of staleness. Frequently changing data or keys with few hits may gain little. Sensitive data or values used for security decisions may not belong in a shared cache; read the source or enforce user/tenant boundaries carefully. (Microsoft Cache-Aside)
Time to live (TTL) is how long an entry remains before expiry. Too short a TTL creates repeated misses; too long can serve stale values longer. Set it by data change rate, acceptable staleness, source-read cost, and whether writes can invalidate the key. A TTL bounds age but does not guarantee immediate consistency, especially with external writers.
How do you handle writes, concurrent misses, and cache failure?
Update the source successfully, then invalidate its cache key. Clearing the cache first can let a miss reload the old source value. Plan for failures between the two operations, retries, and short TTLs; cache-aside does not guarantee full consistency. ([Microsoft Cache-Aside](https://learn.microsoft.com/en-gb/azure/architecture/patterns/cache-aside))
When many requests miss on a popular expired key, they can all query the source (a cache stampede). Consider single-flight loading, locks, or refresh before expiry based on workload and consistency needs. This guide does not validate a particular locking implementation. ([Redis cache-aside](https://redis.io/docs/latest/develop/use-cases/cache-aside/))
Decide whether a cache outage falls back to the source or returns a controlled error. Unbounded fallback can overload the database; use timeouts, concurrency limits, circuit breaking, and capacity planning. Actual safety depends on source capacity. ([Microsoft Cache-Aside](https://learn.microsoft.com/en-gb/azure/architecture/patterns/cache-aside))
Private in-process caches can diverge between instances. Shared caches unify copies but add network and operational dependencies.
How can you tell if caching helps?
Before rollout, compare a no-cache baseline with the same inputs using the cache. Measure hit and miss rates, source calls, read latency, memory use, and errors. If hits are rare or source calls do not fall, revisit keys, TTL, and data selection. Test that the feature still works from the source when cache nodes are emptied or replaced. AWS notes that lazy loading can continue after node loss but causes extra latency and source reads while warming. (AWS Caching strategies)
If removing the cache makes results incorrect or loses durable data, the cache is being treated as the source of truth. Adopt caching only after confirming a real latency or load problem and deciding acceptable staleness and failure behavior.