Skip to content

Commit 1e3d32b

Browse files
committed
Adding Worker code to RR tutorial.
1 parent 0861280 commit 1e3d32b

File tree

1 file changed

+64
-1
lines changed
  • src/content/docs/d1/tutorials/test-read-replication

1 file changed

+64
-1
lines changed

src/content/docs/d1/tutorials/test-read-replication/index.mdx

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,69 @@ Populate your database with the table from the [D1 get started](/d1/get-started/
8080
Write a Worker file which queries the table and outputs both the results with the query latency.
8181

8282
```js
83-
// TBC
83+
export default {
84+
async fetch(request, env) {
85+
const { pathname } = new URL(request.url);
86+
const companyName1 = `Bs Beverages`;
87+
const stmt = env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`);
88+
const session = env.DB.withSession("first-unconstrained");
89+
90+
if (pathname === `/run`) {
91+
const tsStart1 = Date.now();
92+
const { results, meta } = await stmt.bind(companyName1).run();
93+
const d1Duration1 = Date.now() - tsStart1;
94+
return Response.json({ results, meta, d1Duration1 });
95+
96+
} else if (pathname === `/withsession`) {
97+
const tsStart2 = Date.now();
98+
const { results, meta } = await session.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`).bind(companyName1).run();
99+
const d1Duration2 = Date.now() - tsStart2;
100+
return Response.json({ results, meta, d1Duration2 });
101+
}
102+
return new Response(
103+
`Welcome to the D1 read replication demo!
104+
105+
Add one of the following slugs below to see the effects of using D1 read replication.
106+
107+
\n/run - Queries the table without using read replication
108+
109+
\n/withsession - Queries the table using read replication (using "first-unconstrained")
110+
111+
\nUse the two options to compare the difference in query latency.`
112+
);
113+
}
114+
};
84115
```
85116

117+
## 6. Deploy Worker
118+
119+
Deploy your worker.
120+
121+
```sh
122+
npx wrangler deploy
123+
```
124+
125+
## 7. Compare query latency
126+
127+
Once deployed, you can compare the query latency when using read replication.
128+
129+
- Use the `/run` URL to send a read query without read replication.
130+
- Use the `/withsession` URL to send a read query with read replication.
131+
132+
For both queries, the Worker script returns the `meta` object, which contains `served-by-primary` boolean field. This field indicates whether your request was served by the primary database instance.
133+
134+
The `d1Duration` variable shows the query latency.
135+
136+
## Summary
137+
138+
By completing this tutorial, you have:
139+
140+
1. Created a D1 database using a location hint.
141+
2. Created a Worker script which uses D1 Sessions to use read replication.
142+
3. Deployed the Worker to test the difference in query latency when using read replication.
143+
144+
## Related resources
145+
146+
- [D1 read replication](/d1/concepts/read-replication)
147+
- [D1 Sessions Workers Binding API](/d1/worker-api/d1-database#withsession)
148+
- [D1 read replication demo application](/d1/demos/)

0 commit comments

Comments
 (0)