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

File tree

24 files changed

+1458
-49
lines changed

24 files changed

+1458
-49
lines changed
175 KB
Loading
149 KB
Loading
164 KB
Loading

src/content/apps/index.yaml

Lines changed: 15 additions & 0 deletions
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
Lines changed: 34 additions & 0 deletions
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+

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

Lines changed: 468 additions & 0 deletions
Large diffs are not rendered by default.

src/content/docs/d1/configuration/data-location.mdx

Lines changed: 11 additions & 5 deletions
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.

src/content/docs/d1/configuration/index.mdx

Lines changed: 1 addition & 1 deletion
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
---

src/content/docs/d1/d1-api.mdx

Lines changed: 1 addition & 1 deletion
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
---

src/content/docs/d1/demos.mdx

Lines changed: 13 additions & 1 deletion
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.

0 commit comments

Comments
 (0)