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
Copy file name to clipboardExpand all lines: src/content/changelog/d1/2025-04-04-d1-read-replication-beta.mdx
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,25 +8,25 @@ date: 2025-04-04
8
8
9
9
D1 read replication is available in public beta to help lower average latency and increase overall throughput for read-heavy applications like e-commerce websites or content management tools.
10
10
11
-
Workers can leverage read-only database copies, called read replicase, by using D1 [Sessions API](/d1/best-practices/read-replication). A Session encapsulates all the queries from one logical session for your application. For example, a Session may correspond to all queries coming from a particular web browser session. With Sessions API, D1 queries in a Session are guanranteed to be [sequentially consistent](/d1/best-practices/read-replication/#replica-lag-and-consistency-model) to avoid data consistency pitfalls. D1 [bookmarks](/d1/reference/time-travel/#bookmarks) can be used from a previous Session to ensure logical consistency between Sessions.
11
+
Workers can leverage read-only database copies, called read replicas, by using D1 [Sessions API](/d1/best-practices/read-replication). A session encapsulates all the queries from one logical session for your application. For example, a session may correspond to all queries coming from a particular web browser session. With Sessions API, D1 queries in a session are guaranteed to be [sequentially consistent](/d1/best-practices/read-replication/#replica-lag-and-consistency-model) to avoid data consistency pitfalls. D1 [bookmarks](/d1/reference/time-travel/#bookmarks) can be used from a previous session to ensure logical consistency between sessions.
12
12
13
13
```ts
14
-
// retrieve bookmark from previous Session stored in HTTP header
14
+
// retrieve bookmark from previous session stored in HTTP header
Read replicas are automatically created by Cloudlfare (currently one in each supported [D1 region](/d1/best-practices/read-replication/#read-replica-locations)), are active/inactive based on query traffic, and are transparently routed to by Cloudflare at no additional cost. To checkout D1 read replication:
25
+
Read replicas are automatically created by Cloudflare (currently one in each supported [D1 region](/d1/best-practices/read-replication/#read-replica-locations)), are active/inactive based on query traffic, and are transparently routed to by Cloudflare at no additional cost. To checkout D1 read replication:
26
26
27
27
Deploy the following Worker code using Sessions API, which will prompt you to create a D1 database and enable read replication on said database.
28
28
29
29
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/staging/d1-starter-sessions-api)
30
30
31
-
To read more about how read replication was implemented, read our [blog post](https://blog.cloudflare.com/d1-read-replication-beta)
31
+
To learn more about how read replication was implemented, go to our [blog post](https://blog.cloudflare.com/d1-read-replication-beta).
D1 read replication can lower latency for read queries and scale read throughput by adding read-only database copies, called read replicas, across regions globally closer to clients.
12
12
13
-
Your application can use read replicas with D1 [Sessions API](/d1/worker-api/d1-database/#withsession). A Session encapsulates all the queries from one logical session for your application. For example, a Session may correspond to all queries coming from a particular web browser session. All queries within a Session read from a database instance which is as up-to-date as your query needs it to be. Sessions API ensures [sequential consistency](/d1/best-practices/read-replication/#replica-lag-and-consistency-model) for all queries in a Session.
13
+
Your application can use read replicas with D1 [Sessions API](/d1/worker-api/d1-database/#withsession). A session encapsulates all the queries from one logical session for your application. For example, a session may correspond to all queries coming from a particular web browser session. All queries within a session read from a database instance which is as up-to-date as your query needs it to be. Sessions API ensures [sequential consistency](/d1/best-practices/read-replication/#replica-lag-and-consistency-model) for all queries in a session.
// In order for the application to be correct, this SELECT
85
85
// statement must see the results of the INSERT statement above.
86
86
const resp =awaitsession
@@ -183,20 +183,20 @@ A system with multiple read replicas located around the world improves the perfo
183
183
184
184
## Use Sessions API
185
185
186
-
By using [Sessions API](/d1/worker-api/d1-database/#withsession) for read replication, all of your queries from a single <GlossaryTooltipterm="session">Session</GlossaryTooltip> read from a version of the database which ensures sequential consistency. This ensures that the version of the database you are reading is logically consistent even if the queries are handled by different read replicas.
186
+
By using [Sessions API](/d1/worker-api/d1-database/#withsession) for read replication, all of your queries from a single <GlossaryTooltipterm="session">session</GlossaryTooltip> read from a version of the database which ensures sequential consistency. This ensures that the version of the database you are reading is logically consistent even if the queries are handled by different read replicas.
187
187
188
-
D1 read replication achieves this by attaching a <GlossaryTooltipterm="bookmark">bookmark</GlossaryTooltip> to each query within a Session. For more information, refer to [Bookmarks](/d1/reference/time-travel/#bookmarks).
188
+
D1 read replication achieves this by attaching a <GlossaryTooltipterm="bookmark">bookmark</GlossaryTooltip> to each query within a session. For more information, refer to [Bookmarks](/d1/reference/time-travel/#bookmarks).
189
189
190
-
### Enable read replicaton
190
+
### Enable read replication
191
191
192
192
Read replication can be enabled at the database level in the Cloudflare dashboard. Check **\<D1 Database\>** > **Settings** to view if read replication is enabled.
193
193
194
194
1. Go to [**Workers & Pages** > **D1**](https://dash.cloudflare.com/?to=/:account/workers/d1).
To create a Session from any available database version, use `withSession()` without any parameters, which will route the first query to any database instance, either the primary database instance or a read replica.
199
+
To create a session from any available database version, use `withSession()` without any parameters, which will route the first query to any database instance, either the primary database instance or a read replica.
@@ -207,16 +207,16 @@ const result = await session
207
207
```
208
208
209
209
-`withSession()` is the same as `withSession("first-unconstrained")`
210
-
- This approach is best when your application does not require the latest database version. All queries in a Session ensure sequential consistency.
210
+
- This approach is best when your application does not require the latest database version. All queries in a session ensure sequential consistency.
211
211
- Refer to the [D1 Workers Binding API documentation](/d1/worker-api/d1-database#withsession).
212
212
213
-
{/* #### Example of a D1 Session without constraints
213
+
{/* #### Example of a D1 session without constraints
214
214
215
215
Suppose you want to develop a feature for displaying “likes” on a social network application.
216
216
217
-
The number of likes is a good example of a situation which does not require the latest information all the time. When displaying the number of likes of a post, the first request starts a new D1 Session using the constraint `first-unconstrained`, which will be served by the nearest D1 read replica.
217
+
The number of likes is a good example of a situation which does not require the latest information all the time. When displaying the number of likes of a post, the first request starts a new D1 session using the constraint `first-unconstrained`, which will be served by the nearest D1 read replica.
218
218
219
-
Subsequent interactions on the application should continue using the same Session by passing the `bookmark` from the first query to subsequent requests. This guarantees that all interactions will observe information at least as up-to-date as the initial request, and therefore never show information older than what a user has already observed. The number of likes will be updated with newer counts over time with subsequent requests as D1 asynchronously updates the read replicas with the changes from the primary database.
219
+
Subsequent interactions on the application should continue using the same session by passing the `bookmark` from the first query to subsequent requests. This guarantees that all interactions will observe information at least as up-to-date as the initial request, and therefore never show information older than what a user has already observed. The number of likes will be updated with newer counts over time with subsequent requests as D1 asynchronously updates the read replicas with the changes from the primary database.
To create a Session from the latest database version, use `withSession("first-primary")`, which will route the first query to the primary database instance.
236
+
To create a session from the latest database version, use `withSession("first-primary")`, which will route the first query to the primary database instance.
@@ -243,16 +243,16 @@ const result = await session
243
243
.run()
244
244
```
245
245
246
-
- This approach is best when your application requires the latest database version. All queries in a Session ensure sequential consistency.
246
+
- This approach is best when your application requires the latest database version. All queries in a session ensure sequential consistency.
247
247
- Refer to the [D1 Workers Binding API documentation](/d1/worker-api/d1-database#withsession).
248
248
249
249
{/* #### Example of using `first-primary`
250
250
251
251
Suppose you want to develop a webpage for an electricity provider which lists the electricity bill statements. An assumption here is that each statement is immutable. Once issued, it never changes.
252
252
253
-
In this scenario, you want the first request of the page to show a list of all the statements and their issue dates. Therefore, the first request starts a new D1 Session using the constraint `first-primary` to get the latest information (ensuring that the list includes all issued bill statements) from the primary database instance.
253
+
In this scenario, you want the first request of the page to show a list of all the statements and their issue dates. Therefore, the first request starts a new D1 session using the constraint `first-primary` to get the latest information (ensuring that the list includes all issued bill statements) from the primary database instance.
254
254
255
-
Then, when opening an individual electricity bill statement, we can continue using the same Session by passing the `bookmark` from the first query to subsequent requests. Since each bill statement is immutable, any bill statement listed from the first query is guaranteed to be available in subsequent requests using the same Session.
255
+
Then, when opening an individual electricity bill statement, we can continue using the same session by passing the `bookmark` from the first query to subsequent requests. Since each bill statement is immutable, any bill statement listed from the first query is guaranteed to be available in subsequent requests using the same session.
256
256
257
257
```ts
258
258
async function listBillStatements(accountId: string, db: D1Database): Promise<ListBillStatementsResult> {
### Start a Session from previous context (bookmark)
278
+
### Start a session from previous context (bookmark)
279
279
280
-
To create a new Session from the context of a previous Session, pass a `bookmark` parameter to guarantee that the Session starts with a database version that is at least as up-to-date as the provided `bookmark`.
280
+
To create a new session from the context of a previous session, pass a `bookmark` parameter to guarantee that the session starts with a database version that is at least as up-to-date as the provided `bookmark`.
281
281
282
282
```ts
283
-
// retrieve bookmark from previous Session stored in HTTP header
283
+
// retrieve bookmark from previous session stored in HTTP header
- Starting a Session with a `bookmark` ensures the new Session will be at least as up-to-date as the previous Session that generated the given `bookmark`.
294
+
- Starting a session with a `bookmark` ensures the new session will be at least as up-to-date as the previous session that generated the given `bookmark`.
295
295
- Refer to the [D1 Workers Binding API documentation](/d1/worker-api/d1-database#withsession).
296
296
297
297
{/* #### Example of using `bookmark`
@@ -443,7 +443,7 @@ With the REST API, set `read_replication.mode: disabled` to disable read replica
443
443
For this REST endpoint, you need to have an API token with `D1:Edit` permission. If you do not have an API token, follow the guide: [Create API token](/fundamentals/api/get-started/create-token/).
444
444
445
445
:::note
446
-
Disabling read replication takes up to 24 hours for replicas to stop processing requests. Sessions API works with databases that do not have read replication enabled, so it’s safe to run code with Sessions API even after you disable replicas.
446
+
Disabling read replication takes up to 24 hours until replicas to stop processing requests. Sessions API works with databases that do not have read replication enabled, so it is safe to run code with Sessions API even after disabling read replication.
447
447
:::
448
448
449
449
<Tabs>
@@ -583,7 +583,7 @@ Sequential consistency has properties such as:
583
583
584
584
You may wish to refer to the following resources:
585
585
586
-
- Blog: [Beta announcement & technical deep dive](https://blog.cloudflare.com/d1-read-replication-beta/)
586
+
- Blog: [Sequential consistency without borders: How D1 implements global read replication](https://blog.cloudflare.com/d1-read-replication-beta/)
587
587
- Blog: [Building D1: a Global Database](https://blog.cloudflare.com/building-d1-a-global-database/)
588
588
-[D1 Sessions API documentation](/d1/worker-api/d1-database#withsession)
589
589
-[D1 Sessions API starter demo](https://github.com/cloudflare/templates/tree/staging/d1-starter-sessions-api)
Copy file name to clipboardExpand all lines: src/content/glossary/d1.yaml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ entries:
15
15
a component in a database management system which takes a user query and generates the most efficient plan of executing that query (the query plan). For example, the query planner decides which indices to use, or which table to access first.
16
16
- term: session
17
17
general_definition: |-
18
-
a Session encapsulates all the queries from one logical session for your application. For example, a Session may correspond to all queries coming from a particular web browser session.
18
+
a session encapsulates all the queries from one logical session for your application. For example, a session may correspond to all queries coming from a particular web browser session.
19
19
- term: bookmark
20
20
general_definition: |-
21
21
a bookmark represents the state of a database at a specific point in time.
0 commit comments