You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These examples exclude the costs for the Workers calling the Durable Objects. When modelling the costs of a Durable Object, note that:
23
28
24
-
* Inactive objects receiving no requests do not incur any duration charges.
25
-
* The [WebSocket Hibernation API](/durable-objects/best-practices/websockets/#websocket-hibernation-api) can dramatically reduce duration-related charges for Durable Objects communicating with clients over the WebSocket protocol, especially if messages are only transmitted occasionally at sparse intervals.
29
+
- Inactive objects receiving no requests do not incur any duration charges.
30
+
- The [WebSocket Hibernation API](/durable-objects/best-practices/websockets/#websocket-hibernation-api) can dramatically reduce duration-related charges for Durable Objects communicating with clients over the WebSocket protocol, especially if messages are only transmitted occasionally at sparse intervals.
26
31
27
32
### Example 1
28
33
29
34
This example represents a simple Durable Object used as a co-ordination service invoked via HTTP.
30
35
31
-
* A single Durable Object was called by a Worker 1.5 million times
32
-
* It is active for 1,000,000 seconds in the month
36
+
- A single Durable Object was called by a Worker 1.5 million times
37
+
- It is active for 1,000,000 seconds in the month
33
38
34
39
In this scenario, the estimated monthly cost would be calculated as:
35
40
36
41
**Requests**:
37
42
38
-
* (1.5 million requests - included 1 million requests) x $0.15 / 1,000,000 = $0.075
43
+
- (1.5 million requests - included 1 million requests) x $0.15 / 1,000,000 = $0.075
This example represents a moderately trafficked Durable Objects based application using WebSockets to broadcast game, chat or real-time user state across connected clients:
50
55
51
-
* 100 Durable Objects have 50 WebSocket connections established to each of them.
52
-
* Clients send approximately one message a minute for eight active hours a day, every day of the month.
56
+
- 100 Durable Objects have 50 WebSocket connections established to each of them.
57
+
- Clients send approximately one message a minute for eight active hours a day, every day of the month.
53
58
54
59
In this scenario, the estimated monthly cost would be calculated as:
55
60
56
61
**Requests**:
57
62
58
-
* 50 WebSocket connections \* 100 Durable Objects to establish the WebSockets = 5,000 connections created each day \* 30 days = 150,000 WebSocket connection requests.
* 150,000 + (72 million requests / 20 for WebSocket message billing ratio) = 3.75 million billing request.
61
-
* (3.75 million requests - included 1 million requests) x $0.15 / 1,000,000 = $0.41.
63
+
- 50 WebSocket connections \* 100 Durable Objects to establish the WebSockets = 5,000 connections created each day \* 30 days = 150,000 WebSocket connection requests.
This example represents a horizontally scaled Durable Objects based application using WebSockets to communicate user-specific state to a single client connected to each Durable Object.
74
79
75
-
* 100 Durable Objects each have a single WebSocket connection established to each of them.
76
-
* Clients sent one message every second of the month so that the Durable Objects were active for the entire month.
80
+
- 100 Durable Objects each have a single WebSocket connection established to each of them.
81
+
- Clients sent one message every second of the month so that the Durable Objects were active for the entire month.
77
82
78
83
In this scenario, the estimated monthly cost would be calculated as:
79
84
80
85
**Requests**:
81
86
82
-
* 100 WebSocket connection requests.
83
-
* 1 message per second \* 100 connections \* 60 seconds \* 60 minutes \* 24 hours \* 30 days = 259,200,000 WebSocket message requests.
84
-
* 100 + (259.2 million requests / 20 for WebSocket billing ratio) = 12,960,100 requests.
85
-
* (12.9 million requests - included 1 million requests) x $0.15 / 1,000,000 = $1.79.
87
+
- 100 WebSocket connection requests.
88
+
- 1 message per second \* 100 connections \* 60 seconds \* 60 minutes \* 24 hours \* 30 days = 259,200,000 WebSocket message requests.
89
+
- 100 + (259.2 million requests / 20 for WebSocket billing ratio) = 12,960,100 requests.
90
+
- (12.9 million requests - included 1 million requests) x $0.15 / 1,000,000 = $1.79.
This example represents a moderately trafficked Durable Objects based application using WebSocket Hibernation to broadcast game, chat or real-time user state across connected clients:
98
103
99
-
* 100 Durable Objects each have 100 Hibernatable WebSocket connections established to each of them.
100
-
* Clients send one message per minute, and it takes 10ms to process a single message in the `webSocketMessage()` handler. Since each Durable Object handles 100 WebSockets, cumulatively each Durable Object will be actively executing JS for 1 second each minute (100 WebSockets \* 10ms).
104
+
- 100 Durable Objects each have 100 Hibernatable WebSocket connections established to each of them.
105
+
- Clients send one message per minute, and it takes 10ms to process a single message in the `webSocketMessage()` handler. Since each Durable Object handles 100 WebSockets, cumulatively each Durable Object will be actively executing JS for 1 second each minute (100 WebSockets \* 10ms).
101
106
102
107
In this scenario, the estimated monthly cost would be calculated as:
103
108
104
109
**Requests**:
105
110
106
-
* 100 WebSocket connections \* 100 Durable Objects to establish the WebSockets = 10,000 initial WebSocket connection requests.
<sup>1</sup> 100 messages per minute comes from the fact that 100 clients connect to each DO, and each sends 1 message per minute.
124
+
<sup>1</sup> 100 messages per minute comes from the fact that 100 clients
125
+
connect to each DO, and each sends 1 message per minute.
120
126
121
-
<sup>2</sup> The example uses 1 second because each Durable Object is active for 1 second per minute. This can also be thought of as 432 million requests that each take 10 ms to execute (4,320,000 seconds).
127
+
<sup>2</sup> The example uses 1 second because each Durable Object is active for
128
+
1 second per minute. This can also be thought of as 432 million requests that
129
+
each take 10 ms to execute (4,320,000 seconds).
122
130
123
131
## Frequently Asked Questions
124
132
@@ -136,17 +144,15 @@ The metadata remains in the Durable Object until you call [`deleteAll()`](/durab
136
144
137
145
A Durable Object incurs duration charges as long as the JavaScript object is held in memory. Once an object has been evicted from memory, the next time it is needed, it will be recreated (calling the constructor again). There are two factors which decide when an object may be evicted from memory: hibernatability and existence of clients.
138
146
139
-
A Durable Object is considered hibernatable any time that it is not waiting for any I/O or callbacks that depend on the in-memory state.
147
+
A Durable Object is considered hibernatable any time that it is not waiting for any I/O or callbacks that depend on the in-memory state.
140
148
141
-
- For example, if an object calls `fetch()` and awaits the response, it is considered to be waiting for I/O, and so cannot hibernate.
149
+
- For example, if an object calls `fetch()` and awaits the response, it is considered to be waiting for I/O, and so cannot hibernate.
142
150
143
-
- Less obvious to a user, if an object calls `setTimeout()` to schedule a callback in the future - no matter how far in the future - then it is considered non-hibernatable, since there would be no way to recreate the callback after hibernating.
151
+
- Less obvious to a user, if an object calls `setTimeout()` to schedule a callback in the future - no matter how far in the future - then it is considered non-hibernatable, since there would be no way to recreate the callback after hibernating.
144
152
145
153
- Additionally, if the Durable Object has received an incoming request and has not fully responded yet, then it cannot be hibernated, because hibernating would mean losing track of the async function which is eventually supposed to return a response to that request.
146
154
147
-
- As an exception, a WebSocket request which has explicitly been accepted using the [WebSocket hibernation API](/durable-objects/best-practices/websockets/#websocket-hibernation-api allows a Durable Object to hibernate even while the WebSocket is still connected.
148
-
149
-
155
+
- As an exception, a WebSocket request which has explicitly been accepted using the [WebSocket hibernation API](/durable-objects/best-practices/websockets/#websocket-hibernation-api) allows a Durable Object to hibernate even while the WebSocket is still connected.
150
156
151
157
Once a Durable Object has been in a hibernatable state for 10 consecutive seconds, it hibernates, and duration billing stops.
0 commit comments