Durable Objects · Exercise 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.
Paste this into your agent
Copied! Now paste it into your agent. No agent yet? Here's how to get one.
One object per name
A Worker runs wherever a request lands, as many copies as needed, and those copies share nothing. That’s great until two requests need to agree on something, like the current value of a counter.
A Durable Object fixes that. You ask for one by name, and every request for that name, from anywhere, reaches the same object, with its own storage right next to its code.
What you’ll build
A Worker with a Counter Durable Object class and a /count route. POST /count?name=a adds one to the counter named a. Every name gets its own counter, and each one keeps its value across requests and deploys.
Prerequisites
Your agent can help you with these.
- Node.js installed.
- A free Cloudflare account. Durable Objects work on the Workers Free plan.
- The
cfCLI logged in to your Cloudflare account (cf auth whoami).