Hi everyone, I'm new to durable objects
Hi everyone, I'm new to durable objects and don't understand much from the front page of their docs. (This seems to be a trend with CF; the docs could definitely be more non-expert-friendly. Reading "without requiring you to build serialization and coordination primitives on your own." on the very first page is quite daunting).
I would like to understand what this product is and whether it matches my need. I need something that can power websocket-like behaviour to buld a realtime chat. Previously I've used pusher.com for this. Is that the sort of thing DO is? Or how does it compare to Queues, which, to a layman like me, also sounds sort of like what's needed? Any layman help appreciated! 🙂
3 Replies
Hey! Thanks for the feedback on docs. We're continuously discussing how to improve docs, so all feedback is helpful. I've actually just come off a call where we discussed this very point.
We want the docs to be easily understood to users who're new to DO, but also not impede experienced users from getting to where they want to be.
We'll be regularly pushing updates over the coming weeks, but one helpful place to get you started (for now) would be the DO Glossary
or this blog post
@Mitya this is also useful as a starting point https://blog.cloudflare.com/introducing-workers-durable-objects/
If you're familiar with Cloudflare Workers, you can think of a Durable Object as a special type of worker. Rather than having your requests be handled by any Worker instance, you can have them handled by a particular Durable Object instance. This is what lets you "serialize" (i.e. order requests into a single file line) and "coordinate" (all requests go to the same place) .
Then, you can have a bunch of these individual Durable Object "instances", and can decide where you want to route your requests to.
Since you can route any request to a particular "instance", you can have in-memory state shared between requests. This doesn't work for normal workers because you can't guarantee any 2 requests go to the same worker instance (no way to share memory if you're in different data centers 😉).
Durable Objects also give you access to private local storage through the storage API. If you have any state that isn't ephemeral, you can just write it to DO storage (KV or SQL api -- note this is not Cloudflare KV, which is another product). Anything successfully written to storage is considered durable (we replicate for fault tolerance), so if the machine crashes or your DO is evicted and loses in-memory state, you can read from storage when the next request comes in and you're back to where you were.
The Cloudflare Blog
Workers Durable Objects Beta:
A New Approach to Stateful Serverless
Durable Objects provide a truly serverless approach to storage and state: consistent, low-latency, distributed, yet effortless to maintain and scale. They also enable coordination and real-time collaboration between clients.
Thanks for this! That's a great explanation and the one that I wish was in the docs. I am indeed familiar with Workers so this analogy is really helpful.