Cache tag

On this page 7

A cache tag is a label that an origin server attaches to a cached response so that a CDN or reverse proxy can later invalidate every object carrying that label with a single purge request. In content delivery across the Internet, cache tags replace URL-by-URL purging with dependency-based invalidation: content is removed because the data behind it changed, not because someone knows each affected address.

Vendors use different names for the same idea, including surrogate keys, cache-tags and purge tags. The concept is the same in each case: a many-to-many mapping between cached objects and the logical entities, such as a product, an author or a template, that they depend on.

Cache tags work by sending labels in a response header that the edge stores alongside the cached object. The origin adds a header such as Surrogate-Key or Cache-Tag with a space- or comma-separated list of tags. The edge records each tag in an index that points to all cached objects that carry it, usually in every point of presence.

When the underlying data changes, the application sends one purge call per tag to the CDN API. The CDN looks up the tag index and removes or marks stale every matching object, regardless of its URL, query string or cache key variant. The edge normally strips the tag header before delivering the response to browsers, so the labels stay internal.

The table below traces the life cycle of a tagged object.

StepActorWhat happens
1. Response generationOrigin serverAdds tags such as product-482 and category-shoes
2. Edge storageEdge cacheStores the object and indexes it under each tag
3. DeliveryEdge cacheServes hits and removes the tag header
4. Data changeApplication or CMSCalls the purge API with product-482
5. InvalidationCDN control planePurges every object tagged product-482 globally
Life cycle of a tagged response

Header names and platform limits

There is no single standardized header for cache tags, so each platform defines its own name and limits. The Edge Architecture Specification from the early 2000s introduced the surrogate concept, and several CDNs adopted the Surrogate-Key name from that tradition. Limits on tag length and total header size matter, because an object with hundreds of tags can exceed them and lose some labels silently.

Platform typeHeader nameTypical constraints
FastlySurrogate-KeySpace-separated keys, header size limit of a few kilobytes
CloudflareCache-TagComma-separated tags, purge by tag on supported plans
AkamaiEdge-Cache-TagComma-separated tags, purge through the Fast Purge API
Varnish with xkeyxkey or a custom headerModule-based index in the proxy itself
Amazon CloudFrontNo native tag purgeInvalidation by path or wildcard only
Typical cache tag implementations

Exact limits change between product generations and plans, so the current platform documentation should be checked before a tagging scheme is designed. Where native support is missing, teams often keep their own tag-to-URL index and translate tag purges into path invalidations.

Cache tags compared with other purge methods

Cache tags solve the problem that one data change affects many URLs. A product price appears on the product page, on category listings, on search results and in API responses. URL purging requires the application to know all those addresses, while a tag purge only needs the identifier of the changed entity.

MethodPrecisionApplication effortBest fit
Single URL purgeExact for one objectHigh when many pages depend on dataStatic assets, isolated pages
Wildcard or prefix purgeCoarseLowWhole sections or deployments
Purge allNoneMinimalEmergencies, major releases
Cache tag purgeExact by dependencyModerate, tagging logic at render timeDynamic sites with shared data
Short TTL onlyTime basedNoneContent that tolerates delay
Purge methods and their trade-offs

Tags also work well with stale-while-revalidate and soft purging. A soft purge marks tagged objects as stale instead of deleting them, so the edge can still serve the old copy while it fetches a fresh one, which protects the origin from a sudden traffic spike after a large invalidation.

Designing an effective tagging scheme

A good tagging scheme mirrors the data model of the application, not its URL structure. Each rendered response receives tags for every entity it displays, plus broader tags for templates or sections. Frameworks and content management systems often generate these tags automatically while rendering, which keeps them consistent with real dependencies.

  • Tag by stable identifiers such as product-482 or author-17, not by titles that can change.
  • Add a coarse tag per template or section so a layout change can be purged without a full purge.
  • Keep tag names short and predictable, and prefix them by environment when staging and production share one CDN account.
  • Combine tags with long TTLs, because event-driven invalidation makes long caching safe.
  • Monitor purge latency and purge volume, since a single popular tag can invalidate a large share of the cache at once.

The following table helps decide how broadly to tag.

Content situationRecommended tagsReason
Product detail pageProduct, brand and category tagsPrice or stock changes must appear quickly
Listing or search pageCategory tag and a listing tagToo many entity tags would exceed header limits
Shared navigation fragmentMenu or layout tagOne edit affects every page
API responseEntity tags matching the payloadKeeps clients and pages consistent
Choosing tag granularity

Common mistakes and failure modes

Most cache tag problems come from missing or excessive tags. A page that omits a dependency keeps showing outdated data after a purge, which is often blamed wrongly on the CDN. A page with too many tags may exceed the header limit and lose the most important ones, or it may be purged so often that the cache hit ratio collapses.

  • Forgetting fragment dependencies such as prices in a related-products widget.
  • Purging broad tags on every save, which turns frequent editorial work into near-total cache flushes.
  • Assuming purge is instant: global propagation typically takes from under a second to several seconds, depending on the platform.
  • Leaking internal tags to browsers when the edge is not configured to remove the header.

Explained without jargon

Imagine a library that keeps photocopies of popular pages near the entrance so visitors do not wait. A cache tag is like a colored sticker on each photocopy that says which book it came from.

When a book is corrected, the librarian does not need to remember every copy made from it. She simply removes every copy with that sticker, and fresh copies are made on the next request. That is why cache tags keep fast copies accurate without guesswork.

A typical use case

An online shoe retailer serves about 95 percent of page views from its CDN with a 24-hour TTL. Each product page carries tags for its product, brand and category, and each listing page carries its category tag. When the warehouse system lowers the price of one running shoe, the shop backend sends a single purge request for product-482.

Within a few seconds, the product page, three category listings, the brand page and the JSON response used by the mobile app are refreshed at every edge location. The retailer keeps its high cache hit ratio and correct prices at the same time, instead of choosing between short TTLs and outdated listings.