Durable Objects
Learn what a Durable Object is and when you need one: a small piece of compute with its own storage, addressed by name, that every request for that name reaches. Build a counter that can't lose updates, a message store in SQLite, and a real-time room over WebSockets. For students who've deployed a Worker.
Durable Objects
A Worker runs wherever a request lands, and any number of copies can run at once. That’s what makes it fast and cheap, but it also means two requests can’t easily agree on anything: there’s no one place where “the current count” or “everyone in this chat room” lives.
A Durable Object is that one place. You give it a name, and every request for that name, from anywhere in the world, reaches the same object, with its own private storage right next to its code.
In this course you’ll build one Worker with two kinds of Durable Objects: a counter that stays correct under a burst of simultaneous requests, and a room that stores messages in its own SQLite database and broadcasts them to everyone connected over WebSockets. Your agent checks each step itself before marking it complete.
Exercises
- 00.Interview: Tell your agent what you already know about Durable Objects.✓
- 01.One object per name: Create a Durable Object class, reach one object per name from your Worker, and see that each name keeps its own state across requests and deploys.✓
- 02.No lost updates: Send 50 simultaneous increments to your Durable Object counter and to a KV-based counter, and see which one loses updates.✓
- 03.Store data with SQL: Give a second Durable Object class its own SQLite database, store messages in it with SQL, and see that every object's database is separate.✓
- 04.Talk in real time: Connect clients to a room over WebSockets, broadcast every message to everyone in it, and let the room sleep between messages without dropping anyone.✓
Paste this into your agent
Copied! Now paste it into your agent. No agent yet? Here's how to get one.