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
D1 read replication scales read throughput and lowers latency for read queries by adding almost up-to-date, read-only database copies (read replicas) across global regions closer to clients.
11
+
D1 read replication can lower latency for read queries and scale read throughput by adding read-only database copies, called read replicas, across global regions closer to clients.
12
12
13
-
You can use D1 read replication by using D1 Sessions API. 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.
14
-
15
-
By using Sessions API for read replication, all of your queries from a single Session read from a database instance which is as up-to-date as your query needs it to be. This ensures that the version of the database you are reading from is logically consistent with your queries when using read replicas.
13
+
Your application uses 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.
16
14
17
15
To use read replication with the following Worker code and Sessions API:
When using D1 without read replication, D1 routes all queries (both read and write) to a specific database instance in [one location in the world](/d1/configuration/data-location/), known as the primary database instance. D1 request latency is dependent on the physical closeness of a user to the primary database instance. Users located further away from the primary database instance experience longer request latency due to [network round-trip time](https://www.cloudflare.com/learning/cdn/glossary/round-trip-time-rtt/).
166
+
When using D1 without read replication, D1 routes all queries (both read and write) to a specific database instance in [one location in the world](/d1/configuration/data-location/), known as the <GlossaryTooltipterm="primary database instance"> primary database instance </GlossaryTooltip>. D1 request latency is dependent on the physical closeness of a user to the primary database instance. Users located further away from the primary database instance experience longer request latency due to [network round-trip time](https://www.cloudflare.com/learning/cdn/glossary/round-trip-time-rtt/).
162
167
163
-
When using read replication, D1 introduces multiple “almost up-to-date” copies of the primary database instance which only serve read requests, called <GlossaryTooltipterm="read replica"> read replicas </GlossaryTooltip>. D1 creates the read replicas in multiple regions throughout the world [across the Cloudflare network](/d1/best-practices/read-replication/#read-replica-locations).
168
+
When using read replication, D1 introduces multiple, asynchronously replicated copies of the primary database instance which only serve read requests, called <GlossaryTooltipterm="read replica"> read replicas </GlossaryTooltip>. D1 creates the read replicas in multiple regions throughout the world [across the Cloudflare network](/d1/best-practices/read-replication/#read-replica-locations).
164
169
165
170
A user may be located far away from the primary database instance but close to a read replica. When D1 routes read requests to the read replica instead of the primary database instance, the user enjoys shorter read request response times.
166
171
167
-
D1 asynchronously replicates changes from the primary database instance to all read replicas. This means that at any given time, a read replica may be arbitrarily out of date. The time it takes for the latest committed data in the primary database instance to be replicated to the read replica is known as the <GlossaryTooltipterm="replica lag"> replica lag </GlossaryTooltip>.
168
-
169
-
It is replica lag and non-deterministic routing to individual replicas that can lead to application data consistency issues, which Sessions API help handle by ensuring sequential consistency. For more information refer to [Replica lag and consistency model](/d1/best-practices/read-replication/#replica-lag-and-consistency-model).
172
+
D1 asynchronously replicates changes from the primary database instance to all read replicas. This means that at any given time, a read replica may be arbitrarily out of date. The time it takes for the latest committed data in the primary database instance to be replicated to the read replica is known as the <GlossaryTooltipterm="replica lag"> replica lag </GlossaryTooltip>. It is replica lag and non-deterministic routing to individual replicas that can lead to application data consistency issues, which Sessions API helps handle by ensuring sequential consistency. For more information, refer to [replica lag and consistency model](/d1/best-practices/read-replication/#replica-lag-and-consistency-model).
170
173
171
174
:::note
172
175
All write queries are still forwarded to the primary database instance. Read replication only improves the query response time for read requests.
@@ -185,17 +188,17 @@ D1 read replication achieves this by attaching a <GlossaryTooltip term="bookmark
185
188
186
189
### Enable read replication
187
190
188
-
Use REST APIto enable read replication on your D1 database.
191
+
With REST API, set `"read_replication": "mode":"auto"`to enable read replication on a D1 database.
189
192
190
-
Set `"mode":"auto"` to enable read replication.
193
+
For REST API, use a [API token](/fundamentals/api/get-started/create-token/) with `D1:Edit`[permission](/fundamentals/api/reference/permissions/#account-permissions).
191
194
192
195
<Tabs>
193
196
<TabItemlabel="cURL">
194
197
```curl
195
198
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/d1/database/{database_id}" \
Use REST APIto disable read replication on your D1 database. If this is your first time using REST API, create an API token. Refer to [Create API token](/fundamentals/api/get-started/create-token/).
223
+
With REST API, set `"read_replication": "mode":"disable"`to disable read replication on a D1 database.
221
224
222
-
Set `"mode":"disable"` to disable read replication.
225
+
For REST API, use a [API token](/fundamentals/api/get-started/create-token/) with `D1:Edit`[permission](/fundamentals/api/reference/permissions/#account-permissions).
223
226
224
227
<Tabs>
225
228
<TabItemlabel="cURL">
226
229
```curl
227
230
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/d1/database/{database_id}" \
The following code uses REST API to check if read replication is enabled / disabled.
255
+
`GET` D1 database REST endpoint returns if read replication is enabled or disabled.
256
+
257
+
For REST API, use a [API token](/fundamentals/api/get-started/create-token/) with `D1:Read`[permission](/fundamentals/api/reference/permissions/#account-permissions).
- If the output is `auto`, read replication is enabled.
279
-
- If the output is `disabled`, read replication is disabled.
283
+
- Check the `read_replication` property of the `result` object
284
+
-`"mode": "auto"` indicates read replication is enabled
285
+
-`"mode": "disable"` indicates read replication is disabled
280
286
281
287
### Start a Session without constraints
282
288
283
289
To create a Session from any 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.
284
290
285
291
```ts
286
-
// synchronous
287
-
let session =env.DB_D1.withSession()
288
-
const stmt =session
289
-
.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
290
-
// wait for Session condition, queries within batch have casual consistency
### Start a Session from previous context (bookmark)
365
368
366
-
To create a new Session from the context of a previous Session, pass a `bookmark` parameter to guarantee that the Session starts with database state as of or later than the provided `bookmark`.
369
+
To create a new Session from the context of a previous Session, pass a `bookmark` parameter to guarantee that the Session starts with database state as of or later than the provided `bookmark`.`bookmark` could be saved in HTTP header from previous Session.
367
370
368
371
```ts
369
-
// synchronous
370
-
let session =env.DB_D1.withSession('bookmark_string')
371
-
const stmt =session
372
-
.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
373
-
// wait for Session condition, queries within batch have casual consistency
- Starting a Session with a `bookmark` ensures the new Session has all the same context as the previous Session.
@@ -470,14 +475,14 @@ export default {
470
475
} satisfies ExportedHandler<Env>;
471
476
``` */}
472
477
473
-
### Inspect `meta` fields
478
+
### Check where D1 request was processed
474
479
475
-
To see how D1 requests are processed by the additional database instances, return `served_by_region` and `served_by_primary` fields from the `meta` object. You can also manually calculate the query latency using `Date.now()` before and after your query.
480
+
To see how D1 requests are processed by the addition of read replicas, `served_by_region` and `served_by_primary` fields are returned by the `meta` object in a [D1 Result](/d1/worker-api/return-object/#d1result).
476
481
477
482
```ts
478
-
function buildResponse(session:D1DatabaseSession, res:D1Result, tsStart:number) {
483
+
function buildResponse(session:D1DatabaseSession, res:D1Result, startTimestamp:number) {
-`served_by_region` and `served_by_primary` fields are present for all D1 remote requests, regardless of whether read replication is enabled. On local development, `npx wrangler dev`, these fields are `null`.
498
+
- You can also manually calculate the query latency using `Date.now()` before and after your query.
499
+
492
500
## Read replica locations
493
501
494
502
Currently, D1 automatically creates a read replica in [every supported region](/d1/configuration/data-location/#available-location-hints), including the region where the primary database instance is located. These regions are:
@@ -506,12 +514,12 @@ Read replica locations are subject to change at Cloudflare's discretion.
506
514
507
515
## Observability
508
516
509
-
To see the impact of read replication, check the how D1 requests are processed by additional database instances. You can use:
517
+
To see the impact of read replication and check the how D1 requests are processed by additional database instances, you can use:
510
518
511
519
- The `meta` object within the [`D1Result`](/d1/worker-api/return-object/#d1result) return object, which includes new fields:
512
520
-`served_By_Region`
513
521
-`served_By_Primary`
514
-
- The [Cloudflare dashboard](https://dash.cloudflare.com/), where you can view your metrics breakdown by region.
522
+
- The [Cloudflare dashboard](https://dash.cloudflare.com/), where you can view your metrics breakdown by region that processed D1 requests.
515
523
516
524
## Known limitations
517
525
@@ -546,7 +554,7 @@ A system with multiple read replicas located around the world improves the perfo
546
554
547
555
You may wish to refer to the following resources:
548
556
549
-
- Blog: TBC
557
+
- Blog: [Beta announcement & technical deep dive](https://blog.cloudflare.com/d1-read-replication-beta)
550
558
- Blog: [Building D1: a Global Database](https://blog.cloudflare.com/building-d1-a-global-database/)
551
559
-[D1 Sessions API documentation](/d1/worker-api/d1-database#withsession)
Copy file name to clipboardExpand all lines: src/content/docs/d1/reference/time-travel.mdx
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,13 +23,14 @@ To understand which storage subsystem your database uses, run `wrangler d1 info
23
23
24
24
## Bookmarks
25
25
26
-
Time Travel introduces the concept of a "bookmark" to D1. A bookmark represents the state of a database at a specific point in time, and is effectively an append-only log.
26
+
Time Travel leverages D1's concept of a <GlossaryTooltipterm="bookmark"> bookmark </GlossaryTooltip> to restore to a point in time.
27
27
28
-
- Bookmarks are lexicographically sortable. Sorting orders a list of bookmarks from oldest-to-newest.
29
28
- Bookmarks older than 30 days are invalid and cannot be used as a restore point.
30
29
- Restoring a database to a specific bookmark does not remove or delete older bookmarks. For example, if you restore to a bookmark representing the state of your database 10 minutes ago, and determine that you needed to restore to an earlier point in time, you can still do so.
30
+
- Bookmarks are lexicographically sortable. Sorting orders a list of bookmarks from oldest-to-newest.
31
+
- Bookmarks can be derived from a [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time) (seconds since Jan 1st, 1970), and conversion between a specific timestamp and a bookmark is deterministic (stable).
31
32
32
-
Bookmarks can be derived from a [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time) (seconds since Jan 1st, 1970), and conversion between a specific timestamp and a bookmark is deterministic (stable).
33
+
Bookmarks are also leveraged by [Sessions API](/d1/best-practices/read-replication/#sessions-api-examples) to ensure sequential consistency within a Session.
- Directs the first query in the Session (whether read or write) to any database instance. Use this option if you do not need to start the Session with the most up-to-date data, and wish to prioritize minimizing query latency from the very start of the Session.
263
263
- Subsequent queries in the Session have sequential consistency.
264
-
- This is the default behavior when no parameters are provided.
264
+
- This is the default behavior when no parameter is provided.
- A [`bookmark`](/d1/reference/time-travel/#bookmarks) from an existing D1 Session. This allows you to continue the existing Session using the `bookmark` as a reference point.
267
+
- A [`bookmark`](/d1/reference/time-travel/#bookmarks) from a previous D1 Session. This allows you to start a new Session from at least the provided `bookmark`.
268
268
- Subsequent queries in the Session have sequential consistency.
- A [`bookmark`](/d1/reference/time-travel/#bookmarks) which identifies the latest version of the database seen by all queries executed within the Session.
300
+
- A [`bookmark`](/d1/reference/time-travel/#bookmarks) which identifies the latest version of the database seen by the last query executed within the Session.
301
+
- Returns `null` if no query is executed within a Session.
Copy file name to clipboardExpand all lines: src/content/glossary/d1.yaml
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -18,4 +18,5 @@ entries:
18
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
-
a bookmark represents the state of a database at a specific point in time.
21
+
a bookmark represents the state of a database at a specific point in time.
22
+
- Bookmarks are lexicographically sortable. Sorting orders a list of bookmarks from oldest-to-newest.
0 commit comments