Cloudflare tutor

A morning desk for Kotha · Gold Coast

· Australia/Brisbane · Lesson

Durable Objects: one small stateful helper that remembers between requests

Durable Objects (stateful Workers coordination)

What it is

A Durable Object is a small piece of your app that Cloudflare keeps alive with its own memory and storage. Unlike an ordinary Worker, which starts fresh on every request, each Durable Object has a unique ID and can keep state between requests: a counter, a room full of chat messages, a lock so only one job runs at a time, or a live connection list for people looking at the same page.

You still write a Worker as the front door. That Worker looks up a Durable Object by name or ID through a binding (for example env.COUNTER), then calls methods or forwards the request with stub.fetch(). In wrangler.toml you declare the binding under [[durable_objects.bindings]], point it at a class name in your script, and add a [[migrations]] block the first time you introduce that class so Cloudflare knows how to create it.

Each object is single-threaded: Cloudflare sends its requests one after another, so you do not get two updates fighting over the same counter. Storage on the object (the storage API) is strongly consistent for that object. That is the opposite feel of Workers KV, which is eventually consistent across the world. Durable Objects need the Workers Paid plan for production use; check the dashboard and pricing page for current limits before you rely on them.

Why it matters

KV, D1, R2, and Queues each solve a different storage or background job. None of them give you a single place that coordinates many visitors in real time. Durable Objects fill that gap: one object per room, per user session, or per shared resource, with memory that lasts across requests.

They are also the right tool when "exactly once" or "only one at a time" matters. A heartbeat checker that must not run two overlapping scans, a booking slot that must not be double-booked, or a live board where everyone should see the same number, all fit the Durable Object shape better than a plain Worker plus KV.

The caution: they are more moving parts than a static lesson page. You manage class names, migrations, and object IDs carefully. Never put identifiable patient data in a learning object, and do not attach a Durable Object class to a live Worker while you are still learning.

On your account

From your own setup: a live status board for freehosts.kosan.xyz or a VPS heartbeat could use one Durable Object per site name. The cron or queue consumer would call that object's stub to record the latest check, and the page Worker would ask the same object for the current status, so every visitor sees one shared answer.

A collaborative notes or triage whiteboard on kosan.xyz is another natural fit: one Durable Object per room ID, holding the current draft text and a list of open WebSocket connections, so edits from one browser reach the others without a separate server.

This tutor, cloudflare.kosan.xyz, served by the cloudflare-tutor Worker, has no Durable Object binding; its lessons are fixed text in worker.js. Nothing in this lesson creates a class, migration, or binding on cloudflare-tutor, ainews, aiweekly, or any other app.

Do this yourself

These stay in the dashboard, on your side. This site does not change DNS, routes, or other Workers.

  1. In the Cloudflare dashboard, open Workers & Pages, then Durable Objects (wording may differ slightly under Storage & Databases or the Worker bindings area), and just look: note whether any Durable Object namespaces or classes already exist and which Workers they are bound to. On paper, pick one small idea from your kosan.xyz apps (for example a shared counter, a per-site heartbeat lock, or a live room) and write the object name, what state it would hold, and which Worker would call it. Do not create a Durable Object namespace, add a binding, run a migration, or deploy anything to cloudflare-tutor, ainews, aiweekly, or any live app.