Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
1350499
Creating new branch D1 RR 2
Oxyjun Mar 18, 2025
a497e67
Enhancing Read Replication chapter from comments
Oxyjun Mar 19, 2025
8d13b91
Offloading some content to Workers API page.
Oxyjun Mar 20, 2025
beb3e61
Inserting example which uses cookies to retrieve
Oxyjun Mar 20, 2025
db88e2a
Updating diagram
Oxyjun Mar 20, 2025
5d1b253
Removing redundant header
Oxyjun Mar 20, 2025
8960586
Updating image again, adding starter code
Oxyjun Mar 20, 2025
95c3bfa
Merge commit 'f202b11ff9e6cb29309ec9a2a5b1c5898775fb47' into jun/d1/r…
Oxyjun Mar 20, 2025
0ed9469
Addressing feedback part 2, WIP
Oxyjun Mar 21, 2025
5bcfcd8
Fixing link
Oxyjun Mar 21, 2025
0d474cf
Fixing links
Oxyjun Mar 21, 2025
50ffa22
Merge commit 'b5dbe957cf6dde2854adf1472c54026f2379cfca' into jun/d1/r…
Oxyjun Mar 21, 2025
0e70d96
PM edits
vy-ton Mar 23, 2025
5e1b136
Fix missing component
vy-ton Mar 23, 2025
61bd1e4
Updating d1 read replication image.
Oxyjun Mar 24, 2025
5810ce4
Apply Lambros' suggestions from code review part 1
Oxyjun Mar 25, 2025
ca8210e
Update src/content/docs/d1/best-practices/read-replication.mdx
Oxyjun Mar 25, 2025
743ce11
Apply Lambros' suggestions from code review part 2
Oxyjun Mar 25, 2025
6007ebc
Update src/content/docs/d1/best-practices/read-replication.mdx
Oxyjun Mar 25, 2025
16d3fe4
Updating diagram with correct labels.
Oxyjun Mar 25, 2025
ad5d26e
Merge branch 'jun/d1/rr-v2' of github.com:cloudflare/cloudflare-docs …
Oxyjun Mar 25, 2025
0f7b956
Add deploy to Workers button, pm fixes
vy-ton Mar 26, 2025
c384a17
fix button format
vy-ton Mar 26, 2025
bed72ca
Merge commit 'd202cc3d0694b1661379623c8d09d275e2972641' into jun/d1/r…
Oxyjun Mar 26, 2025
4d697ce
Fixing curl json quote formatting.
Oxyjun Mar 26, 2025
5d5de41
Adding in consistency diagrams and explanations.
Oxyjun Mar 27, 2025
6f83aa9
Adding note on creating an API token.
Oxyjun Mar 28, 2025
060c48a
add changelog, shuffle sections
vy-ton Apr 1, 2025
a65c5d3
fix compile tag issue
vy-ton Apr 1, 2025
5caf814
fix format issues
vy-ton Apr 1, 2025
beb24fb
Sessions capitalisation edit. Adding demo to demo
Oxyjun Apr 1, 2025
2f01a11
Fixing link for e-commerce demo bullet point
Oxyjun Apr 1, 2025
31d7f4d
Minor fix
Oxyjun Apr 1, 2025
333e0b4
Merge commit '4bdf47a2b13b1e2f64fb1694a2db7bebdd809aa9' into jun/d1/r…
Oxyjun Apr 1, 2025
9ae1b41
Removing basic tutorial, adding featured demos.
Oxyjun Apr 1, 2025
ca35b8b
[D1] Add e-commerce tutorial (#21125)
harshil1712 Apr 1, 2025
88316c8
Tweaking wording in e-com tutorial. Linking as
Oxyjun Apr 1, 2025
c0003d0
Adding a note about binding name vs database name
Oxyjun Apr 2, 2025
35c522b
Removing duplicate demo, using GitHubCode.
Oxyjun Apr 3, 2025
1e9d9b7
Removing step of activating RR from tutorial, when
Oxyjun Apr 3, 2025
fc93b7e
Fixing wording, improving tutorial steps.
Oxyjun Apr 4, 2025
26138e7
small fixups
vy-ton Apr 7, 2025
c03defb
Updating link for the D2C button
Oxyjun Apr 8, 2025
b37c6e7
Updating demo links, adding note to place
Oxyjun Apr 9, 2025
ebc9978
Hiding read replication chapter.
Oxyjun Apr 9, 2025
42b78e5
pm tutorial edits
vy-ton Apr 9, 2025
530bcc4
rename, add back changelog
vy-ton Apr 9, 2025
7bdef48
modify demo tip
vy-ton Apr 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/content/apps/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,11 @@
languages: [TypeScript]
cloudflare: false
updated: 2024-10-07
- link: https://github.com/harshil1712/e-com-d1
id: E-commerce Store
description: An application to showcase D1 read replication in the context of an online store.
tags: []
products: [Workers, D1]
languages: [TypeScript]
cloudflare: true
updated: 2025-02-27
561 changes: 561 additions & 0 deletions src/content/docs/d1/best-practices/read-replication.mdx

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions src/content/docs/d1/configuration/data-location.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Learn how the location of data stored in D1 is determined, including where the l

## Automatic (recommended)

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.
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.

## Provide a location hint

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

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:
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:

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

:::note

To install Wrangler, the command-line interface for D1 and Workers, refer to [Install and Update Wrangler](/workers/wrangler/install-and-update/).

:::

To provide a location hint when creating a new database, pass the `--location` flag with a valid location hint:
Expand Down Expand Up @@ -70,3 +68,11 @@ D1 supports the following location hints:
:::caution
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.
:::

## Read replica locations

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.

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.

Refer to [D1 read replication](/d1/best-practices/read-replication/) for more information.
2 changes: 1 addition & 1 deletion src/content/docs/d1/configuration/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Configuration
pcx_content_type: navigation
sidebar:
order: 8
order: 9
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/d1-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pcx_content_type: navigation
title: REST API
external_link: /api/resources/d1/subresources/database/methods/create/
sidebar:
order: 6
order: 7
---
2 changes: 1 addition & 1 deletion src/content/docs/d1/demos.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: navigation
title: Demos and architectures
sidebar:
order: 12
order: 13

---

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/examples/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ hideChildren: false
pcx_content_type: navigation
title: Examples
sidebar:
order: 10
order: 11
group:
hideIndex: true
---
Expand Down
11 changes: 1 addition & 10 deletions src/content/docs/d1/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ sidebar:
order: 2
---

import {
Render,
PackageManagers,
Steps,
FileTree,
Tabs,
TabItem,
TypeScriptExample,
WranglerConfig
} from "~/components";
import { Render, PackageManagers, Steps, FileTree, Tabs, TabItem, TypeScriptExample, WranglerConfig } from "~/components";

This guide instructs you through:

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/observability/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Observability
pcx_content_type: navigation
sidebar:
order: 9
order: 10
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/platform/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: navigation
title: Platform
sidebar:
order: 12
order: 13
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/reference/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: navigation
title: Reference
sidebar:
order: 13
order: 14
group:
hideIndex: true
---
Expand Down
9 changes: 6 additions & 3 deletions src/content/docs/d1/reference/time-travel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sidebar:
order: 2
---

import { GlossaryTooltip} from "~/components"

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.

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

## Bookmarks

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

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

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

## Timestamps

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/sql-api/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: SQL API
pcx_content_type: navigation
sidebar:
order: 5
order: 6
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/tutorials/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pcx_content_type: navigation
title: Tutorials
hideChildren: true
sidebar:
order: 11
order: 12

---

Expand Down
165 changes: 165 additions & 0 deletions src/content/docs/d1/tutorials/test-read-replication/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
---
updated: 2025-03-04
difficulty: Beginner
content_type: Tutorial
pcx_content_type: tutorial
title: Test D1 read replication
products:
- D1
languages:
- JavaScript
- TypeScript
- SQL
---

import { Render, Steps, Tabs, TabItem, FileTree } from "~/components";

In this tutorial, you will create a basic Worker script to test the effect of [D1 read replication](/d1/best-practices/read-replication/), and see the difference in the request latency.

## Prerequisites

This tutorial assumes you have completed and understood the [D1 get started](/d1/get-started/) tutorial.

## 1. Create a Worker

Create a new Worker as the means to query your database.

```sh
npm create cloudflare@latest -- <DATABASE_NAME>
```

## 2. Create a database located far away

Create a database located far away by specifying a [location hint](/d1/configuration/data-location/) which is far away from the region you are located in.

```sh
npx wrangler d1 create <DATABASE_NAME> --location=apac
```
```sh output
✅ Successfully created DB '<DATABASE_NAME>' in region APAC
Created your new D1 database.

{
"d1_databases": [
{
"binding": "DB",
"database_name": "<DATABASE_NAME>",
"database_id": "<DATABASE_ID>"
}
]
}
```

This creates a new D1 database and outputs the [binding](/workers/runtime-apis/bindings/) configuration needed in the next step.

## 3. Bind your D1 database to your Worker

Modify your `wrangler.jsonc` file to include the output of the CLI to bind your D1 database to your Worker.

## 4. Populate the D1 database

Populate your database with the table from the [D1 get started](/d1/get-started/) tutorial.

<Steps>
1. Copy the following code and save it as a `schema.sql` file in the Worker directory you created in step 1:

```sql
DROP TABLE IF EXISTS Customers;
CREATE TABLE IF NOT EXISTS Customers (CustomerId INTEGER PRIMARY KEY, CompanyName TEXT, ContactName TEXT);
INSERT INTO Customers (CustomerID, CompanyName, ContactName) VALUES (1, 'Alfreds Futterkiste', 'Maria Anders'), (4, 'Around the Horn', 'Thomas Hardy'), (11, 'Bs Beverages', 'Victoria Ashworth'), (13, 'Bs Beverages', 'Random Name');
```

2. Initialize your database to run remotely.

```sh
npx wrangler d1 execute <DATABASE_NAME> --remote --file=./schema.sql
```
</Steps>

## 5. Write a Worker file which queries the table

Write a Worker file which queries the table and outputs both the results with the query latency.

```js
export default {
async fetch(request, env) {
const { pathname } = new URL(request.url);
const companyName1 = `Bs Beverages`;
const stmt = env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`);
const session = env.DB.withSession("first-unconstrained");

