← All posts

Architecture

Origin shield and request collapsing: turning 52 misses into one

Without a shield, every edge location fetches its own copy from your origin. How our second cache tier and request collapsing cut origin traffic by up to 91% for a video customer.

Edge Runtime team4 min read

A cache miss is cheap for us and expensive for our customers. It costs an origin a request, bandwidth and CPU, often in a cloud region that bills for every byte that leaves it. With 52 edge locations, a new object can be requested from its origin up to 52 times before every location has a copy, and far more often than that if many visitors in one city ask for it at the same moment. Origin shield is our answer to both problems.

Two tiers

Shield locations are larger edge locations that also act as a second cache tier. We operate seven: Amsterdam, Frankfurt, Ashburn, San Jose, São Paulo, Singapore and Tokyo. When shield is enabled for a site, an edge location that misses asks the shield for the object instead of the origin. The shield answers from its own cache if it can, and otherwise fetches once from the origin and fills both tiers.

Edge, shield and origin Edge LISEdge MADEdge WAW Shield FRAcollapses misses Origin 1 request
Three edge misses for the same object become a single origin request at the shield.

The shield for each origin is chosen by latency. Every shield location probes each customer origin every few minutes, and with shield = "auto" we pick the one with the lowest median round-trip time, with hysteresis so a short blip does not move it. An origin in Germany ends up behind Frankfurt; one in Virginia behind Ashburn.

Request collapsing

A second cache tier halves the problem; collapsing removes the rest. When many requests for the same cache key arrive while a fetch is already in flight, only the first goes upstream. The others wait on the in-flight fetch and are answered from it as it streams in, byte by byte, not after it completes. We collapse at both tiers, so 3,000 viewers starting the same live video segment within the same second produce one request from each edge to the shield, and one from the shield to the origin.

Collapsing has a sharp edge: if the upstream response turns out to be uncacheable, such as a personalised page that sets a cookie, every waiting request was held for nothing and must now go to the origin. We protect against that in two ways. Each key remembers for ten minutes that it was uncacheable, so the next burst goes straight through. And waiting requests give up after collapse_timeout, 15 seconds by default, and fetch on their own.

cachebz.toml
[site]
origin = "origin.example.com"
shield = "auto"            # picks FRA for an origin in Germany

[[rules]]
match = "/video/*"
collapse = true
collapse_timeout = "15s"
stale_while_revalidate = "30s"

A real example

A video-on-demand customer with an origin in Frankfurt enabled shield in May. Their library has about 80,000 titles cut into six-second HLS segments at five bitrates, and a long tail where most segments are watched only a handful of times a day across the world. Before the shield, each of those rare views was likely a miss at its local edge and therefore an origin request.

MetricBefore shieldWith shieldChange
Origin requests per hour412,00038,600−91%
Origin egress per day6.1 TB0.74 TB−88%
Cache miss TTFB, p50182 ms121 ms−34%
Hit ratio seen by visitors92.1%98.7%+6.6 pt

Miss latency improved too, which surprises people. The shield keeps warm, pooled TLS connections to the origin, so a miss from Lisbon is a fast hop to Frankfurt over our backbone followed by a request on an already-open connection, instead of a cold TLS handshake across the internet.

When not to use it

Shield adds one hop on a miss. For paths that are rarely cacheable, such as authenticated APIs, that hop is pure overhead, so those paths should be marked bypass = true; bypassed requests go straight from the edge to the origin. For everything else, shield is on by default on Business and Enterprise plans, and we have yet to meet a cacheable workload that was better off without it.

Questions or corrections? Write to the engineering team at hello@cache.bz. We read everything and update posts when we get something wrong.