You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -18,9 +16,7 @@ To checkout D1 read replication, deploy the following Worker code using Sessions
18
16
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/main/d1-starter-sessions-api-template)
19
17
20
18
:::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.
24
20
:::
25
21
26
22
<GitHubCode
@@ -59,8 +55,8 @@ All write queries are still forwarded to the primary database instance. Read rep
59
55
60
56
A system with multiple read replicas located around the world improves the performance of databases:
61
57
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.
63
58
- 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.
64
60
65
61
## Use Sessions API
66
62
@@ -324,7 +320,7 @@ With the REST API, set `read_replication.mode: disabled` to disable read replica
324
320
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/).
325
321
326
322
:::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.
`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.
359
357
360
358
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/).
Copy file name to clipboardExpand all lines: src/content/docs/d1/tutorials/using-read-replication-for-e-com/index.mdx
+14-22Lines changed: 14 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
updated: 2025-03-27
2
+
updated: 2025-04-09
3
3
difficulty: Beginner
4
4
content_type: Tutorial
5
5
pcx_content_type: tutorial
@@ -20,9 +20,9 @@ import {
20
20
Details,
21
21
} from"~/components";
22
22
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.
24
24
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.
26
26
27
27
## Quick start
28
28
@@ -493,22 +493,10 @@ Run the following command to update the `Env` interface in the `worker-congifura
493
493
npm run cf-typegen
494
494
```
495
495
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>" \
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**.
507
497
508
498
## Step 4: Create the API routes
509
499
510
-
511
-
512
500
Update the `src/index.ts` file to import the Hono library and create the API routes.
513
501
514
502
```ts
@@ -544,7 +532,7 @@ The above code creates three API routes:
544
532
545
533
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.
546
534
547
-
## Step 5: Create the D1 database schema
535
+
## Step 5: Create local D1 database schema
548
536
549
537
Create a products table in the D1 database by running the following command:
550
538
@@ -648,7 +636,7 @@ In the above code:
648
636
649
637
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.
650
638
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.
652
640
653
641
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.
654
642
@@ -768,9 +756,7 @@ curl -X POST http://localhost:8787/api/product \
768
756
```
769
757
770
758
:::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.
774
760
:::
775
761
776
762
## 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
781
767
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)"
782
768
```
783
769
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
+
784
776
Optionally, you can insert the products into the remote database by running the following command:
785
777
786
778
```sh
@@ -801,7 +793,7 @@ In this tutorial, you learned how to use D1 Read Replication for your e-commerce
801
793
802
794
You then created the products table in the remote database and deployed the application.
803
795
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.
805
797
806
798
You can find the complete code for this tutorial in the [GitHub repository](https://github.com/harshil1712/e-com-d1-hono).
0 commit comments