Websocket Costs estimation - The rabbit hole
I've been deep diving into the CF documentation and blogs for days, trying to crack the code on cost estimation, but I'm still lost in the fog. SOS! HILFE! :NotLikeThis:
I need some help to estimate potential costs for using WebSockets.
Example scenario: Let's say I've got 10 clients (front-ends) connected, and I'm sending 2 basic individual messages to each client every second (10kb pay load each?) = 20 messages/s. = 200kb/s. = 51,840,000 messages /month
I don't use cache, but I might use D1, queues, or other workers to push messages,
so what moneys it could cost me? if I run this WebSocket 24/7?
And would it be cheaper than just making 20 standard worker invocations /second (client<>worker)?
Thanks a million for any insights you can share! ❤️ :bibicat:
2 Replies
Three things here:
Firstly, while Durable Objects(DOs) can be more expensive than regular Workers, there are some special considerations to be made with WebSockets on DOs specifically. DOs bill on Wall Time, yes, but only while an event is processed. So, if you connect a WebSocket to a DurableObject via the Hibernation API and then do nothing further, the wall time clock would stop, and you would not be billed.
Secondly, you do not necessarily have to connect all of your WebSockets to a dedicated DO. Bundling multiple WebSockets into a single DO is a perfectly valid decision. This means that, even if your DO is actively processing 24/7, your total GB-S Wall Time would be 336,384, which is under the 400,000 included cap. While this doesn't scale indefinitely, this can definitely help, billing-wise.
Lastly, WebSockets, both within Workers and DOs, aren't billed on outbound messages, but rather on CPU/Wall Time, so to more accurately estimate costs, we would need to know an estimate of how much time is spent generating each message to be sent via the WebSocket
Cloudflare Docs
Using WebSockets · Cloudflare Durable Objects docs
WebSockets are long-lived TCP connections that enable bi-directional, real-time communication between client and server. Both Cloudflare Durable Objects and Workers can act as WebSocket endpoints – either as a client or as a server. Because WebSocket sessions are long-lived, applications commonly use Durable Objects to accept either the client o...