if (pathname === `/run`) {
const tsStart1 = Date.now();
const { results, meta } = await stmt.bind(companyName1).run();
const d1Duration1 = Date.now() - tsStart1;
return Response.json({ results, meta, d1Duration1 });

} else if (pathname === `/withsession`) {
const tsStart2 = Date.now();
const { results, meta } = await session.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`).bind(companyName1).run();
const d1Duration2 = Date.now() - tsStart2;
return Response.json({ results, meta, d1Duration2 });
}
return new Response(
`Welcome to the D1 read replication demo!

Add one of the following slugs below to see the effects of using D1 read replication.

\n/run - Queries the table without using read replication

\n/withsession - Queries the table using read replication (using "first-unconstrained")

\nUse the two options to compare the difference in query latency.`
);
}
};
```

## 6. Enable read replication

Use the REST API to enable read replication on your D1 database.

Run the following `curl` command on your terminal, with the details of your account ID and database ID.

```curl
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/d1/database/{database_id}" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"read_replication": {"mode": "auto"}}'
```

## 7. Deploy Worker

Deploy your Worker.

```sh
npx wrangler deploy
```

## 8. Compare query latency

Once deployed, you can compare the query latency when using read replication.

- Use the `/run` URL to send a read query without read replication.
- Use the `/withsession` URL to send a read query with read replication.

For both queries, the Worker script returns the `meta` object, which contains:

- `served_by_primary`: indicates whether the query was served by the primary database instance
- `served_by_region`: shows the location of the database instance that processed the query

The `d1Duration` variable shows the whole round-trip latency.

## Summary

By completing this tutorial, you have:

1. Created a D1 database using a location hint.
2. Created a Worker script which uses read replication.
3. Deployed the Worker to test the difference in query latency when using read replication.

## Related resources

- [D1 read replication](/d1/best-practices/read-replication)
- [D1 Sessions Workers Binding API](/d1/worker-api/d1-database#withsession)
- [D1 read replication demo application](/d1/demos/)
Loading
Loading