Overview
The article discusses a recent optimization in the Cloudflare Workers runtime that reduces memory usage and, consequently, costs for users of Unbound Workers. By marking Workers as 'idle' once response headers are sent, the billing model changes, leading to an average reduction of 70% in the time Workers are considered 'in use'.
What You'll Learn
1
How to optimize Cloudflare Workers for cost efficiency
2
Why marking Workers as 'idle' reduces billing
3
When to apply the new changes for WebSocket proxying
4
How to implement response caching with Workers
Key Questions Answered
How does the optimization in Cloudflare Workers reduce billing?
The optimization allows Workers to be marked as 'idle' once response headers are sent, meaning they are no longer considered 'in use' during the streaming of response bodies. This change reduces the billing duration by an average of 70%, as the system only tracks low-level sockets instead of keeping the Worker state in memory.
What impact does the change have on WebSocket proxying?
With the new optimization, Workers do not remain in use during WebSocket proxying unless they handle individual messages. This means that for stateless Workers, the cost associated with maintaining the Worker state during proxying is eliminated, leading to potential cost savings.
What are the benefits of using cache.match in Workers?
When using cache.match, the Worker is considered idle as soon as the response headers are returned. This means that the Worker does not incur additional memory usage costs while the response is being streamed, making it a cost-effective solution for serving cached content.
How does streaming from KV impact Worker billing?
Streaming from KV can be optimized by fetching values as streams instead of strings or JSON objects. This allows the Worker to be marked as idle during the streaming process, reducing the billing time and associated costs.
Key Statistics & Figures
Reduction in billing time
70%
This average reduction applies to the time a Worker is considered 'in use' after the optimization changes.
Technologies & Tools
Backend
Cloudflare Workers
Used for implementing HTTP proxies and optimizing cost through memory management.
Key Actionable Insights
1Implement the new idle marking feature for your Cloudflare Workers to reduce costs.By ensuring that your Workers are optimized to be marked as idle when they are not actively processing requests, you can significantly lower your billing under the Unbound pricing model.
2Utilize cache.match effectively to minimize Worker usage time.When serving cached responses, ensure to return them directly from cache.match to take advantage of the idle state, which can lead to cost savings.
3Adapt your WebSocket handling to leverage the new optimization.If your Workers are proxying WebSocket connections, ensure they are not handling individual messages to benefit from the idle state and reduce memory usage.
4Stream responses from KV to optimize memory usage.When retrieving data from KV, use streaming methods to ensure that your Workers are marked as idle, thus reducing the time they are billed.
Common Pitfalls
1
Failing to optimize Workers for idle state can lead to unnecessary costs.
If Workers are not configured to be marked as idle during response streaming, they will continue to incur charges for the duration they are considered 'in use', leading to inflated bills.
Related Concepts
Cost Optimization In Serverless Architectures
Caching Strategies In Web Applications
Websocket Handling In Serverless Environments