Caching a dynamic website safely means separating content that is shared from content that belongs to one user or request. This guide explains how to design cache keys, handle cookies and personalization, choose what to cache, and build repeatable tests that detect content leakage before it reaches visitors.
Overview
A cache stores a response so a later request can be served without repeating all of the application, database, and rendering work. That model works naturally for a public article or product category page, but it becomes more complicated when the response depends on identity, location, language, device, account status, or current pricing.
The central question is not simply, “Can this page be cached?” It is, “Which requests are allowed to share this response?” A cache key defines that boundary. It may include the URL, query string, host, selected headers, selected cookies, or a deliberately assigned variation such as a language or regional code. If a value changes the visible response but is missing from the cache key, the cache can serve the wrong representation. If too many values are included, the cache may create numerous low-use variants and deliver few performance benefits.
A practical design usually has three layers:
- Shared page caching: reusable HTML for anonymous visitors, where the response does not contain private or user-specific data.
- Fragment or client-side personalization: a shared page with small personalized areas filled in separately.
- Private or bypassed responses: account pages, carts, checkout flows, and other responses that should not be stored as shared content.
This is different from object caching. A page cache stores a rendered response, while a Redis object cache or another server caching layer may store database results or application objects. Understanding the distinction helps teams decide whether a request needs a different page variant, a private response, or simply faster application data access. See page cache versus object cache for a layer-by-layer comparison.
What to track
1. Classify every response variation
Before changing cache settings, list the inputs that can alter the response. Use a table like this as a starting point:
| Variation | Typical safe approach | What to verify |
|---|---|---|
| Anonymous public page | Shared page cache | No account, cart, or private data appears in the HTML |
| Logged-in session | Bypass shared page cache or use a private cache | Authentication cookies cannot reuse an anonymous or another user’s response |
| Shopping cart or checkout | Bypass shared page cache; keep state private | Cart totals, addresses, and order details remain request-specific |
| Geolocation | Use a controlled region key or render the regional part separately | The region is derived consistently and does not expose location-sensitive data |
| Language | Use a language key or distinct localized URLs | Language detection and fallback behavior are consistent |
| Device type | Prefer responsive output; add a device variation only when necessary | Desktop and mobile variants cannot be mixed accidentally |
| Pricing or eligibility | Keep the response private or key on a validated, limited segment | Prices, discounts, and access rules cannot cross user or eligibility boundaries |
| Recommendations | Load recommendations separately or use coarse, non-sensitive segments | A recommendation response contains no private history or identifying data |
2. Inventory cookies and headers
Track which cookies and request headers are sent, which ones affect the response, and which ones merely support analytics or unrelated scripts. Do not automatically place every cookie in a cache key. That can fragment the cache and may cause a response to be stored under identifiers that should never influence shared delivery.
Instead, define an explicit policy for each relevant value:
- Ignore it: when it cannot change the server response.
- Strip it before lookup: when it is unnecessary for shared content and creates avoidable variants.
- Use it for bypass: when its presence indicates a logged-in, cart, preview, or otherwise private request.
- Normalize it into a small key: when only a controlled category, such as language or region, changes the response.
Document the rule in the CDN, reverse proxy, web server, or application configuration. Cache headers such as Cache-Control, Vary, and any platform-specific surrogate controls should agree with that design. Vendor interfaces and framework defaults can change, so record the effective behavior rather than relying on a plugin or hosting label alone.
3. Measure cache behavior, not only page speed
Track cache status for representative URLs: hit, miss, bypass, expired, revalidated, or error. Also record the cache key inputs, response headers, time to first byte, and whether the origin was contacted. A high hit ratio is not useful if the cache is serving an incorrect variant, and a low hit ratio may be expected for private or highly varied pages. A cache hit-ratio monitoring workflow can help connect these signals.
Cadence and checkpoints
Review dynamic caching monthly for active sites and at least quarterly for stable sites. A shorter review is appropriate after a major authentication, ecommerce, localization, or CDN change. Keep a small test set that represents the important boundaries instead of testing only the homepage.
At each checkpoint:
- Request a public page with a clean browser session and confirm whether the response is cacheable.
- Repeat the request with a logged-in session and confirm that shared delivery is bypassed or correctly isolated.
- Use two separate accounts with different carts, preferences, or entitlements. Confirm that each receives its own expected result.
- Test each supported language and region, including the default and fallback cases.
- Compare mobile and desktop requests if device-specific rendering is enabled.
- Publish or update content, then verify purge, revalidation, and stale-content behavior at every cache layer.
- Inspect response headers and logs to confirm that the observed behavior matches the documented configuration.
Automate these checks where possible. A scripted test can store a response fingerprint for each scenario and fail when a private marker appears in a shared response or when two expected variants become identical unexpectedly. Include cache-warming and cold-cache runs; a cold miss can reveal origin behavior that a warm hit hides.
How to interpret changes
A sudden fall in hit ratio does not automatically mean the cache is broken. A new cookie, query parameter, experiment, or personalization feature may have expanded the cache key. First compare the timing of the change with deployments and inspect a sample of miss and bypass reasons.
If performance improves while cache variants multiply, simplify the key. Prefer stable URL structures and a small set of approved dimensions over raw cookie values. For example, a normalized language key is usually easier to operate than a key containing every browser preference. If personalization is limited to a small region of a page, keep the document shared and request that fragment separately rather than making the whole HTML response private.
If users see another visitor’s content, treat it as a cache isolation failure. Immediately disable the affected shared rule or bypass the relevant response class, purge affected objects, preserve logs for investigation, and test every cache layer. Do not solve the symptom only by lowering the TTL: a shorter lifetime still allows an incorrect response to be served during that lifetime. For broader purge and deployment patterns, see cache invalidation patterns.
When content is stale but correctly scoped, investigate invalidation, revalidation, and multiple cache layers. The browser, CDN, reverse proxy, server cache, application, and object cache can each hold a different version. A layer-by-layer check is more reliable than repeatedly purging everything. The cache troubleshooting guide provides a useful sequence for this diagnosis.
When to revisit
Revisit the cache-key design on a monthly or quarterly schedule, and immediately when the site gains a new personalization feature, login flow, region, language, device template, pricing rule, experiment, or third-party script that sets cookies. Also review it after moving between hosting, CDN, web server, or cache-plugin configurations. A hosting comparison or migration can change default cookie handling, cache headers, and purge behavior even when application code stays the same.
Keep a one-page cache contract with these fields:
- Responses that are publicly shareable
- Responses that always bypass shared caching
- Approved cache-key dimensions and their allowed values
- Cookies and headers that trigger bypass, variation, or stripping
- TTL, stale, revalidation, and purge expectations
- Test URLs, account scenarios, regions, languages, and devices
- Owner and last review date
After every relevant change, run the test matrix before increasing TTLs or expanding cache coverage. If the expected boundaries are clear and the tests remain green, dynamic website caching can reduce origin work without sacrificing privacy or correctness. If a response cannot be confidently separated into safe shared variants, bypass the shared page cache and optimize the application or object-caching layer instead.