A Durable Object is a single-threaded actor with its own SQLite database. On
one machine, workerd keeps the databases in .wrangler/state/workerd/, or
in the directory that --data-dir names. In a cluster, workerd copies each
database to the bucket, and any instance can serve an object. Read the
Cloudflare Durable Objects documentation
for the API.
Example
The counter example keeps a counter in the
key-value storage of a Durable Object, and idFromName() gives each name
its own counter. The Worker reaches the class through ctx.exports.Counter,
so it needs no binding.
export class Counter {
constructor(state, env) {
this.state = state;
}
async fetch(request) {
let n = (await this.state.storage.get("n")) ?? 0;
n++;
await this.state.storage.put("n", n);
return Response.json({ n, url: request.url });
}
}
export default {
async fetch(request, env, ctx) {
const name = new URL(request.url).searchParams.get("name") ?? "default";
const id = ctx.exports.Counter.idFromName(name);
return ctx.exports.Counter.get(id).fetch(request);
},
};{
"name": "counter",
"main": "index.js",
"compatibility_date": "2026-01-01",
"no_bundle": true,
"exports": {
"Counter": { "type": "durable-object", "storage": "sqlite" }
}
}npx workerd serve ./counter/wrangler.jsonc
curl 'http://localhost:8787/?name=alice'Class declarations
The exports map in wrangler.jsonc declares each Durable Object class.
The map gives the current state, with no tags and no history.
"exports": {
"Counter": { "type": "durable-object", "storage": "sqlite" },
"Session": { "type": "durable-object", "storage": "legacy-kv" },
"Old": { "type": "durable-object", "state": "deleted" },
"Room": {
"type": "durable-object",
"state": "renamed",
"renamed_to": "Chat"
},
"Chat": { "type": "durable-object", "storage": "sqlite" }
}- A new entry creates the class. It must set
storagetosqliteorlegacy-kv. "state": "deleted"removes the class. Its objects stay on disk."state": "renamed"withrenamed_tomoves the objects to the new class. The new class needs its own entry.
The migrations array also works. One configuration cannot declare Durable
Objects in both exports and migrations.
ctx.exports.Counter is the namespace of a declared class. It is available
from compatibility date 2025-11-17. A durable_objects.bindings entry,
with name and class_name, puts a namespace in env instead.
In a cluster
Each object has one leader at a time, the instance that serves it, and the other instances forward its calls there. The leader answers a write after the bucket holds it. When the leader stops or its lease expires, another instance restores the object from the bucket. See What workerd guarantees.
Differences from Cloudflare
- workerd makes no placement, location, or jurisdiction promise.
newUniqueId({ jurisdiction })andnamespace.jurisdiction()throw. - The key of each namespace contains the Worker
name, so a Worker with a new name reaches new, empty objects. The old objects stay on disk. durable_objects.bindingscannot name a class of another Worker. workerd refusesscript_name.
See also Cloudflare compatibility.