Skip to content

Commit 205760f

Browse files
Oxyjunvy-tonlambrospetrouharshil1712
authored
[D1] D1 RR Documentation (#20922)
D1 read replication docs --------- Co-authored-by: Vy Ton <[email protected]> Co-authored-by: Lambros Petrou <[email protected]> Co-authored-by: Harshil Agrawal <[email protected]>
1 parent 32f2c62 commit 205760f

24 files changed

+1458
-49
lines changed

Diff for: public/images/d1/consistency-with-sessions-api.png

175 KB
Loading
149 KB
Loading

Diff for: public/images/d1/d1-read-replication-concept.png

164 KB
Loading

Diff for: src/content/apps/index.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,18 @@
340340
languages: [TypeScript]
341341
cloudflare: false
342342
updated: 2024-10-07
343+
- link: https://github.com/harshil1712/e-com-d1
344+
id: E-commerce Store
345+
description: An application to showcase D1 read replication in the context of an online store.
346+
tags: []
347+
products: [Workers, D1]
348+
languages: [TypeScript]
349+
cloudflare: true
350+
updated: 2025-02-27
351+
- link: https://github.com/cloudflare/templates/tree/main/d1-starter-sessions-api-template
352+
id: Starter code for D1 Sessions API
353+
description: An introduction to D1 Sessions API. This demo simulates purchase orders administration.
354+
products: [Workers, D1]
355+
languages: [TypeScript]
356+
cloudflare: true
357+
updated: 2025-03-31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: D1 Read Replication Public Beta
3+
description: Use D1 Sessions API to leverage read replication.
4+
products:
5+
- d1
6+
- workers
7+
date: 2025-04-10T06:00:00Z
8+
hidden: true
9+
---
10+
11+
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.
12+
13+
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.
14+
15+
```ts
16+
// retrieve bookmark from previous session stored in HTTP header
17+
const bookmark = request.headers.get('x-d1-bookmark') ?? 'first-unconstrained';
18+
19+
const session = env.DB.withSession(bookmark)
20+
const result = await session
21+
.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
22+
.run()
23+
// store bookmark for a future session
24+
response.headers.set('x-d1-bookmark', session.getBookmark() ?? "")
25+
```
26+
27+
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.
28+
29+
To checkout D1 read replication, deploy the following Worker code using Sessions API, which will prompt you to create a D1 database and enable read replication on said database.
30+
31+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/staging/d1-starter-sessions-api)
32+
33+
To learn more about how read replication was implemented, go to our [blog post](https://blog.cloudflare.com/d1-read-replication-beta).
34+

Diff for: src/content/docs/d1/best-practices/read-replication.mdx

+468
Large diffs are not rendered by default.

Diff for: src/content/docs/d1/configuration/data-location.mdx

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Learn how the location of data stored in D1 is determined, including where the l
99

1010
## Automatic (recommended)
1111

12-
By default, D1 will automatically create your database in a location close to where you issued the request to create a database. In most cases this allows D1 to choose the optimal location for your database on your behalf.
12+
By default, D1 will automatically create your primary database instance in a location close to where you issued the request to create a database. In most cases this allows D1 to choose the optimal location for your database on your behalf.
1313

1414
## Provide a location hint
1515

16-
Location hint is an optional parameter you can provide to indicate your desired geographical location for your database.
16+
Location hint is an optional parameter you can provide to indicate your desired geographical location for your primary database instance.
1717

18-
You may want to explicitly provide a location hint in cases where the majority of your writes to a specific database come from a different location than where you are creating the database from. location hints can be useful when:
18+
You may want to explicitly provide a location hint in cases where the majority of your writes to a specific database come from a different location than where you are creating the database from. Location hints can be useful when:
1919

2020
- Working in a distributed team.
2121
- Creating databases specific to users in specific locations.
@@ -33,9 +33,7 @@ Providing a location hint does not guarantee that D1 runs in your preferred loca
3333
### Use Wrangler
3434

3535
:::note
36-
3736
To install Wrangler, the command-line interface for D1 and Workers, refer to [Install and Update Wrangler](/workers/wrangler/install-and-update/).
38-
3937
:::
4038

4139
To provide a location hint when creating a new database, pass the `--location` flag with a valid location hint:
@@ -70,3 +68,11 @@ D1 supports the following location hints:
7068
:::caution
7169
D1 location hints are not currently supported for South America (`sam`), Africa (`afr`), and the Middle East (`me`). D1 databases do not run in these locations.
7270
:::
71+
72+
## Read replica locations
73+
74+
With read replication enabled, D1 creates and distributes read-only copies of the primary database instance around the world. This reduces the query latency for users located far away from the primary database instance.
75+
76+
When using D1 read replication, D1 automatically creates a read replica in [every available region](/d1/configuration/data-location#available-location-hints), including the region where the primary database instance is located.
77+
78+
Refer to [D1 read replication](/d1/best-practices/read-replication/) for more information.

Diff for: src/content/docs/d1/configuration/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Configuration
33
pcx_content_type: navigation
44
sidebar:
5-
order: 8
5+
order: 9
66
group:
77
hideIndex: true
88
---

Diff for: src/content/docs/d1/d1-api.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ pcx_content_type: navigation
33
title: REST API
44
external_link: /api/resources/d1/subresources/database/methods/create/
55
sidebar:
6-
order: 6
6+
order: 7
77
---

Diff for: src/content/docs/d1/demos.mdx

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22
pcx_content_type: navigation
33
title: Demos and architectures
44
sidebar:
5-
order: 12
5+
order: 13
66

77
---
88

99
import { ExternalResources, GlossaryTooltip, ResourcesBySelector } from "~/components"
1010

1111
Learn how you can use D1 within your existing application and architecture.
1212

13+
## Featured Demos
14+
15+
- [Starter code for D1 Sessions API](https://github.com/cloudflare/templates/tree/main/d1-starter-sessions-api-template): An introduction to D1 Sessions API. This demo simulates purchase orders administration.
16+
17+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/main/d1-starter-sessions-api-template)
18+
19+
:::note[Tip: Place your database further away for the read replication demo]
20+
To simulate how read replication can improve a worst case latency scenario, select your primary database location to be in a farther away region (one of the deployment steps).
21+
22+
You can find this in the **Database location hint** dropdown.
23+
:::
24+
1325
## Demos
1426

1527
Explore the following <GlossaryTooltip term="demo application">demo applications</GlossaryTooltip> for D1.

Diff for: src/content/docs/d1/examples/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ hideChildren: false
44
pcx_content_type: navigation
55
title: Examples
66
sidebar:
7-
order: 10
7+
order: 11
88
group:
99
hideIndex: true
1010
---

Diff for: src/content/docs/d1/get-started.mdx

+1-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@ sidebar:
66
order: 2
77
---
88

9-
import {
10-
Render,
11-
PackageManagers,
12-
Steps,
13-
FileTree,
14-
Tabs,
15-
TabItem,
16-
TypeScriptExample,
17-
WranglerConfig
18-
} from "~/components";
9+
import { Render, PackageManagers, Steps, FileTree, Tabs, TabItem, TypeScriptExample, WranglerConfig } from "~/components";
1910

2011
This guide instructs you through:
2112

Diff for: src/content/docs/d1/observability/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Observability
33
pcx_content_type: navigation
44
sidebar:
5-
order: 9
5+
order: 10
66
group:
77
hideIndex: true
88
---

Diff for: src/content/docs/d1/platform/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: navigation
33
title: Platform
44
sidebar:
5-
order: 12
5+
order: 13
66
group:
77
hideIndex: true
88
---

Diff for: src/content/docs/d1/platform/limits.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Render } from "~/components";
1515
| Maximum storage per account | 250 GB (Workers Paid)[^1] / 5 GB (Free) |
1616
| [Time Travel](/d1/reference/time-travel/) duration (point-in-time recovery) | 30 days (Workers Paid) / 7 days (Free) |
1717
| Maximum Time Travel restore operations | 10 restores per 10 minute (per database) |
18-
| Queries per Worker invocation (read [subrequest limits](/workers/platform/limits/#how-many-subrequests-can-i-make)) | 50 (Bundled) / 1000 (Unbound) |
18+
| Queries per Worker invocation (read [subrequest limits](/workers/platform/limits/#how-many-subrequests-can-i-make)) | 50 (Free) / 1000 (Paid) |
1919
| Maximum number of columns per table | 100 |
2020
| Maximum number of rows per table | Unlimited (excluding per-database storage limits) |
2121
| Maximum string, `BLOB` or table row size | 2,000,000 bytes (2 MB) |

Diff for: src/content/docs/d1/reference/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: navigation
33
title: Reference
44
sidebar:
5-
order: 13
5+
order: 14
66
group:
77
hideIndex: true
88
---

Diff for: src/content/docs/d1/reference/migrations.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Currently, the migrations system aims to be simple yet effective. With the curre
2020

2121
Every migration file in the `migrations` folder has a specified version number in the filename. Files are listed in sequential order. Every migration file is an SQL file where you can specify queries to be run.
2222

23+
:::note[Binding name vs Database name]
24+
When running a migration script, you can use either the binding name or the database name.
25+
26+
However, the binding name can change, whereas the database name cannot. Therefore, to avoid accidentally running migrations on the wrong binding, you may wish to use the database name for D1 migrations.
27+
:::
28+
2329
## Wrangler customizations
2430

2531
By default, migrations are created in the `migrations/` folder in your Worker project directory. Creating migrations will keep a record of applied migrations in the `d1_migrations` table found in your database.

Diff for: src/content/docs/d1/reference/time-travel.mdx

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar:
55
order: 2
66
---
77

8+
import { GlossaryTooltip} from "~/components"
9+
810
Time Travel is D1's approach to backups and point-in-time-recovery, and allows you to restore a database to any minute within the last 30 days.
911

1012
- You do not need to enable Time Travel. It is always on.
@@ -23,13 +25,14 @@ To understand which storage subsystem your database uses, run `wrangler d1 info
2325

2426
## Bookmarks
2527

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.
28+
Time Travel leverages D1's concept of a <GlossaryTooltip term="bookmark"> bookmark </GlossaryTooltip> to restore to a point in time.
2729

28-
- Bookmarks are lexicographically sortable. Sorting orders a list of bookmarks from oldest-to-newest.
2930
- Bookmarks older than 30 days are invalid and cannot be used as a restore point.
3031
- 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.
32+
- Bookmarks are lexicographically sortable. Sorting orders a list of bookmarks from oldest-to-newest.
33+
- 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).
3134

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).
35+
Bookmarks are also leveraged by [Sessions API](/d1/best-practices/read-replication/#sessions-api-examples) to ensure sequential consistency within a Session.
3336

3437
## Timestamps
3538

Diff for: src/content/docs/d1/sql-api/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: SQL API
33
pcx_content_type: navigation
44
sidebar:
5-
order: 5
5+
order: 6
66
group:
77
hideIndex: true
88
---

Diff for: src/content/docs/d1/tutorials/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pcx_content_type: navigation
44
title: Tutorials
55
hideChildren: true
66
sidebar:
7-
order: 11
7+
order: 12
88

99
---
1010

0 commit comments

Comments
 (0)