What problem does it solve?
Durable Objects provide a consistent, low-latency way to manage per-entity state and coordination at the Cloudflare edge, eliminating race conditions and complexity when building chat rooms, multiplayer sessions, booking systems, and other stateful features that require strong consistency.
Core Features & Use Cases
- Deterministic routing: Use getByName or idFromName to ensure the same input maps to the same DO instance for chat rooms, game sessions, or tenant-scoped state.
- Durable storage & migrations: Prefer SQLite storage with migrations (new_sqlite_classes) for durable per-entity data and schema evolution.
- Safe initialization & concurrency: Initialize schema and migrations inside blockConcurrencyWhile in the constructor and avoid holding it during external I/O.
- RPC, alarms & WebSockets: Implement typed RPC methods for direct calls, use setAlarm/deleteAlarm for scheduled work, and accept WebSocket connections for persistent streams.
- Testing & tooling: Configure wrangler and use @cloudflare/vitest-pool-workers for unit and integration tests, run alarms with runDurableObjectAlarm, and verify storage with runInDurableObject.
Use cases include chat room message coordination, per-tenant counters and quotas, turn-based game match state, and scheduled cleanup or billing tasks.
Quick Start
Create a Durable Object class for a chat room that uses SQLite for persistent messages, exposes RPC methods sendMessage and getMessages, initializes its schema with blockConcurrencyWhile, and includes a Vitest test that verifies isolation between instances.