Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active May 21, 2021 02:26
Show Gist options
  • Save xeoncross/7a37c741c2f0eb7b6a93fdc9cd720593 to your computer and use it in GitHub Desktop.
Save xeoncross/7a37c741c2f0eb7b6a93fdc9cd720593 to your computer and use it in GitHub Desktop.
Cloudflare Durable Objects

Just saw that cloudflare created https://developers.cloudflare.com/workers/learning/using-durable-objects

Background: Regular lambda/worker/serverless functions are state-less - they have no persistent memory and are created and destroyed often; the are ephemeral.

For those of us that use these, we often default to having a stateful server somewhere (pubsub, websockets, chatroom organizer, rate-limiter, etc..). Often the database or filesystem (lock files) can be used as the manager of state or "source of truth".

Cloudflare basically created long-lived, single-instance lambda functions here. Now they can keep an hash of websocket connections or recent chat messages allowing multiple other short-lived (regular) serverless functions to use them like a regular webserver.

It's the new, old server pretending to be serverless. An expected, but still interesting development.

Make sure to read through the sample chat application to see how this works: https://github.com/cloudflare/workers-chat-demo/blob/master/src/chat.mjs

It's worth noting that this creates a new way to have an auto-scaling server cluster. Instead of how much CPU is being used, you can use these "durable objects" (long-lived servers) to scale out based on partition keys. Each key gets it's own server instance which can result in really unique ways to have ephemeral load solutions.

Cloudflare is also unique in using V8:Isolates https://blog.cloudflare.com/cloud-computing-without-containers/ which solves the problem of cold starts compared to AWS lambda functions or ASG for EC2.

Here's an article about using them to build a fast CAPTCHA service: https://guido.io/posts/going-fully-serverless-with-cloudflare-workers/ including some of the limitations and the choice of external database. (Also checkout out the Macrometa db example)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment