Caching a public article can reduce repeated database work, but invalidation races can return old content after an edit. The risk appears when a slow request begins before the update, finishes after the cache is cleared, and writes its old response back into the cache.
Define the cache key and lifetime
Key the cache by the full representation inputs: canonical slug, locale, and any variant that changes the response. Do not include authorization-sensitive data in a public cache entry. Use a finite TTL as a recovery mechanism, but do not mistake expiration for correct invalidation.
For race-sensitive content, attach a content version or updated timestamp to the cache key. After an edit, new readers request the new version, while an old in-flight write can only populate the old key. Another option is to coordinate invalidation with a generation value. Choose the simplest strategy compatible with the cache and deployment model.
Keep the source authoritative
A cache miss should read from the authoritative store. If data is missing or a cache service is unavailable, decide whether the request can safely continue without cache. Avoid returning stale private data as a fallback. In multi-instance deployments, invalidation must reach every instance or use a shared cache.
Test the race explicitly: pause an old read, update the record, invalidate, allow the old read to finish, then verify the next request returns the new version. Also test deletion, slug changes, cache outages, and concurrent updates. Observe hit rate and stale-response reports. Caching is a consistency decision as much as a performance technique.
Trace the stale write through both caches
Suppose a reader starts fetching revision 4, an editor commits revision 5, and the application invalidates the request key. If the first request finishes afterward and writes revision 4 back under the original key, the next reader can see the old text. Keying the cached representation by a trusted revision limits that in-flight response to its old key. The mechanism that selects the current revision must itself be fresh enough for the site's publication rules.
HTTP headers express a separate contract. In RFC 9111, no-store controls storage of the response, while private prevents a shared cache from storing it. Returning those headers after a publication change does not establish that an old response or an independently managed application entry has already been purged. Test edit, unpublish, and private visibility across the application cache and the HTTP layer. RFC 9111 HTTP caching
Worked example
Use this header on an authenticated draft response. A public article cache must use a separate key and invalidate on unpublish; this header does not purge an older shared cached response.
httpCache-Control: private, no-store




Community
Discussion
Loading comments…