Durable Objects in Dynamic Workers: Give each AI-generated app its own database
Blog post from Cloudflare
Dynamic Workers, a new feature of the Workers platform, allows users to load Worker code dynamically into a secure sandbox using the Dynamic Worker Loader API, which operates on isolates rather than containers for faster and more efficient execution. These isolates are lightweight, loading 100 times faster and using only one-tenth of the memory compared to traditional containers, making them suitable for executing single-use AI-generated code securely. However, for use cases requiring persistent code and long-lived state, Dynamic Workers can be paired with Durable Objects, which are unique Workers with attached SQLite databases offering near-zero latency storage access. The challenge arises when integrating Dynamic Workers with Durable Objects, as the latter requires provisioning through the Cloudflare API, which cannot directly reference a Dynamic Worker. To address this, the newly released feature, Durable Object Facets, allows dynamic instantiation of Durable Object classes while maintaining separate storage for each facet. This approach enables developers to write supervisors that manage requests and logistics before passing them to the agent's code, ensuring control over object creation, storage usage, and observability. Each Durable Object with Facets consists of isolated SQLite databases for both the parent and the facet, ensuring data separation and security.