Skip to content

Commit d9682a2

Browse files
Oxyjunlambrospetrouvy-ton
authored
[DO] Adding "Concepts" node, adding "DO Lifecycle" chapter (#23823)
* Lifecycle of a DO doc initialisation * Adding rest of the text for DO lifecycle chapter * Adding image * Apply suggestions from code review Co-authored-by: Lambros Petrou <[email protected]> * Apply suggestions from code review Co-authored-by: Lambros Petrou <[email protected]> * Making states consistent in formatting. * Typo * Replacing image to fix arrow * Apply suggestions from code review Co-authored-by: Vy Ton <[email protected]> * Resolving feedback --------- Co-authored-by: Lambros Petrou <[email protected]> Co-authored-by: Vy Ton <[email protected]>
1 parent 69ce891 commit d9682a2

File tree

11 files changed

+97
-6
lines changed

11 files changed

+97
-6
lines changed

public/__redirects

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@
495495
/durable-objects/get-started/video-series/serverless-websocket/ /durable-objects/video-tutorials/ 301
496496
/durable-objects/get-started/video-series/ /durable-objects/video-tutorials/ 301
497497

498+
/durable-objects/what-are-durable-objects/ /durable-objects/concepts/what-are-durable-objects/ 301
499+
498500
# email-routing
499501
/email-routing/enable-email-routing/ /email-routing/get-started/enable-email-routing/ 301
500502
/email-routing/get-started/email-addresses/ /email-routing/setup/email-routing-addresses/ 301
245 KB
Loading

src/content/changelog/durable-objects/2025-04-07-durable-objects-free-tier.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ Free plan [limits](/durable-objects/platform/pricing/) apply to Durable Objects
4545

4646
For more information, checkout:
4747

48-
- [Documentation](/durable-objects/what-are-durable-objects/)
48+
- [Documentation](/durable-objects/concepts/what-are-durable-objects/)
4949
- [Zero-latency SQLite storage in every Durable Object blog](https://blog.cloudflare.com/sqlite-in-durable-objects/)

src/content/changelog/durable-objects/2025-06-25-actors-package-alpha.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The new [@cloudflare/actors](https://www.npmjs.com/package/@cloudflare/actors) l
1111

1212
The `@cloudflare/actors` library is a new SDK for Durable Objects and provides a powerful set of abstractions for building real-time, interactive, and multiplayer applications on top of Durable Objects. With beta uasge and feedback, `@cloudflare/actors` will become the recommended way to build on Durable Objects and draws upon Cloudflare's experience building products/features on Durable Objects.
1313

14-
The name "actors" originates from the [actor programming model](/durable-objects/what-are-durable-objects/#actor-programming-model), which closely ties to how Durable Objects are modelled.
14+
The name "actors" originates from the [actor programming model](/durable-objects/concepts/what-are-durable-objects/#actor-programming-model), which closely ties to how Durable Objects are modelled.
1515

1616
The `@cloudflare/actors` library includes:
1717

src/content/changelog/workers/2025-05-14-python-worker-durable-object.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ You can now create [Durable Objects](/durable-objects/) using
1313
[Python Workers](/workers/languages/python/). A Durable Object is a special kind of
1414
Cloudflare Worker which uniquely combines compute with storage, enabling stateful
1515
long-running applications which run close to your users. For more info see
16-
[here](https://developers.cloudflare.com/durable-objects/what-are-durable-objects/).
16+
[here](/durable-objects/concepts/what-are-durable-objects/).
1717

1818
You can define a Durable Object in Python in a similar way to JavaScript:
1919

src/content/docs/agents/platform/limits.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Many limits are inherited from those applied to Workers scripts and/or Durable O
2222

2323
---
2424

25-
[^1]: Yes, really. You can have tens of millions of Agents running concurrently, as each Agent is mapped to a [unique Durable Object](/durable-objects/what-are-durable-objects/) (actor).
25+
[^1]: Yes, really. You can have tens of millions of Agents running concurrently, as each Agent is mapped to a [unique Durable Object](/durable-objects/concepts/what-are-durable-objects/) (actor).
2626
[^2]: You can deploy up to [500 scripts per account](/workers/platform/limits/), but each script (project) can define multiple Agents. Each deployed script can be up to 10 MB on the [Workers Paid Plan](/workers/platform/pricing/#workers)
2727
[^3]: Compute (CPU) time per Agent is limited to 30 seconds, but this is refreshed when an Agent receives a new HTTP request, runs a [scheduled task](/agents/api-reference/schedule-tasks/), or an incoming WebSocket message.
2828

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Lifecycle of a Durable Object
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 3
6+
7+
---
8+
9+
import { Render, TypeScriptExample } from "~/components";
10+
11+
This section describes the lifecycle of a [Durable Object](/durable-objects/concepts/what-are-durable-objects/).
12+
13+
To use a Durable Object you need to create a [Durable Object Stub](/durable-objects/api/stub/). In its simplest form, this looks like the following snippet:
14+
15+
```ts
16+
// Assume a DurableObjectNamespace binding MY_DURABLE_OBJECT
17+
// Every unique ID refers to an individual instance of the Durable Object class
18+
const id = env.MY_DURABLE_OBJECT.idFromName("foo");
19+
const stub = env.MY_DURABLE_OBJECT.get(id);
20+
```
21+
22+
Once we have the Durable Object Stub, we can now invoke methods on the Durable Object. Note that the above two lines do not yet send any request to the remote Durable Object.
23+
24+
The following line invokes the `sayHello()` method (which is an [RPC method](/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#invoke-rpc-methods)) of the Durable Object class bound to the `MY_DURABLE_OBJECT` binding:
25+
26+
```ts
27+
// All invoked methods need to be awaited.
28+
const rpcResponse = await stub.sayHello();
29+
```
30+
31+
At this point, the caller sends a request to the Durable Object identified by the stub. The lifecycle of the Durable Object begins.
32+
33+
## Durable Object Lifecycle state transitions
34+
35+
A Durable Object can be in one of the following states at any moment:
36+
37+
| State | Description |
38+
|--------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
39+
| **Active, in-memory** | The Durable Object runs, in memory, and handles incoming requests. |
40+
| **Idle, in-memory non-hibernateable** | The Durable Object waits for the next incoming request/event, but does not satisfy the criteria for hibernation. |
41+
| **Idle, in-memory hibernateable** | The Durable Object waits for the next incoming request/event and satisfies the criteria for hibernation. It is up to the runtime to decide when to hibernate the Durable Object. Currently, it is after 10 seconds of inactivity while in this state. |
42+
| **Hibernated** | The Durable Object is removed from memory. Hibernated WebSocket connections stay connected. |
43+
| **Inactive** | The Durable Object is completely removed from the host process and might need to cold start. This is the initial state of all Durable Objects. |
44+
45+
This is how a Durable Object transitions among these states (each state is in a rounded rectangle).
46+
47+
![Lifecycle of a Durable Object](~/assets/images/durable-objects/lifecycle-of-a-do.png)
48+
49+
Assuming a Durable Object does not run, the first incoming request or event (like an alarm) will execute the `constructor()` of the Durable Object class, then run the corresponding function invoked.
50+
51+
At this point the Durable Object is in the **active in-memory state**.
52+
53+
If it continuously receives requests or events within 10 seconds of each other, the Durable Object will remain in this state.
54+
55+
After 10 seconds of no incoming request or events, the runtime can now hibernate the Durable Object. Hibernation will only occur if **all** of the below are true:
56+
- No `setTimeout`/`setInterval` scheduled callbacks are set.
57+
- No in-progress `fetch()` waiting for a remote request exists.
58+
- No WebSocket standard API is used.
59+
- No request/event is still being processed.
60+
61+
If all conditions are met, the Durable Object will transition into a **hibernated** state.
62+
63+
:::caution
64+
When hibernated, the in-memory state is discarded, so ensure you persist all important information in the Durable Object's storage.
65+
:::
66+
67+
If any of the above conditions are false, the Durable Object remains in-memory, in the **idle, in-memory, non-hibernateable** state.
68+
69+
In case of an incoming request or event while in the **hibernated** state, the `constructor()` will run again, and the corresponding function invoked will run.
70+
71+
While in the **idle, in-memory, non-hibernateable** state, after 70-140 seconds of inactivity (no incoming requests or events), the Durable Object will be evicted entirely from memory and potentially from the Cloudflare host and transition to the **inactive** state.
72+
73+
Objects in the **hibernated** state keep their Websocket clients connected, and the runtime decides if and when to move the object to a different host, thus restarting the lifecycle.
74+
75+
The next incoming request or event starts the cycle again.
76+
77+
As explained in [When does a Durable Object incur duration charges?](/durable-objects/platform/pricing/#when-does-a-durable-object-incur-duration-charges), a Durable Object incurs charges only when it is **actively running in-memory**, or when it is **idle in-memory and non-hibernateable**, the two states highlighted in red in the above diagram.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Concepts
3+
pcx_content_type: navigation
4+
sidebar:
5+
order: 3
6+
group:
7+
hideIndex: true
8+
---
9+
10+
import { DirectoryListing } from "~/components";
11+
12+
<DirectoryListing />
File renamed without changes.

src/content/docs/durable-objects/get-started.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This guide will instruct you through:
1414
- Instantiating and communicating with a Durable Object from another Worker.
1515
- Deploying a Durable Object and a Worker that communicates with a Durable Object.
1616

17-
If you wish to learn more about Durable Objects, refer to [What are Durable Objects?](/durable-objects/what-are-durable-objects/).
17+
If you wish to learn more about Durable Objects, refer to [What are Durable Objects?](/durable-objects/concepts/what-are-durable-objects/).
1818

1919
## Quick start
2020

0 commit comments

Comments
 (0)