I recently came across a YouTube video about Aaron Francis talking about Durable Objects with Cloudflare's Josh Howard (https://www.youtube.com/watch?v=C5-741uQPVU), and it was extremely interesting to me. I've always been fascinated by the concept of a "one tenant, one database" structure, and in that video, it seems like Durable Objects are a perfect solution for this pattern.
My understanding of Durable Objects is that they are essentially Cloudflare Workers with enhanced capabilities: they maintain state and have access to storage systems like KV and D1. This makes them perfect for building APIs and real-time apps that need WebSocket-like functionalities.
Unlike traditional Cloudflare Workers, which are stateless, Durable Objects provide:
Each Durable Object instance maintains its own in-memory state that persists across requests until the end of the lifecycle of the object. This is useful for storing data that needs to be accessed by multiple requests, such as:
All requests to a specific Durable Object are routed to the same instance, ensuring consistent state
Direct integration with Cloudflare's storage services (KV, D1, R2)
The object is created near to the first request of the user
The "one tenant, one database" pattern becomes incredibly elegant with Durable Objects, and making scalable SaaS applications easier. Instead of managing complex database sharding or partitioning logic, you can:
Durable Objects excel in scenarios that require:
Chat rooms, collaborative editing, live updates. Imagine you have one web socket server per each tenant, and you do not need to consider the scalability of the web socket server.
Isolating data and logic per tenant. For example, you can have one Durable Object instance per tenant, and each instance will have its own state and storage. Scalability is also easier to manage.
To demonstrate Durable Objects in practice, I built a collaborative note-taking application that showcases the multi-tenancy pattern.
Tech Stack:
Links:
Each organization gets its own Durable Object instance, which manages:
Item records, where each item represents a simple record with a titleThe Cloudflare Worker serves three main purposes: