Problem
A US consumer app served more than 50 million images a day. Repeated image delivery put substantial load on the origin, even when the underlying image had not changed. Image-path CPU was approximately 90%.
Constraints
The origin needed to remain the source of truth and generate missing variants. Replaced or deleted images could not leave stale derivatives scattered across caches. Existing browser copies needed correct conditional responses.
What I did
I gave each layer a specific job. A Cloudflare Worker normalized image requests into canonical variant keys. The edge cache served hot responses; R2 held durable copies for cold locations. Requests reached the origin only when the required variant was missing.
D1 tracked the object-key inventory for lifecycle changes. Signed invalidation handled privileged replacements and deletions. Conditional requests allowed browsers with a valid copy to receive a 304 Not Modified response.
Result
50M+ images delivered per day. Image-path origin CPU fell from approximately 90% to below 15%.
The documented cached path measured approximately 250 ms at p50 and 670 ms at p95. Requests that missed both the edge cache and R2 reached roughly 1.9–2.1 seconds at p95.
Tradeoffs
Caching reduced repeat work but added a consistency problem. Canonical keys, a discoverable variant inventory and authenticated invalidation became part of correctness. The origin remained necessary for misses and generation; moving delivery to the edge did not eliminate that dependency.
Read the full engineering write-up, including the read path and invalidation rules.