Architecture
Purge by meaning: how cache tags scale to billions of objects
Tags let you purge every page that shows an article, wherever it lives. Inside the per-location tag index, the memory budget, and how a tag purge reaches 52 cities in 150 ms.
Purging by URL works when you know every URL that changed. Real sites rarely do. When an editor fixes a headline, the article page changes, but so do the homepage, the section front, the author page, three listing pages and an RSS feed. Cache tags let the origin describe what a response is about, so the purge can be about the same thing.
How tags work for customers
The origin adds a Cache-Tag header listing any number of tags. We index them when the response is stored and strip the header before the response leaves the edge, so tags never leak to browsers.
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: public, max-age=300, stale-while-revalidate=600
Cache-Tag: article-1842, author-77, section-science, homepageA single API call purging article-1842 then invalidates every cached object that carried that tag, in every location, whatever its URL, query string, encoding or device variant.
The index
Each location keeps a tag index alongside its cache. The index maps a 64-bit hash of each lower-cased tag to the set of object IDs that carry it. Object IDs are dense 64-bit integers assigned when an object is stored, which lets us keep each set as a compressed bitmap. Most tags belong to a handful of objects and their bitmaps are a few bytes; a tag like homepage on a large site may cover millions of objects and still compresses to a few hundred kilobytes.
// One entry per tag per location; values are 8-byte object ids.
type TagIndex struct {
shards [256]struct {
mu sync.RWMutex
tags map[uint64]*roaring64.Bitmap // xxh3(tag) -> object ids
}
}
func (ix *TagIndex) Purge(tag string, soft bool) (n uint64) {
h := xxh3.HashString(strings.ToLower(tag))
s := &ix.shards[h&0xff]
s.mu.Lock()
bm := s.tags[h]
delete(s.tags, h)
s.mu.Unlock()
if bm == nil {
return 0
}
bm.Iterate(func(id uint64) bool {
store.Invalidate(id, soft)
n++
return true
})
return n
}The index is sharded 256 ways by hash to keep lock contention low. A purge locks one shard, and only long enough to detach the bitmap; the invalidation itself happens outside the lock. Objects that are evicted from the cache are removed from their tags lazily during compaction, which runs continuously at low priority.
Memory budget
The index has to live in RAM to be fast, and RAM on a cache node is also our hottest cache tier. We budget roughly 2.5% of node memory for tags. Two decisions keep us within it. First, we store hashes, not tag strings: a collision would purge a few extra objects, which is harmless, and with 64-bit hashes we have never observed one. Second, we cap tags at 64 per response and 128 characters each, which rules out the pathological case of one tag per visitor.
Getting a purge everywhere
A tag purge is accepted by the API, validated, and appended to a replicated purge log with a sequence number. Every location consumes the log, applies the purge to its local index, and acknowledges the sequence number. The API reports the purge complete when all 52 locations have acknowledged it. Because the log is durable and ordered, a location that was restarting or briefly partitioned replays the purges it missed before it serves traffic again; it never serves content that a completed purge should have removed.
A soft purge follows the same path but marks objects stale instead of deleting them. The next request is served from the stale copy while one background revalidation per object per location refreshes it. For a homepage tag covering a million objects, that turns what would have been a thundering herd on the origin into a gentle, spread-out refill.
| Metric (October 2026) | Value |
|---|---|
| Tags indexed per location (p50) | 1.9 billion |
| Index memory per cache node | 11–14 GB |
| Tag purge, API to last ack (p50) | 148 ms |
| Tag purge, API to last ack (p99) | 410 ms |
| Largest single tag purge last month | 38 million objects |
Lessons
Ordering and durability are what make purge trustworthy, and they come with a cost: a stuck consumer holds back everything behind it. That is exactly what happened during the 28 October incident, and it is why every consumer now has a progress watchdog and a dead-letter queue.
The other lesson is about how customers use tags. The most effective schemes we see are boring: one tag per content entity (article-1842), one per listing (section-science), and one per template that appears everywhere (header-nav). Tag what your editors change, not how your pages are built, and purges stay small and precise.