Skip to content

Commit 42b78e5

Browse files
committed
pm tutorial edits
1 parent ebc9978 commit 42b78e5

File tree

3 files changed

+20
-64
lines changed

3 files changed

+20
-64
lines changed

src/content/changelog/d1/2025-04-04-d1-read-replication-beta.mdx

Lines changed: 0 additions & 34 deletions
This file was deleted.

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ title: Global read replication
33
pcx_content_type: concept
44
sidebar:
55
order: 90
6-
hidden: true
7-
noindex: true
86
---
97

108
import { GlossaryTooltip, Details, GitHubCode, APIRequest, Tabs, TabItem, TypeScriptExample } from "~/components"
@@ -18,9 +16,7 @@ To checkout D1 read replication, deploy the following Worker code using Sessions
1816
[![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)
1917

2018
:::note[Tip: Place your database further away for the read replication demo]
21-
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).
22-
23-
You can find this in the **Database location hint** dropdown.
19+
To simulate how read replication can improve a worst case latency scenario, set your D1 database location hint to be in a farther away region.
2420
:::
2521

2622
<GitHubCode
@@ -59,8 +55,8 @@ All write queries are still forwarded to the primary database instance. Read rep
5955

6056
A system with multiple read replicas located around the world improves the performance of databases:
6157

62-
- The read throughput increases by distributing load across multiple replicas. Since multiple database instances are able to serve read-only requests, your application can serve a larger number of queries at any given time.
6358
- The query latency decreases for users located close to the read replicas. By shortening the physical distance between a the database instance and the user, read query latency decreases, resulting in a faster application.
59+
- The read throughput increases by distributing load across multiple replicas. Since multiple database instances are able to serve read-only requests, your application can serve a larger number of queries at any given time.
6460

6561
## Use Sessions API
6662

@@ -324,7 +320,7 @@ With the REST API, set `read_replication.mode: disabled` to disable read replica
324320
For this REST endpoint, you need to have an API token with `D1:Edit` permission. If you do not have an API token, follow the guide: [Create API token](/fundamentals/api/get-started/create-token/).
325321

326322
:::note
327-
Disabling read replication takes up to 24 hours until replicas to stop processing requests. Sessions API works with databases that do not have read replication enabled, so it is safe to run code with Sessions API even after disabling read replication.
323+
Disabling read replication takes up to 24 hours for replicas to stop processing requests. Sessions API works with databases that do not have read replication enabled, so it is safe to run code with Sessions API even after disabling read replication.
328324
:::
329325

