Engineering notes
Caching Business Data: Define Freshness Before Speed
Choose what may be stale, how changes invalidate cached results and what happens when the cache disappears during a busy period.
A cached catalogue can make browsing responsive while an outdated account price creates a customer dispute. These are different freshness requirements. In a hypothetical ordering portal, the useful question is not whether Redis should be added; it is which decisions may use an older observation and which must return to the authoritative source.
Classify data by the consequence of staleness
List the reads that are expensive and the business decisions they support. A product description, stock promise and account credit decision have different consequences if stale. Agree on an acceptable freshness window for each, including whether the interface must show an observation time.
Cache a derived view when it can be rebuilt from an authoritative source. Do not let the cache become an undocumented second database. For a checkout, revalidate the final price and availability at the commitment boundary according to the product contract, even if browsing uses a cached projection.
Generated editorial illustration; not a record of a live system.
Design keys around the full context
A cache key must represent every input that changes the result: organization, language, currency, permissions, filters and representation version where relevant. Omitting the account from a negotiated-price key is a correctness and confidentiality problem, not a minor cache miss.
Keep the key scheme understandable and version it when the stored shape changes. Define how empty or missing results are cached and for how long. An absent record should not remain invisible after a legitimate creation because a negative cache entry was forgotten.
Specify invalidation and its failure mode
For each write, identify the cached views it affects. Decide whether to delete, replace or version those views after the authoritative transaction commits. If invalidation is asynchronous, record it durably and expose delayed work. A successful database write followed by a failed cache update needs a recovery path.
Expiry can limit staleness but does not make an incorrect invalidation rule correct. Concurrent readers can repopulate an old value while a write is completing. Test this race and select a coordination approach appropriate to the consequence, rather than assuming one deletion solves every case.
Prepare for a cold or unavailable cache
A cache outage can suddenly send all traffic to the database. Set request budgets and bound concurrent rebuilding. Use coalescing or controlled refresh where suitable so many callers do not rebuild the same expensive value simultaneously. A stale response is acceptable only if the freshness contract permits it.
Test startup, eviction, a full flush and a slow cache connection. Decide whether each feature fails open, returns a limited response or blocks the action. Avoid long cache timeouts that consume the request budget before the source is even queried. Cache availability should not silently determine whether an invalid business action is accepted.
Measure correctness alongside hit rate
Record source latency, cache latency, misses, rebuild work and observed stale-result incidents. Segment by important key families without logging personal identifiers. A high hit rate can coexist with wrong data, oversized values or expensive invalidation.
Compare the same workload before and after a change, including cold-cache behaviour. Rehearse a price update and a permission change while requests are in flight. Keep the simplest cache that meets the agreed freshness and load requirements; a numerical speedup becomes a claim only after it is measured.
Worked example and implementation boundary
A fictional account price changes from 100 to 110 while a reader is rebuilding a cached catalogue. If that reader fetched the old value before the commit and writes it after invalidation, the stale price can reappear. A versioned projection or another deliberately chosen coordination rule must address that race. Browsing may tolerate a declared stale window, while final order commitment revalidates the authoritative price and explains a change to the buyer.
Trade-offs and limitations
Short expiry reduces the possible stale window but increases rebuilding load. Event-driven invalidation can be faster but adds a delivery and recovery path. Neither choice removes the need to define the permitted consequence of an older observation.
Common pitfalls
Do not treat expiry as proof of correctness, omit account or permission context from the key, or allow a cache outage to send unbounded rebuild work to the database. A high hit rate can still serve the wrong price efficiently.
Key takeaways
Classify freshness by business consequence, specify keys and invalidation, and rehearse cold-cache behaviour. Measure stale-result incidents alongside latency and hit rate. Revalidate important commitments at the authoritative boundary.
References and further reading
Bitcave — Scalable Software Engineering: Principles and Patterns
Related reading and implementation support
Related content
Software Modernisation and Performance Optimisation
Architecture review, database tuning and focused software improvements for applications that have become slow, fragile or difficult to change.
Prevent Overselling Across Your Store and Marketplaces
Design stock reservations, order states and reconciliation so two sales channels cannot quietly promise the same unit to different customers.
Define the implementation boundary
Discuss cache invalidation in the context of your current systems, decision owners and exception paths, then define a bounded first step.
Discuss your project
