Skip to content

Commit 6fed1ab

Browse files
joshthowardOxyjun
andauthored
Move reference documentation out of Durable Objects best practices and other fixes (#17445)
* Fix multiple parameters sharing bullet in Durable Object Namespace * Update Durable Objects walkthrough to use RPC * Add partial for id to string example * Move reference documentation out of Durable Objects best practices * Apply suggestions from code review * fixup! Move reference documentation out of Durable Objects best practices --------- Co-authored-by: Jun Lee <[email protected]>
1 parent 63dbb9c commit 6fed1ab

File tree

15 files changed

+446
-452
lines changed

15 files changed

+446
-452
lines changed

src/content/docs/durable-objects/api/id.mdx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ sidebar:
55
order: 2
66
---
77

8-
import { Tabs, TabItem } from "~/components";
8+
import { Render, Tabs, TabItem } from "~/components";
99

1010
## Description
1111

12-
The `DurableObjectId` interface refers to a new or existing Durable Object instance. This interface is most frequently used by [`DurableObjectNamespace::get`](/durable-objects/api/namespace/#get) to obtain a stub for submitting requests to a Durable Object instance.
12+
A Durable Object ID is a 64-digit hexadecimal number used to identify a Durable Object instance. Not all 64-digit hex numbers are valid IDs. Durable Object IDs are constructed indirectly via the [`DurableObjectNamespace`](/durable-objects/api/namespace) interface.
1313

14-
Note that creating an ID for a Durable Object instance does not create the Durable Object. The Durable Object is created lazily after calling [`DurableObjectNamespace::get`](/durable-objects/api/namespace/#get) to create a [`DurableObjectStub`](/durable-objects/api/stub) from a `DurableObjectId`. This ensures that objects are not constructed until they are actually accessed.
14+
The `DurableObjectId` interface refers to a new or existing Durable Object instance. This interface is most frequently used by [`DurableObjectNamespace::get`](/durable-objects/api/namespace/#get) to obtain a [`DurableObjectStub`](/durable-objects/api/stub) for submitting requests to a Durable Object instance. Note that creating an ID for a Durable Object instance does not create the Durable Object. The Durable Object is created lazily after creating a stub from a `DurableObjectId`. This ensures that objects are not constructed until they are actually accessed.
1515

16-
:::note[`DurableObjectId`]
16+
:::note[Logging]
1717

1818
If you are experiencing an issue with a particular Durable Object instance, you may wish to log the `DurableObjectId` from your Worker and include it in your Cloudflare support request.
1919

@@ -25,16 +25,7 @@ If you are experiencing an issue with a particular Durable Object instance, you
2525

2626
`toString` converts a `DurableObjectId` to a 64 digit hex string. This string is useful for logging purposes or storing the `DurableObjectId` elsewhere, for example, in a session cookie. This string can be used to reconstruct a `DurableObjectId` via `DurableObjectNamespace::idFromString`.
2727

28-
```js
29-
// Create a new unique ID
30-
const id = env.MY_DURABLE_OBJECT.newUniqueId();
31-
// Convert the ID to a string to be saved elsewhere, e.g. a session cookie
32-
const session_id = id.toString();
33-
34-
...
35-
// Recreate the ID from the string
36-
const id = env.MY_DURABLE_OBJECT.idFromString(session_id);
37-
```
28+
<Render file="example-id-from-string" />
3829

3930
#### Parameters
4031

src/content/docs/durable-objects/api/namespace.mdx

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ sidebar:
55
order: 1
66
---
77

8-
import { Tabs, TabItem } from "~/components";
8+
import { Render, Tabs, TabItem } from "~/components";
99

1010
## Description
1111

12-
The `DurableObjectNamespace` interface is used to obtain a reference to a new or existing Durable Object instance. The interface is accessible from the fetch handler on a Cloudflare Worker via the `env` parameter, which is the standard interface when referencing bindings declared in `wrangler.toml`.
12+
A Durable Object namespace is a set of Durable Object instances that are backed by the same Durable Object class. There is only one Durable Object namespace per class. A Durable Object namespace can contain any number of Durable Object instances.
13+
14+
The `DurableObjectNamespace` interface is used to obtain a reference to new or existing Durable Object instances. The interface is accessible from the fetch handler on a Cloudflare Worker via the `env` parameter, which is the standard interface when referencing bindings declared in `wrangler.toml`.
1315

1416
This interface defines several [methods](/durable-objects/api/namespace/#methods) that can be used to create an ID for a Durable Object instance. Note that creating an ID for a Durable Object instance does not create the Durable Object. The Durable Object is created lazily after calling [`DurableObjectNamespace::get`](/durable-objects/api/namespace/#get) to create a [`DurableObjectStub`](/durable-objects/api/stub) from a `DurableObjectId`. This ensures that objects are not constructed until they are actually accessed.
1517

@@ -69,7 +71,7 @@ export default {
6971

7072
### `idFromName`
7173

72-
`idFromName` creates a [`DurableObjectId`](/durable-objects/api/id) which refers to an individual instance of the Durable Object class from a particular name.
74+
`idFromName` creates a unique [`DurableObjectId`](/durable-objects/api/id) which refers to an individual instance of the Durable Object class. Named Durable Object instances are the most common method of referring to Durable Object instances.
7375

7476
```js
7577
const fooId = env.MY_DURABLE_OBJECT.idFromName("foo");
@@ -86,16 +88,18 @@ const barId = env.MY_DURABLE_OBJECT.idFromName("bar");
8688

8789
### `newUniqueId`
8890

89-
`newUniqueId` creates a `DurableObjectId` which refers to an individual instance of the Durable Object class.
91+
`newUniqueId` creates a randomly generated and unique [`DurableObjectId`](/durable-objects/api/id) which refers to an individual instance of the Durable Object class. IDs created using `newUniqueId`, will need to be stored as a string in order to refer to the same Durable Object again in the future. For example, the ID can be stored in Workers KV, another Durable Object instance, or in a cookie in the user's browser.
9092

9193
```js
9294
const id = env.MY_DURABLE_OBJECT.newUniqueId();
9395
const euId = env.MY_DURABLE_OBJECT.newUniqueId({ jurisdiction: "eu" });
9496
```
9597

96-
:::note[`newUniqueId`]
98+
:::note[`newUniqueId` results in lower request latency at first use]
99+
100+
The first time you get a Durable Object stub based on an ID derived from a name, the system has to take into account the possibility that a Worker on the opposite side of the world could have coincidentally accessed the same named Durable Object at the same time. To guarantee that only one instance of the Durable Object is created, the system must check that the Durable Object has not been created anywhere else. Due to the inherent limit of the speed of light, this round-the-world check can take up to a few hundred milliseconds. `newUniqueId` can skip this check.
97101

98-
IDs created by `newUniqueId` will result in lower latencies when getting a [`DurableObjectStub`](/durable-objects/api/stub) relative to [`idFromName`](/durable-objects/api/namespace/#idfromname).
102+
After this first use, the location of the Durable Object instance will be cached around the world so that subsequent lookups are faster.
99103

100104
:::
101105

@@ -109,16 +113,9 @@ IDs created by `newUniqueId` will result in lower latencies when getting a [`Dur
109113

110114
### `idFromString`
111115

112-
`idFromString` creates a [`DurableObjectId`](/durable-objects/api/id) from a previously generated ID that has been converted to a string. This method ensures the ID is valid, for example, it checks that the ID consists of 64 hex digits.
116+
`idFromString` creates a [`DurableObjectId`](/durable-objects/api/id) from a previously generated ID that has been converted to a string. This method throws an exception if the ID is invalid, for example, if the ID was not created from the same `DurableObjectNamespace`.
113117

114-
```js
115-
// Create a new unique ID
116-
const id = env.MY_DURABLE_OBJECT.newUniqueId();
117-
// Save the unique ID elsewhere, e.g. a session cookie via id.toString()
118-
...
119-
// Recreate the ID from the string
120-
const id = env.MY_DURABLE_OBJECT.idFromString(session_id);
121-
```
118+
<Render file="example-id-from-string" />
122119

123120
#### Parameters
124121

@@ -132,14 +129,17 @@ const id = env.MY_DURABLE_OBJECT.idFromString(session_id);
132129

133130
`get` obtains a [`DurableObjectStub`](/durable-objects/api/stub) from a [`DurableObjectId`](/durable-objects/api/id) which can be used to invoke methods on a Durable Object instance.
134131

132+
This method returns the stub immediately, often before a connection has been established to the Durable Object instance. This allows requests to be sent to the instance right away, without waiting for a network round trip.
133+
135134
```js
136135
const id = env.MY_DURABLE_OBJECT.newUniqueId();
137136
const stub = env.MY_DURABLE_OBJECT.get(id);
138137
```
139138

140139
#### Parameters
141140

142-
- A required [`DurableObjectId`](/durable-objects/api/id) and an optional object with the key `locationHint` and value of a [locationHint](/durable-objects/reference/data-location/#provide-a-location-hint) string.
141+
- A required [`DurableObjectId`](/durable-objects/api/id)
142+
- An optional object with the key `locationHint` and value of a [locationHint](/durable-objects/reference/data-location/#provide-a-location-hint) string.
143143

144144
#### Return values
145145

@@ -165,4 +165,3 @@ const euId = subnamespace.idFromName("foo");
165165
## Related resources
166166

167167
- [Durable Objects: Easy, Fast, Correct – Choose Three](https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/).
168-
- [Durable Objects best practices](/durable-objects/best-practices/access-durable-objects-from-a-worker/).

src/content/docs/durable-objects/api/stub.mdx

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,17 @@ sidebar:
55
order: 3
66
---
77

8-
import { Tabs, TabItem } from "~/components";
8+
import { Render } from "~/components";
99

1010
## Description
1111

12-
The `DurableObjectStub` interface is used to obtain a reference a Durable Object instance and invoke methods on that instance. The type of `DurableObjectStub` is generic to allow for RPC methods to be invoked on the stub.
12+
The `DurableObjectStub` interface is a client used to invoke methods on a remote Durable Object instance. The type of `DurableObjectStub` is generic to allow for RPC methods to be invoked on the stub.
1313

14-
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
14+
Durable Objects implement E-order semantics, a concept deriving from the [E distributed programming language](<https://en.wikipedia.org/wiki/E_(programming_language)>). When you make multiple calls to the same Durable Object, it is guaranteed that the calls will be delivered to the remote Durable Object in the order in which you made them. E-order semantics makes many distributed programming problems easier. E-order is implemented by the [Cap'n Proto](https://capnproto.org) distributed object-capability RPC protocol, which Cloudflare Workers uses for internal communications.
1515

16-
```js
17-
import { DurableObject } from "cloudflare:workers";
18-
19-
// Durable Object
20-
export class MyDurableObject extends DurableObject {
21-
constructor(ctx, env) {
22-
super(ctx, env);
23-
}
24-
25-
sayHello() {
26-
return "Hello, World!";
27-
}
28-
}
29-
30-
// Worker
31-
export default {
32-
async fetch(request, env) {
33-
// Every unique ID refers to an individual instance of the Durable Object class
34-
const id = env.MY_DURABLE_OBJECT.idFromName("foo");
35-
36-
// A stub is a client used to invoke methods on the Durable Object instance
37-
const stub = env.MY_DURABLE_OBJECT.get(id);
38-
39-
// Methods on the Durable Object are invoked via the stub
40-
const rpcResponse = stub.sayHello();
41-
42-
return new Response(rpcResponse);
43-
},
44-
};
45-
```
46-
47-
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
48-
49-
```ts
50-
import { DurableObject } from "cloudflare:workers";
51-
52-
export interface Env {
53-
MY_DURABLE_OBJECT: DurableObjectNamespace<MyDurableObject>;
54-
}
55-
56-
// Durable Object
57-
export class MyDurableObject extends DurableObject {
58-
constructor(ctx: DurableObjectState, env: Env) {
59-
super(ctx, env);
60-
}
61-
62-
async sayHello(): String {
63-
return "Hello, World!";
64-
}
65-
}
66-
67-
// Worker
68-
export default {
69-
async fetch(request, env) {
70-
// Every unique ID refers to an individual instance of the Durable Object class
71-
const id = env.MY_DURABLE_OBJECT.idFromName("foo");
72-
73-
// A stub is a client used to invoke methods on the Durable Object instance
74-
const stub = env.MY_DURABLE_OBJECT.get(id);
75-
76-
// Methods on the Durable Object are invoked via the stub
77-
const rpcResponse = await stub.sayHello();
78-
79-
return new Response(rpcResponse);
80-
},
81-
} satisfies ExportedHandler<Env>;
82-
```
16+
If an exception is thrown by a Durable Object stub all in-flight calls and future calls will fail with [exceptions](/durable-objects/observability/troubleshooting/). To continue invoking methods on a remote Durable Object instance a Worker must recreate the stub. There are no ordering guarantees between different stubs.
8317

84-
</TabItem> </Tabs>
18+
<Render file="example-rpc" />
8519

8620
## Properties
8721

src/content/docs/durable-objects/best-practices/access-durable-objects-from-a-worker.mdx

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)