330326
<Tabs>
@@ -355,7 +351,9 @@ await fetch ("/v4/accounts/{account_id}/d1/database/{database_id}", {
355351

356352
### Check if read replication is enabled
357353

358-
`GET` D1 database REST endpoint returns if read replication is enabled or disabled.
354+
On the Cloudflare dashboard, check **Settings** for your D1 database to view if read replication is enabled.
355+
356+
Alternatively, `GET` D1 database REST endpoint returns if read replication is enabled or disabled.
359357

360358
For this REST endpoint, you need to have an API token with `D1:Read` permission. If you do not have an API token, follow the guide: [Create API token](/fundamentals/api/get-started/create-token/).
361359

src/content/docs/d1/tutorials/using-read-replication-for-e-com/index.mdx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
updated: 2025-03-27
2+
updated: 2025-04-09
33
difficulty: Beginner
44
content_type: Tutorial
55
pcx_content_type: tutorial
@@ -20,9 +20,9 @@ import {
2020
Details,
2121
} from "~/components";
2222

23-
[D1 Read Replication](/d1/best-practices/read-replication/) is a feature that allows you to replicate your D1 database to multiple regions. This is useful for your e-commerce website, as it reduces read latencies and improves availability. In this tutorial, you will learn how to use D1 read replication for your e-commerce website.
23+
[D1 Read Replication](/d1/best-practices/read-replication/) is a feature that allows you to replicate your D1 database to multiple regions. This is useful for your e-commerce website, as it reduces read latencies and improves read throughput. In this tutorial, you will learn how to use D1 read replication for your e-commerce website.
2424

25-
While this tutorial uses a fictional e-commerce website, the principles can be applied to any use-case that requires high availability and low read latencies, such as a news website, a social media platform, or a marketing website.
25+
While this tutorial uses a fictional e-commerce website, the principles can be applied to any use-case that requires low read latencies and scaling reads, such as a news website, a social media platform, or a marketing website.
2626

2727
## Quick start
2828

@@ -493,22 +493,10 @@ Run the following command to update the `Env` interface in the `worker-congifura
493493
npm run cf-typegen
494494
```
495495

496-
Next, enable read replication for the D1 database by running the following command. You will need to replace `<accountid>` with your Cloudflare account ID, `<databaseId>` with the ID of the D1 database, and `$TOKEN` with your Cloudflare API token. You can learn more about it in the [Read replication documentation](/d1/best-practices/read-replication/#enable-read-replication).
497-
498-
:::note
499-
Read replication is only used when the application has been [deployed](/d1/tutorials/using-read-replication-for-e-com/#step-8-deploy-the-application). D1 does not create read replicas when you develop locally. To test it locally, you can start the development server with the `--remote` flag.
500-
:::
501-
502-
```sh
503-
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/<accountid>/d1/database/<databaseId>" \
504-
-H "Authorization: Bearer $TOKEN" \
505-
-H "Content-Type:application/json" --data '{ "read_replication": { "mode": "auto" } }'
506-
```
496+
Next, enable read replication for the D1 database. Navigate to [**Workers & Pages** > **D1**](https://dash.cloudflare.com/?to=/:account/workers/d1), then select an existing database > **Settings** > **Enable Read Replication**.
507497

508498
## Step 4: Create the API routes
509499

510-
511-
512500
Update the `src/index.ts` file to import the Hono library and create the API routes.
513501

514502
```ts
@@ -544,7 +532,7 @@ The above code creates three API routes:
544532

545533
However, the API routes are not connected to the D1 database yet. In the next steps, you will create a products table in the D1 database, and update the API routes to connect to the D1 database.
546534

547-
## Step 5: Create the D1 database schema
535+
## Step 5: Create local D1 database schema
548536

549537
Create a products table in the D1 database by running the following command:
550538

@@ -648,7 +636,7 @@ In the above code:
648636

649637
Since you want to start the session with the latest data, you use the `first-primary` constraint. Even if you use the `first-unconstrained` constraint or pass a bookmark, the write request will always be routed to the primary database.
650638

651-
The bookmark set in the cookie can be used to get the data from the database for a new session from that instance.
639+
The bookmark set in the cookie can be used to guarantee that a new session reads a database version that is at least as up-to-date as the provided bookmark.
652640

653641
If you are using an external platform to manage your products, you can connect this API to the external platform, such that, when a product is created or updated in the external platform, the D1 database automatically updates the product details.
654642

@@ -768,9 +756,7 @@ curl -X POST http://localhost:8787/api/product \
768756
```
769757

770758
:::note
771-
772-
Since you have been developing the application locally, the data is not replicated to other regions. To see the data in other regions, you need to deploy the application to Cloudflare.
773-
759+
Read replication is only used when the application has been [deployed](/d1/tutorials/using-read-replication-for-e-com/#step-8-deploy-the-application). D1 does not create read replicas when you develop locally. To test it locally, you can start the development server with the `--remote` flag.
774760
:::
775761

776762
## Step 8: Deploy the application
@@ -781,6 +767,12 @@ Since the database you used in the previous steps is local, you need to create t
781767
npx wrangler d1 execute fast-commerce --remote --command "CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, description TEXT, price DECIMAL(10, 2) NOT NULL, inventory INTEGER NOT NULL DEFAULT 0, category TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"
782768
```
783769

770+
Next, create an index on the products table by running the following command:
771+
772+
```sh
773+
npx wrangler d1 execute fast-commerce --remote --command "CREATE INDEX IF NOT EXISTS idx_products_id ON products (id)"
774+
```
775+
784776
Optionally, you can insert the products into the remote database by running the following command:
785777

786778
```sh
@@ -801,7 +793,7 @@ In this tutorial, you learned how to use D1 Read Replication for your e-commerce
801793

802794
You then created the products table in the remote database and deployed the application.
803795

804-
You can use the same approach for your existing read heavy application to reduce read latencies and improve availability. If you are using an external platform to manage the content, you can connect the external platform to the D1 database, so that the content is automatically updated in the database.
796+
You can use the same approach for your existing read heavy application to reduce read latencies and improve read throughput. If you are using an external platform to manage the content, you can connect the external platform to the D1 database, so that the content is automatically updated in the database.
805797

806798
You can find the complete code for this tutorial in the [GitHub repository](https://github.com/harshil1712/e-com-d1-hono).
807799

0 commit comments

Comments
 (0)