Workers Cache (blog.cloudflare.com)
178 points by ilreb 4 hours ago
simonw 3 hours ago
> This is the caching API we've always wanted Workers to have. Here's why it took us this long
I was looking forward to the "why it took us this long" explanation but it wasn't explicitly spelled out. Any Cloudflare staff here able to expand on that?
(The article does a good job of showing how many different smart design decisions went into this, but given caching is core to what a Cloudflare does I'm still a little surprised it took 9 years to get here!)
kentonv 2 hours ago
The honest answer is:
We implemented the "standard" Cache API back in the early days because it was a standard, one which was intended to be used together with the Service Workers API, which we were also building around back then.
But it was never a good fit. The get/put API was designed for a local browser cache, not a distributed cache like Cloudflare's. We probably should have realized this before implementing it, but it really became obvious over the years of actual use.
But given we had something that mostly worked for most use cases, it was hard to prioritize redoing it against all the other things on our plate. So we deferred.
More recently, architectural changes we've been making in Workers for other reasons happened to make it significantly easier to finally implement this the way we wanted, and we were able to find some engineering time to get it over the line.
Gory details for the curious: We've been improving the infrastructure around the notion of workers having multiple "entrypoints", with the ability to parameterize those entrypoints. ctx.props[0] and ctx.exports[1] are part of this. A lot of this was motivated by Dynamic Workers sandboxing, but the concept also presents a clean way to inject a cache between two parts of the same worker, by applying it to the entrypoint and having the worker call itself using ctx.exports.
Moreover, the introduction of "channel tokens" made a big difference[2][3]. Essentially I created a way to encode a token (bytes) representing an arbitrary entrypoint to a Worker, complete with its serialized parameters. I did this to enable these entrypoint stubs to be passed over RPC, which is again useful for sandboxing use cases, but it also created a convenient, encapsulated way to pass information through our cache infrastructure about what worker should run at the other end.
It's not a huge breakthrough or anything, but I think it made the architecture clearer in everyone's mind to the point that we got excited about using it to implement caching properly, finally.
[0] https://developers.cloudflare.com/workers/runtime-apis/conte...
[1] https://developers.cloudflare.com/workers/runtime-apis/conte...
[2] https://github.com/cloudflare/workerd/blob/main/src/workerd/...
[3] https://github.com/cloudflare/workerd/blob/main/src/workerd/...
kentonv an hour ago
Realizing there's a more fundamental piece I'm not explaining well here.
Our architecture is something like:
ingress -> routing -> security -> workers -> cache -> origin
That is, workers run "in front of" cache. That's usually what you want. Workers run on the edge, so putting them in front of cache doesn't cost anything in terms of latency, and there's a lot of useful stuff you can do only if they run in front, e.g. serving pages from cache but customizing them for specific users.
Putting workers behind cache is an architectural change. All of the logic around routing to them lives in front of the cache. And it makes Workers a lot less useful.
We weren't that excited about it until we had a clearer story for how to run custom logic on both sides of the cache, but it was pretty unclear how to do that in a nice way until the recent developments around ctx.props, ctx.exports, channel tokens, etc.
drdexebtjl an hour ago
tshaddox 3 hours ago
The article was very clearly written or heavily edited by AI, which I suspect explains some of the peculiarities in structure and wording.
vlucas 3 hours ago
Huge props to just sticking with the HTTP spec on this one with `Cache-Control` headers with `stale-while-revalidate` support. It's amazing how many other providers mess that up.
On top of that the cache tags are a slick way to do invalidation. This looks like a great product.
mchav 3 hours ago
Great feature. Although I’m starting to get annoyed by obvious signs of LLM writing like no X, no Y etc.
xpct 3 hours ago
People that don't like writing now get to write by offloading it to an LLM, and this is the result. I miss the world where articles were mostly written by people who had the interest and patience to do it.
383toast 2 hours ago
At least for me I don't really read these posts/docs, I just give the link to claude and it'll implement it regardless of wording
pythonaut_16 2 hours ago
Is this a bot or LLM reply? Because you've given this exact comment word for word at least twice in this thread...
dbbk 2 hours ago
I was a bit confused what this adds other than just standard CDN-Cache-Control page caching that we do now. Some quirks I've found;
- You still get billed per request, whether the request hits cache or not (but don't get billed for CPU time)
- You now get billed for static asset requests! This makes no sense to me. "One thing to watch: when caching is enabled, requests that are normally free — static asset requests and worker-to-worker invocations through service bindings or ctx.exports — are billed at the standard request rate, because each one now consults the cache in front of your Worker." Yeah that sounds like a bug that just happens to generate them more money.
- The cache key automatically has the worker deployment version, so even gradual deployments populate their own cache which is nice
- It seems like you can set a totally custom cache key? But that was previously Enterprise only, can't see if that's still the case here.
drdexebtjl 44 minutes ago
> Yeah that sounds like a bug that just happens to generate them more money.
Yeah. It sounds like they moved the component that counts requests earlier in the pipeline so that it can count cache hits, and now it also counts these.
ascorbic an hour ago
> I was a bit confused what this adds other than just standard CDN-Cache-Control page caching that we do now.
Until now these cache headers didn't work if you set them in a Worker, because Workers were always run in front of the cache. You could use them if you're running Cloudflare cache in front of another origin, but not if you were e.g. rendering a site in Workers. This changes that.
drdexebtjl 2 hours ago
Finally :)
Time to migrate rarely-updating websites that still need a CMS to Cloudflare.
> One thing to watch: when caching is enabled, requests that are normally free — static asset requests and worker-to-worker invocations through service bindings or ctx.exports — are billed at the standard request rate, because each one now consults the cache in front of your Worker.
That’s a very weird limitation from a product standpoint. You might end up spending more money by enabling caching.
I wonder if static assets and worker-to-worker invocations are really more expensive to them with caching enabled, or if this is just a metering problem.
I would have made this free in the interest of making billing simpler and of making the decision to enable caching more straightforward.
If you’re min-maxing costs, this encourages awkward setups where you split workers into two, one with caching enabled and one with caching disabled.
dangoodmanUT 3 hours ago
finally, this was needed.
A big worry was always "why does workers sit in front of my cache? that's a waste of an invocation if i'm returning a cached result"
jasoncartwright 3 hours ago
The previous Worker in-front of the cache never made sense to my old school proxy in-front of the appserver mindset. Already using this to speed up a tool. Nice work.
drdexebtjl an hour ago
It makes sense in a world where Cloudflare is something that runs in front of your app. You need to run some arbitrary code before their logic, including their cache. If you wanted something that ran after the cache, you could put it in your app, you didn’t need the worker. That was Cloudflare when Workers launched.
But now the platform has evolved to where you’re running the entire app from Workers.
dbbk 2 hours ago
Cloudflare Snippets were actually better if you wanted to run logic before the cache (they are entirely free)
lekevicius 3 hours ago
Amazing, exactly what Workers lacked, I was quite annoyed that a worker would spin up when 99% of my requests then return a cache. Waste of 3ms, times millions.
TimCTRL 2 hours ago
i just love the sweet $0 invoice at the end of each month for all these services.
Thanks CF.
thinkafterbef 4 hours ago
Finally proper stale-while-revalidate support!
dbbk 2 hours ago
That landed a few months ago
NSUserDefaults 3 hours ago
Reinventing the web, one service at a time. Is it really that hard to build things like this on a per-case base, small scale, all on a bare bones VPS? Do we need a global gateway to help us not screw up the basics? Service looks nice but makes me sad somehow.
jesse_dot_id 2 hours ago
As a DevOps engineer, if you learn to implement CF Workers with Claude Code's Cloudflare skill, you will no longer be burdened by this question. I'm running a Docker swarm on my homelab, a Docker swarm on a EC2 instance, but there's no longer a single web service in either of them. Workers is insanely good.
ignoramous 3 hours ago
When Workers Cache is enabled, every cacheable request to your Worker hits Cloudflare's cache first. If there's a fresh cached response, Cloudflare returns it directly — your Worker doesn't run, and you don't pay CPU time for it. On a miss, your Worker runs, and if your response is cacheable, Cloudflare stores it for the next request. The next request from anywhere on Earth can be served straight from cache.
Incredible! This is why I shoehorn all my server side usecases on to the Workers Platform. Cloudflare, since 2020 when I first went all-in, has consistently shipped features that reduce bills significantly (except for 2023 Workers usage model changes). In one case, when they shipped free Snippets (Workers but 32kb code size & 5s CPU time) for Pro accounts ($200/yr), our bills went from £15k+ to £0.I know about the infamous "Enterprise plan" (especially, when your bandwidth is as high as ours in 100s of TBs) and know of at least one other tech shop that was required to pay for it ... but we haven't got that sales call, yet.
PUSH_AX 3 hours ago
Our bandwidth is very high, we constantly get invited onto the call with their team, but after talking with them a few times it makes absolutely zero sense for us to have a committed spend, all the stuff I needed an account manager for in GCP/AWS just doesn't exist in CF. Support wise I imagine if it's broken for us it's also broken for 2 million other people so... yeah... Thanks CF!
htsh 3 hours ago
curious, how does one configure something like this for AWS Lambda? Appreciate it.
I am assuming it is a bunch of manual work.
baueric 3 hours ago
Equivalent is probably putting AWS CloudFront in front of your lambda. Has edge caching with customizable config.
afavour 3 hours ago
You use Cloudfront. Which you’re already doing anyway but yes, you do need to configure it.
coredog64 3 hours ago
Technically, probably CloudFront.
davidmurdoch 4 hours ago
The feature is great. The post itself is a slop grenade.
napsterbr 3 hours ago
One of the recent AI tells other than em dash is the excessive usage of hyphenated words:
> multi-tenant-safe cache keys
> on a server-rendered app
> byte-for-byte identical (classic)
> gets a cache-speed response
> cached-file-extensions list
Honestly, this is terrible. I had to add a "use simple words only, don't hyphenate unnecessarily" warning to my Claude config. After a full day of work, having to read these Claudisms all the time make a noticeable difference on how tired you get. It gets even worse when Claude starts to make up its own vocabulary.
topgrain2 3 hours ago
Fuck, I spent all these years developing a thoughtful writing style that leaned toward clarity for the reader, even if it meant extra work to achieve precision, or adding affordances like “excessive” hyphenation, and now I guess have to learn to write worse.
tshaddox 3 hours ago
napsterbr 3 hours ago
ambicapter 2 hours ago
davidmurdoch 3 hours ago
swiftcoder 3 hours ago
jesse_dot_id 2 hours ago
I have used em dashes extensively — for at least a decade — and I also generously apply hyphens when it makes sense to do so as well. You people are about as annoying as the grammar nazis on IRC in the '90s, except you're saying that prose is less readable because a large language model has been trained on nearly the sum total of human knowledge and found that em dashes are used extensively in the highest rated prose and therefore must be the correct choice to make.
Also, most engineers will likely just be skimming this article before feeding it into their harness to implement the changes anyway, so it makes sense for it to be more heavy on context than it would be if meant for only humans to consume.
CodesInChaos 3 hours ago
I have an over-hyphenated writing-style as well. Probably my Germanness.
ambicapter 2 hours ago
nwatson 3 hours ago
arikrahman 3 hours ago
One of the earliest tells was the use of emdashes.
dan_sbl 4 hours ago
Billion dollar company can't afford one human copywriter. The future is great! (edit: copyrighter -> copywriter)
davidmurdoch 4 hours ago
They could just feed an llm a small corpus of past human authored posts from their site, and have the LLM rewrite it in a style matching style, and it would likely turn out pretty great.
jgrahamc 3 hours ago
ButlerianJihad 3 hours ago
Neither can you afford a copywriter, evidently?
CodesInChaos 4 hours ago
Modern cloudflare in a nutshell.
lijok 4 hours ago
Can you elaborate? I read it, found the concepts well explained, walked away better informed.
Responding to alleged slop with more slop doesn’t decrease the total amount of slop on the internet.
davidmurdoch 4 hours ago
You think I'm using AI to leave comments like this on HN?
skrebbel 4 hours ago
geraneum 3 hours ago
Not the original commenter; but, at least for me, the idea is that when it’s written by humans we know that effort and care were put into communicating the news. Otherwise they could post a link to the docs and we could ask my flavor of LLM to summerize. No need for extra filler content. That why it’s slop and it’s different.
lijok 3 hours ago
hdz 3 hours ago
Does anything beat a free static site hosted with Cloudflare workers at this point, performance wise?
nicce 3 hours ago
Very difficult to beat if you don't process or store private information that should not be given to the US company.
adasq 4 hours ago
now, waiting for opennext adapter maintainers to catchup with it
brendanib 3 hours ago
use vinext! (now 1.0 beta) https://vinext.dev/ https://x.com/James_Elicx/status/2073457440461303836
it already uses Workers Cache for the route-level ISR cache
danabramov 2 hours ago
Please please please write your own blogposts. I already read “but here’s the biggest unlock, and it’s the part that’s hardest to see” all day.
I just don’t understand why undermine your own announcements by delegating comms to the machine. It’s disrespectful to the reader.
rob 2 hours ago
There has to be some sort of "/humanize-text" skill they can pass the output to before publishing.
danabramov 2 hours ago
There is this one! Though I have not tested it. https://github.com/stephenturner/skill-deslop
I found it funny to just read through.
383toast 2 hours ago
At least for me I don't really read these posts/docs, I just give the link to claude and it'll implement it regardless of wording
jesse_dot_id 2 hours ago
This is also 100% what I just did. In fact, I think companies should be releasing a human readable format as well as a machine readable format.