-
Notifications
You must be signed in to change notification settings - Fork 10k
[D1] D1 RR Documentation #20922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[D1] D1 RR Documentation #20922
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 a497e67
Enhancing Read Replication chapter from comments
Oxyjun 8d13b91
Offloading some content to Workers API page.
Oxyjun beb3e61
Inserting example which uses cookies to retrieve
Oxyjun db88e2a
Updating diagram
Oxyjun 5d1b253
Removing redundant header
Oxyjun 8960586
Updating image again, adding starter code
Oxyjun 95c3bfa
Merge commit 'f202b11ff9e6cb29309ec9a2a5b1c5898775fb47' into jun/d1/r…
Oxyjun 0ed9469
Addressing feedback part 2, WIP
Oxyjun 5bcfcd8
Fixing link
Oxyjun 0d474cf
Fixing links
Oxyjun 50ffa22
Merge commit 'b5dbe957cf6dde2854adf1472c54026f2379cfca' into jun/d1/r…
Oxyjun 0e70d96
PM edits
vy-ton 5e1b136
Fix missing component
vy-ton 61bd1e4
Updating d1 read replication image.
Oxyjun 5810ce4
Apply Lambros' suggestions from code review part 1
Oxyjun ca8210e
Update src/content/docs/d1/best-practices/read-replication.mdx
Oxyjun 743ce11
Apply Lambros' suggestions from code review part 2
Oxyjun 6007ebc
Update src/content/docs/d1/best-practices/read-replication.mdx
Oxyjun 16d3fe4
Updating diagram with correct labels.
Oxyjun ad5d26e
Merge branch 'jun/d1/rr-v2' of github.com:cloudflare/cloudflare-docs …
Oxyjun 0f7b956
Add deploy to Workers button, pm fixes
vy-ton c384a17
fix button format
vy-ton bed72ca
Merge commit 'd202cc3d0694b1661379623c8d09d275e2972641' into jun/d1/r…
Oxyjun 4d697ce
Fixing curl json quote formatting.
Oxyjun 5d5de41
Adding in consistency diagrams and explanations.
Oxyjun 6f83aa9
Adding note on creating an API token.
Oxyjun 060c48a
add changelog, shuffle sections
vy-ton a65c5d3
fix compile tag issue
vy-ton 5caf814
fix format issues
vy-ton beb24fb
Sessions capitalisation edit. Adding demo to demo
Oxyjun 2f01a11
Fixing link for e-commerce demo bullet point
Oxyjun 31d7f4d
Minor fix
Oxyjun 333e0b4
Merge commit '4bdf47a2b13b1e2f64fb1694a2db7bebdd809aa9' into jun/d1/r…
Oxyjun 9ae1b41
Removing basic tutorial, adding featured demos.
Oxyjun ca35b8b
[D1] Add e-commerce tutorial (#21125)
harshil1712 88316c8
Tweaking wording in e-com tutorial. Linking as
Oxyjun c0003d0
Adding a note about binding name vs database name
Oxyjun 35c522b
Removing duplicate demo, using GitHubCode.
Oxyjun 1e9d9b7
Removing step of activating RR from tutorial, when
Oxyjun fc93b7e
Fixing wording, improving tutorial steps.
Oxyjun 26138e7
small fixups
vy-ton c03defb
Updating link for the D2C button
Oxyjun b37c6e7
Updating demo links, adding note to place
Oxyjun ebc9978
Hiding read replication chapter.
Oxyjun 42b78e5
pm tutorial edits
vy-ton 530bcc4
rename, add back changelog
vy-ton 7bdef48
modify demo tip
vy-ton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
561 changes: 561 additions & 0 deletions
561
src/content/docs/d1/best-practices/read-replication.mdx
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| pcx_content_type: navigation | ||
| title: Demos and architectures | ||
| sidebar: | ||
| order: 12 | ||
| order: 13 | ||
|
|
||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| pcx_content_type: navigation | ||
| title: Platform | ||
| sidebar: | ||
| order: 12 | ||
| order: 13 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| pcx_content_type: navigation | ||
| title: Reference | ||
| sidebar: | ||
| order: 13 | ||
| order: 14 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| title: SQL API | ||
| pcx_content_type: navigation | ||
| sidebar: | ||
| order: 5 | ||
| order: 6 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ pcx_content_type: navigation | |
| title: Tutorials | ||
| hideChildren: true | ||
| sidebar: | ||
| order: 11 | ||
| order: 12 | ||
|
|
||
| --- | ||
|
|
||
|
|
||
165 changes: 165 additions & 0 deletions
165
src/content/docs/d1/tutorials/test-read-replication/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - 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/) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.