Skip to main content
Reference vendor/cloudflare 2 min read

rules

Durable Objects Rules & Best Practices

Choose one object per entity that needs coordinated state. Keep essential data in durable storage; in-memory state must be reconstructible. Prefer SQLite for new classes, and inspect the backend of existing classes before selecting APIs. For idle WebSocket servers, prefer hibernation and plan for state restoration.

Fetch the relevant current documentation before implementing or reviewing changes.

TaskDocumentation
Choose object boundaries, deterministic routing, parent-child relationships, or initializationRules of Durable Objects
Choose SQLite or maintain an existing KV-backed classSQLite storage API; Legacy KV storage API
Review storage gates, external I/O races, transactions, or schema initializationRules of Durable Objects; SQLite storage API; Durable Object State
Configure class lifecycle changesClass exports; Legacy class migrations
Set placement hints or jurisdiction constraintsData location
Create stubs, invoke RPC, or use HTTP handlersInvoke methods; Namespace API
Schedule per-object work and handle retriesAlarms
Restore WebSocket connection state after hibernationUse WebSockets
Handle exceptions, restarts, and shutdownsError handling; Object lifecycle

For verification, use Testing Durable Objects. Keep API signatures, configuration, limits, and implementation examples in the linked docs.