Note
Who owns a cached read, and who can see it?
Cache Reads Need an Ownership Model
A cache is safe only when its key, invalidation owner, fallback path and pagination behavior are explicit.
Note
Who owns a cached read, and who can see it?
A cache is safe only when its key, invalidation owner, fallback path and pagination behavior are explicit.
A cache can make a read path feel instantaneous and still return the wrong answer. The risk is not the cache server; it is an unclear ownership model. Who owns a value, who may read it, who invalidates it, and what happens when it is absent are application decisions.
The reader outcome is a cache design that remains correct when tenants, filters, updates and pagination all meet the same endpoint.
The cache key must include every input that changes the visible result. In a tenant-aware application, that normally includes the tenant identity before the resource identity. It may also include the actor's permission scope, locale, feature variant, filter set, sort order and page size when those affect the response.
Leaving one of those dimensions out creates an accidental shared view. Prefixes that make keys easy to inspect are useful, but readable names alone do not make a key safe. Define the inputs from the response contract, then create the key from a canonical representation of those inputs.
Every cached value needs a clear answer to one question: which write knows it is stale? If no write owns invalidation, time-to-live becomes the only repair mechanism and readers can receive old data for an arbitrary period.
The writer that changes the source of truth is usually the right owner. It can invalidate or replace the affected representation after the durable write succeeds. For compound views, record the relationship explicitly: a change to one record may invalidate its detail key, a list key, and a summary key. This is less magical than broad prefix deletion and much easier to test.
The database or source-of-truth read is not a failure path. It is the normal fallback when the cache is cold, unavailable, expired, or deliberately bypassed. The response should retain the same authorization checks, ordering and shape as the cached response.
Do not make a cache hit more privileged than a miss. Authorize before deriving or reading the key when access itself is scoped. If cache infrastructure fails, degrade to the source of truth when the service can still safely serve the request, and make the degradation observable to operators.
A page is not just a resource plus a page number. Its contents depend on the sort order, filters, page size and cursor position. Reusing the first page's key for a later cursor is a correctness bug; reusing a cursor without its filter context is one too.
Caching individual pages can work for read-heavy, stable views, but invalidation cost grows with the number of possible cursors. Caching a normalized detail record while assembling pages from the database can be a better boundary. The right level is the one whose freshness and invalidation rules the team can state plainly.
When a popular key expires, many identical requests can all miss and rebuild the same value. A short randomized TTL, request coalescing, or a bounded lock can reduce that load. Each option has a recovery rule: locks need expiry, and coalesced callers need a timeout and a fallback.
Do not add those mechanisms by habit. First establish that the underlying read is expensive enough and concurrent enough to justify them. A simple cache with a correct fallback is safer than a sophisticated one that can block every reader.
Cache tests are strongest when they describe time and ownership:
Those tests turn caching from a performance decoration into an explicit part of the read model. The speed then comes from a system whose correctness has an owner.