Skip to content
Merged
Show file tree
Hide file tree
Changes from 47 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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/content/apps/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,18 @@
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
- link: https://github.com/cloudflare/templates/tree/main/d1-starter-sessions-api-template
id: Starter code for D1 Sessions API
description: An introduction to D1 Sessions API. This demo simulates purchase orders administration.
products: [Workers, D1]
languages: [TypeScript]
cloudflare: true
updated: 2025-03-31
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: D1 Read Replication Public Beta
description: Use D1 Sessions API to leverage read replication.
products:
- d1
- workers
date: 2025-04-10T06:00:00Z
hidden: true
---

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.

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.

```ts
// retrieve bookmark from previous session stored in HTTP header
const bookmark = request.headers.get('x-d1-bookmark') ?? 'first-unconstrained';

const session = env.DB.withSession(bookmark)
const result = await session
.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
.run()
// store bookmark for a future session
response.headers.set('x-d1-bookmark', session.getBookmark() ?? "")
```

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, deploy the following Worker code using Sessions API, which will prompt you to create a D1 database and enable read replication on said database.

[![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)

To learn more about how read replication was implemented, go to our [blog post](https://blog.cloudflare.com/d1-read-replication-beta).

468 changes: 468 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
---
14 changes: 13 additions & 1 deletion src/content/docs/d1/demos.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@
pcx_content_type: navigation
title: Demos and architectures
sidebar:
order: 12
order: 13

---

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

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

## Featured Demos

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

[![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)

:::note[Tip: Place your database further away for the read replication demo]
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).

You can find this in the **Database location hint** dropdown.
:::

## Demos

Explore the following <GlossaryTooltip term="demo application">demo applications</GlossaryTooltip> for D1.
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/platform/limits.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Render } from "~/components";
| Maximum storage per account | 250 GB (Workers Paid)[^1] / 5 GB (Free) |
| [Time Travel](/d1/reference/time-travel/) duration (point-in-time recovery) | 30 days (Workers Paid) / 7 days (Free) |
| Maximum Time Travel restore operations | 10 restores per 10 minute (per database) |
| Queries per Worker invocation (read [subrequest limits](/workers/platform/limits/#how-many-subrequests-can-i-make)) | 50 (Bundled) / 1000 (Unbound) |
| Queries per Worker invocation (read [subrequest limits](/workers/platform/limits/#how-many-subrequests-can-i-make)) | 50 (Free) / 1000 (Paid) |
| Maximum number of columns per table | 100 |
| Maximum number of rows per table | Unlimited (excluding per-database storage limits) |
| Maximum string, `BLOB` or table row size | 2,000,000 bytes (2 MB) |
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
6 changes: 6 additions & 0 deletions src/content/docs/d1/reference/migrations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Currently, the migrations system aims to be simple yet effective. With the curre

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.

:::note[Binding name vs Database name]
When running a migration script, you can use either the binding name or the database name.

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

## Wrangler customizations

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