Durafetch
What Cloudflare Durable Objects are.
Cloudflare Workers allow you to upload JS code to respond to HTTP requests that run in 200+ globally distributed datacenters. This reduces user-to-server latency to less than 50ms regardless of user location.
Edge hosting solutions do not handle state well as there is no central location.
In typical web servers state is stored in a central location. This is the simplest possible set-up (single web server, single database), but has a few disadvantages:
- Will only have low latency for users close by. Latency can be 300ms for the opposite side of the world.
- Must be always-on to handle those requests (a small VM is around $12/month).
- Is a single point of failure.
- You must manage the OS updates, CPU/RAM/disk sizing etc yourself.
Durable Objects are defined in JS classes that are bundled with a Cloudflare Worker. The worker acts as a proxy to receive public HTTP requests and then instantiates a Durable Object to pass the HTTP request to.
Durable Object’s have these features:
- A single identity
- You can name a specific server instance with an ID and route requests to the same instance.
- This allows you to keep JS state in memory.
- Persistent storage.
- A JS key value datastore, like
localStorage
that is written to disk.
- A JS key value datastore, like
- WebSockets.
- You can upgrade the HTTP request to a long-lived WebSocket for bidirectional communication.
- This allows the WebSocket to interact with JS and KV stored state.
What Durafetch is.
Durafetch consists of two parts:
- A CLI
- Server functions you add you your Cloudflare Worker/Durable Object class.
A missing feature of Durable Objects is the ability to inspect and observe state outside of the JS API - you cannot access your data unless your write the HTTP endpoints yourself.
Durafetch:
- Implements those HTTP endpoints
- Implements a CLI to fetch the data from the endpoints, and then write them to a SQLite database
The two main use cases I have in mind are:
-
Development
- When developing you need to refresh and run code paths repeatedly as you modify the code. Not being able to see what the state of the storage is makes this very difficult as you have to either print the values to a console, or write your own solution to read the state.
- Even if you can read the state, you often need to filter/query it. SQL allows you to do this.
-
Production
- Accessing and querying your server based state. E.g. How many sign-ups, sales. Usage based billing etc.
Why D1 cannot be used (for now)
Cloudflare have a SQLite based persistence API called D1 that looks good.
- D1 is in Alpha, and looks like it will be in GA in around 12 months which is too faraway.
- D1 is still distributed.
- Each Durable Object gets its own SQLite instance, which means you will still need to join them into one central DB to do a global query.
Take a look
Durafetch is described in more detail at durafetch.com or the Github repo.