Cache in front of Worker

Currently, no matter what, a Cloudflare Worker will run in front of CF’s cache. This makes Workers unappealing for doing things like serving static content (obtained via a complicated scheme only Workers can do) since you will be billed for every request; in this situation, it makes much more sense to build on another Serverless platform like Lambda or Cloud Functions since then you could have it serve the static content and get CF to cache the responses, all at a fraction of the price of what it would be with Workers [this would exponentially grow with the number of requests you get].

This feature request is a request to change this. It would be amazing if Workers could be toggled to run behind the cache to save on billing costs.

+1 although a workaround for costs would be that cached worker requests get excluded from worker billing and only charge for non-cached worker requests :slight_smile:

2 Likes

+1 This can help significantly reduce billing costs. This is the only thing that’s stopping me from moving to a serverless platform like Cloudflare Workers.

Please correct me if I’m wrong…

Can’t you do that by adding options to the cf object?

Setting Cloudflare cache rules (i.e. operating on the cf object of a request)

Source

I haven’t tried it but from the docs I got the impression you can interact with CF’s cache without using the Cache API which incurs in a worker hit.

Also, if you use a cloud function (eg: AWS Lambda) wouldn’t a significant portion of requests still go back to origin whenever a request hits a new data center without a cached version of the request?

For comparison:

AWS Lambda at Edge costs $0.60 per 1M requests + compute time + memory + network. You are also paying for idle time when waiting for the DB, fetch, etc. Also cloudfront costs for https I believe.

Google Cloud Functions cost $0.40 per 1M requests + compute time + memory + network. Not running on edge.

Cloudflare Workers running at edge costs $0.50 per 1M requests with no extra costs for cpu time, memory, or bandwidth.

1 Like

You can set cache for fetch requests you do within the worker, however you can’t set it so that your Worker only runs if the request URL [that would have otherwise triggered the worker] isn’t in CF’s cache.

1 Like

When a URL served by a Worker is requested, CloudFlare should first check to see if the content is available in cache and is valid before sending the request to the Worker.

When the Worker responds, CloudFlare should inspect the cache headers in the response to determine whether the response should be held in cache and for how long.

Effectively this is the standard origin behaviour in Cloudflare but applied to Workers.

Implementing this feature will primarily improve performance for visitors (as content can be served from CloudFlare cache instead of the worker) but an additional side benefit is to reduce billing as less requests are needed to be processed by the Worker for static content.

Unfortunately, that would prevent CF Workers from doing CF caching itself which is what I am using CF Workers for = CF Worker cache bypass on cookie with per url path and time of day cache TTL values (off peak has higher cache TTL vs peak hour traffic with lower cache TTL values).