Cache Security, Debugging & Production Incidents
Caching failures are dangerous because the application can look healthy while returning the wrong data.
The worst incidents are often not obvious crashes. They are:
stale data
cross-tenant data
wrong permissions
inconsistent regions
unexpected source load
read-your-own-write failures
Treat cache correctness as a production reliability concern.
Incident taxonomyβ
Staleness incidentβ
Users see data older than the product contract allows.
Isolation incidentβ
One user's or tenant's cached result is visible to another.
Invalidation incidentβ
A mutation succeeds but relevant cache entries remain reusable.
Stampede incidentβ
Many requests regenerate the same expensive entry simultaneously.
Divergence incidentβ
Different instances or regions serve different cache generations.
Cost incidentβ
Low hit ratio or excessive invalidation causes more compute/network spend than caching saves.
First rule: identify the layerβ
Before changing code, classify the stale value:
source database
upstream API cache
React request memoisation
Next.js fetch/Data Cache
use cache entry
client Router Cache
CDN/edge cache
browser HTTP cache
Do not run a blanket purge until you know which layer is wrong.
Stale-data debugging workflowβ
- capture the exact URL, tenant, user, and timestamp
- query the authoritative source
- compare soft navigation and hard reload
- compare fresh browser session
- inspect server cache hit/miss if observable
- inspect tag/path invalidation logs
- inspect CDN or proxy headers
- compare multiple instances/regions
- reproduce with production build
- identify the earliest layer where the wrong value appears
Cross-tenant cache leakβ
A catastrophic design:
async function getDashboard() {
'use cache'
return db.invoice.findMany({
where: { tenantId: currentTenantId },
})
}
where currentTenantId is not safely represented in cache identity.
The result can become reusable beyond its intended scope.
Safer design:
trusted request tenant
β
authorize membership
β
explicit tenantId argument
β
cache only if tenant-scoped reuse is intended
Even then, evaluate whether sensitive tenant data belongs in a persistent/shared cache.
Cache keys are security boundariesβ
A cache key determines which requests can share one result.
If the key omits a dimension that changes authorization or content, data can leak.
Potential missing dimensions:
- tenant
- locale
- currency
- plan tier
- feature variant
- permissions
- resource version
Do not add dimensions blindly either; high-cardinality caches may become ineffective or expensive.
Do not cache authorization as truth carelesslyβ
Permission decisions can change independently from content.
Example:
user removed from organization
β
old permission cache still says allowed
β
protected data returned
Authorization caches require especially careful lifetime, invalidation, and revocation semantics.
For many applications, checking authoritative permission state dynamically is simpler and safer.
Read-your-own-write failureβ
Symptom:
user edits record
success message appears
redirects to detail page
old value still shown
Investigate:
- was
updateTagused where immediate expiration was required? - was only
revalidateTag(..., 'max')used, allowing stale content? - is the detail page using a different tag?
- is client Router Cache reusing old route data?
- is a CDN/upstream cache still stale?
Consistency requirement determines the API choice.
Invalidation fan-outβ
A single mutation may affect multiple views:
product:p42
products
category:lighting
search:index
homepage:featured
If you invalidate only the detail entry, list views may remain stale.
The solution is not always βinvalidate everything.β
Model affected domains explicitly.
Cache stampedeβ
When one popular cache entry expires:
thousands of requests
β
all see miss/stale hard-expiry
β
origin overloaded
Mitigations can include:
- stale-while-revalidate
- single-flight/coalescing
- staggered expiration
- background refresh
- rate limiting
- upstream capacity planning
revalidateTag(..., 'max') can be preferable to immediate hard expiration for public high-traffic data when slight staleness is acceptable.
Invalidation stormβ
The inverse problem:
mutation every second
β
revalidate broad tag/path every second
β
cache never becomes useful
Fix domain granularity or reduce cache scope.
A cache with a 1% hit ratio and heavy invalidation may be negative value.
Region divergenceβ
Symptom:
user in region A sees new product
user in region B sees old product
Possible causes:
- region-local in-memory caches
- delayed distributed invalidation
- CDN propagation
- multi-primary data replication delay
- version skew during deployment
Do not assume Next.js cache is the only distributed system involved.
Deployment version skewβ
During rolling deploys, old and new application versions may coexist.
If cache keys or serialized output formats change incompatibly, one version can read data generated by another.
Strategies:
- deployment/version namespace in keys where needed
- backwards-compatible cache payloads
- purge on incompatible schema changes
- short overlap windows
Treat cache format like a data contract.
Sensitive loggingβ
Debug cache incidents without logging:
- session cookies
- auth headers
- personal data
- full database rows
- secret cache keys containing raw credentials
Prefer safe metadata:
cache namespace
resource ID
hash of key where appropriate
tenant ID if allowed
hit/miss
age
tag list
revalidation reason
Observability metricsβ
Useful cache metrics:
hit ratio
miss ratio
stale-hit ratio
origin latency
cache lookup latency
entry age
eviction count
revalidation count
invalidation failures
source request count
A high hit ratio is not automatically good if the cache serves incorrect data.
Correctness metrics matter too.
Cache correctness testsβ
Test scenarios such as:
Tenant isolationβ
request tenant A
request tenant B
assert no shared private result
Mutation freshnessβ
read old
mutate
read immediately
assert expected consistency
Tag fan-outβ
invalidate product
assert detail/list/category behavior
Expirationβ
Use controlled clock/test abstractions where possible rather than long real waits.
Multi-instance behaviorβ
If production uses shared cache, integration tests should exercise the handler contract rather than only process-local memory.
Development is not productionβ
Dev has special HMR behavior and pages are rendered differently from production.
A cache incident must be reproduced with:
next build
next start
or the real deployment environment before conclusions are final.
Disable caching as a diagnostic, not a permanent reflexβ
Temporarily switching to dynamic/no-store can help prove a cache is involved.
But leaving everything uncached can:
- increase latency
- overload databases
- increase API bills
- remove stable shell performance
After diagnosis, fix the specific cache contract.
Security review checklistβ
For each persistent/shared entry:
- ownership is explicit
- key dimensions are complete
- authorization is not bypassed
- sensitive fields are minimized
- tenant/user scope is correct
- invalidation handles permission/content changes
- retention is acceptable
- remote storage compliance is reviewed
- failure fallback does not leak data
Production runbookβ
When a cache incident occurs:
1. contain
2. classify layer
3. establish source truth
4. determine scope
5. invalidate narrowly if possible
6. disable problematic cache if necessary
7. protect origin from stampede
8. restore correct policy
9. add regression test/metric
10. document root cause
For a suspected data leak, prioritize containment and access/security review over performance preservation.
Interview questionsβ
What makes a cache key a security boundary?
Requests sharing the same key can receive the same stored result, so every dimension that affects allowed output must be represented or kept outside shared caching.
What is a cache stampede?
Many requests regenerate the same expired/missing entry simultaneously, potentially overloading the origin.
Why can high cache hit ratio still be bad?
If the cache serves stale or unauthorized data, efficiency only makes the correctness bug larger.
How would you debug βsoft navigation stale, hard reload freshβ?
Start with the client Router Cache/prefetch layer before blaming the server Data Cache.
Exerciseβ
Write an incident postmortem for this scenario:
organization admin removes a user
removed user can still view one cached dashboard for 10 minutes
Include root cause hypotheses, containment, key/authorization analysis, invalidation design, test plan, and observability improvements.