A CDN cache key determines which requests share a cached response and which must remain separate. This guide explains how to design cache keys around query strings, cookies, devices, languages, and personalization, then gives you a repeatable workflow for testing cache safety and reviewing performance over time.
Overview
A cache key is the identity used by an edge cache to store and retrieve a response. Depending on the CDN, the default key may include the URL path, query string, host, protocol, selected request headers, or other request attributes. You can often customize some of these inputs through cache rules, edge logic, or provider-specific cache-key settings.
The design objective is not to make every request share one object. It is to make equivalent requests share an object while keeping materially different responses separate. A key that ignores a language selector may serve French content to an English visitor. A key that includes every tracking parameter may create a separate cache object for nearly every campaign click. A key that ignores authentication or cart state can expose private or transactional data.
Start by treating cache-key design as a content-variation problem. For every cacheable URL, identify the request properties that can change the response. Then classify each property as one of four types:
- Content-defining: it changes the actual representation, such as locale, image width, or API version.
- Tracking-only: it measures acquisition but should not change the page, such as many campaign parameters.
- Personalization or authorization: it depends on the visitor, account, cart, or permissions.
- Operational: it affects routing or debugging but should not normally create a public cache variant.
Only content-defining inputs generally belong in a public CDN cache key. Tracking-only inputs are usually normalized away. Personalization and authorization inputs normally require bypassing the shared cache or using a carefully isolated private strategy.
What to track
Build a request-variation inventory
Before changing a CDN rule, record the URL patterns your application serves and the variables that can alter their responses. A simple inventory prevents assumptions from being hidden in framework middleware, plugins, origin code, or JavaScript-driven personalization.
| Request property | Typical treatment | Reason to review |
|---|---|---|
| Path and host | Include in the key | Different paths or domains commonly represent different content. |
| Language or locale | Include only when it changes the response | Prevents one language variant replacing another. |
| Image width, format, or transformation | Include normalized values | Different dimensions and formats are separate representations. |
| Campaign parameters | Usually remove or ignore | They often measure traffic without changing page content. |
| Authentication and session cookies | Bypass shared caching or isolate securely | They may expose account-specific data. |
| Device class | Use only if the origin returns genuinely different content | A responsive page does not need separate desktop and mobile keys. |
| Experiment assignment | Include the stable variant or bypass caching | Visitors must receive the intended experiment version. |
Review query strings deliberately
Query-string caching is useful when parameters select a representation or API response. Examples can include ?width=1200, ?format=webp, ?lang=de, or a documented API version. These values should be validated and normalized where possible. For example, treating width=1200 and width=01200 as separate objects may reduce the cache hit ratio unnecessarily if the application considers them equivalent.
Tracking parameters such as utm_source, utm_medium, and utm_campaign commonly do not change the HTML response. If your application does not use them to render different content, remove them from the cache key or redirect to a normalized URL. Do not assume every unfamiliar parameter is safe to ignore: parameters such as preview, draft, format, redirect, or token may affect content or security.
Handle cookies and headers with caution
Cookies can indicate login state, shopping-cart contents, consent choices, experiment assignment, or a simple analytics identifier. Including a high-cardinality cookie in a public cache key can create one cache object per visitor and eliminate the benefit of edge caching. Ignoring a cookie that changes the response can be worse.
Separate anonymous, cacheable responses from authenticated or personalized responses where possible. A common safe pattern is to cache the public page and load account-specific elements separately through a client-side request or an explicitly private endpoint. For WordPress and WooCommerce sites, verify that login, cart, checkout, account, preview, and administrative paths are excluded from shared caching; see the WooCommerce cache rules guide for a focused testing checklist.
Decide whether device variation is real
Do not add a device value to the cache key merely because visitors use different devices. A responsive page that uses the same HTML and CSS can share one object. Create device variants only when the origin produces materially different markup, images, or functionality based on a reliable signal. If you do vary by device, use a small normalized set such as mobile, tablet, and desktop rather than raw user-agent strings.
Cadence and checkpoints
Cache-key design should be reviewed on a monthly or quarterly schedule, and immediately after a change that can introduce a new response variant. Maintain a short record containing the URL pattern, current cache-key inputs, bypass conditions, expected cache behavior, and the person responsible for review.
At each checkpoint, inspect:
- Cache status: Compare cache hits, misses, bypasses, expired objects, and revalidations by hostname and URL pattern.
- Cache hit ratio: Look for a sustained decline rather than reacting to a small sample or a single deployment.
- Object count and storage growth: A sudden increase can indicate unnecessary query-string or cookie variation.
- Origin load and TTFB: If more requests reach the origin, determine whether the key is fragmenting or whether content has become uncacheable.
- Response headers: Confirm that
Cache-Control,Vary,Age, and provider cache-status headers match the intended policy. - Functional samples: Test anonymous, logged-in, localized, experiment, and device-specific requests from multiple regions.
Repeat this review after adding localization, A/B testing, personalization, image transformations, framework middleware, a new tracking system, or a major CDN release. Provider dashboards may use different names for cache-key controls, so record the effective behavior rather than relying only on a rule label.
How to interpret changes
A lower cache hit ratio does not automatically mean the cache key is wrong. A new product launch, a low-volume URL, short time-to-live values, or frequent purges can produce more misses. Interpret the metric alongside request volume, URL distribution, origin response time, and the number of cache objects.
If the hit ratio falls after adding a query parameter, compare normalized and raw URLs. A tracking parameter that creates many near-identical objects is a strong candidate for removal. If the origin load rises after ignoring a parameter, verify whether that parameter actually selects content before restoring it to the key.
Unexpected content is a higher-priority signal than efficiency. Test two requests that should differ and confirm that their response bodies, important headers, language, prices, account state, and experiment assignment remain separate. Then test two requests that should be equivalent and confirm that they can reuse the same object.
Use response headers and browser developer tools to distinguish browser caching from CDN caching. Check the request URL, cookies, request headers, status code, cache-status header, age, and any CDN rule identifier. Repeat the test with a cache-busting value only when your test plan explains how that value affects the key. A purge can remove an object, but it will not correct a key that continues to combine or fragment requests incorrectly.
When testing from multiple regions, use separate networks or regional probes where available and allow for propagation and revalidation behavior. Compare the first request with subsequent requests, but avoid concluding that one miss proves a configuration failure.
When to revisit
Use this practical review workflow whenever the site changes and at least once per quarter:
- List every URL family that is currently cached at the edge.
- For each family, record path, host, query parameters, cookies, relevant headers, language, device behavior, and authentication state.
- Mark each input as content-defining, tracking-only, personalized, or operational.
- Compare the inventory with the CDN cache key, cache bypass rules, origin
Cache-Controlheaders, and application middleware. - Test safe equivalence cases and dangerous separation cases from at least two regions.
- Record hit ratio, origin traffic, TTFB, object count, and any functional anomalies before and after the change.
- Document the decision and schedule the next review.
Revisit the design immediately after a CDN or DNS migration, a localization launch, a personalization feature, a checkout change, an experiment rollout, or an update that changes response headers. For broader validation, pair this review with a Cloudflare cache rules audit and a Core Web Vitals check after CDN changes. A disciplined cache-key review protects correctness first, then improves cache reuse without turning every visitor, parameter, or device into a separate cache object.