-
Notifications
You must be signed in to change notification settings - Fork 9.9k
[DO] Adding "Concepts" node, adding "DO Lifecycle" chapter #23823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4fdc745
Lifecycle of a DO doc initialisation
Oxyjun 7772336
Adding rest of the text for DO lifecycle chapter
Oxyjun 44562d6
Adding image
Oxyjun e26cfa0
Apply suggestions from code review
Oxyjun 79540a6
Apply suggestions from code review
Oxyjun d00c20c
Making states consistent in formatting.
Oxyjun 8307c41
Typo
Oxyjun 1de19dd
Replacing image to fix arrow
Oxyjun 81350e0
Apply suggestions from code review
Oxyjun fe27f5c
Resolving feedback
Oxyjun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/content/docs/durable-objects/concepts/durable-object-lifecycle.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| --- | ||
| title: The Lifecycle of a Durable Object | ||
| pcx_content_type: concept | ||
| sidebar: | ||
| order: 3 | ||
|
|
||
| --- | ||
|
|
||
| import { Render, TypeScriptExample } from "~/components"; | ||
|
|
||
| This section describes the lifecycle of a [Durable Object](/durable-objects/concepts/what-are-durable-objects/). | ||
|
|
||
| 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: | ||
|
|
||
| ```ts | ||
| // Assume a DurableObjectNamespace binding MY_DURABLE_OBJECT | ||
| // Every unique ID refers to an individual instance of the Durable Object class | ||
| const id = env.MY_DURABLE_OBJECT.idFromName("foo"); | ||
| const stub = env.MY_DURABLE_OBJECT.get(id); | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| The following line invokes the `sayHello()` method of the Durable Object class bound to the `MY_DURABLE_OBJECT` binding: | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```ts | ||
| // All invoked methods need to be awaited. | ||
| const rpcResponse = await stub.sayHello(); | ||
| ``` | ||
|
|
||
| At this point, the caller sends a request to the Durable Object identified by the stub. The lifecycle of the Durable Object begins. | ||
|
|
||
| ## Durable Object Lifecycle state transitions | ||
|
|
||
| A Durable Object can be in one of the following states at any moment: | ||
Oxyjun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| | State | Description | | ||
| |-----------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| | Active in-memory | The Durable Object runs, in memory, and handles incoming requests. | | ||
| | Idle in-memory non-hibernateable | The Durable Object waits for the next incoming request/event, but does not satisfy the criteria for hibernation. | | ||
| | 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. | | ||
| | Hibernated | The Durable Object is removed from memory. | | ||
| | Evicted | The Durable Object is completely removed from the host process. Might need a cold start. | | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| This is how a Durable Object transitions among these states (each state is in a rounded rectangle). | ||
|
|
||
|  | ||
|
|
||
| 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| At this point the Durable Object is in the **Active in-memory state**. | ||
|
|
||
| If it continuously receives requests or events within 10 seconds of each other, the Durable Object will remain in this state. | ||
|
|
||
| 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: | ||
| - No `setTimeout`/`setInterval` scheduled callbacks are set. | ||
| - No in-progress `fetch()` waiting for a remote request exists. | ||
| - No WebSocket standard API is used. | ||
| - No request/event is still being processed. | ||
|
|
||
| If all conditions are met, the Durable Object will transition into a **hibernated** state. | ||
|
|
||
| :::caution | ||
| When hibernated, the in-memory state is discarded, so ensure you persist all important information in the Durable Object's storage. | ||
| ::: | ||
|
|
||
| If any of the above conditions are false, the Durable Object remains in-memory, in the **Idle in-memory non-hibernateable** state. | ||
|
|
||
| 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. | ||
|
|
||
| Once in hibernated state, or 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The next incoming request or event starts the cycle again. | ||
|
|
||
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| title: Concepts | ||
| pcx_content_type: navigation | ||
| sidebar: | ||
| order: 3 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
| import { DirectoryListing } from "~/components"; | ||
|
|
||
| <DirectoryListing /> |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.