Skip to content

Commit 229543c

Browse files
committed
Fixing links, editing the chapter structure.
1 parent 7ceabd7 commit 229543c

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

src/content/docs/d1/configuration/data-location.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ D1 location hints are not currently supported for South America (`sam`), Africa
7373

7474
D1 read replication allows you to create and distribute 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.
7575

76-
When using D1 read replication through [Sessions API](/d1/concepts/read-replication/#how-d1-read-replication-works), 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.
76+
When using D1 read replication through [Sessions API](/d1/features/read-replication/#how-d1-read-replication-works), 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.
7777

78-
Refer to [D1 read replication](/d1/concepts/read-replication/) for more information.
78+
Refer to [D1 read replication](/d1/features/read-replication/) for more information.

src/content/docs/d1/features/read-replication.mdx

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,68 @@
11
---
2-
title: D1 read replication
2+
title: Read replication
33
pcx_content_type: concept
44
sidebar:
55
order: 2
66

77
---
88

9-
import { GlossaryTooltip } from "~/components"
9+
import { GlossaryTooltip, Details } from "~/components"
1010

1111
D1 read replication is a feature which reduces the <GlossaryTooltip term = "request latency">request latency</GlossaryTooltip> for read requests for users who may be located far away from the <GlossaryTooltip term="primary database instance">primary database instance</GlossaryTooltip>.
1212

13+
You can use D1 read replication by using D1 Sessions API. 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.
14+
15+
By using Sessions API for read replication, all of your queries from a single Session read from a version of the database which is as up-to-date as your query. This ensures that the version of the database you are reading is logically consistent with your queries when using read replicas.
16+
17+
```js collapse={8-15, 24-31}
18+
export default {
19+
async fetch(request, env) {
20+
const { pathname } = new URL(request.url);
21+
const companyName1 = `Bs Beverages`;
22+
const stmt = env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`);
23+
const session = env.DB.withSession("first-unconstrained");
24+
25+
// Without Sessions API / read replication
26+
if (pathname === `/run`) {
27+
const tsStart1 = Date.now();
28+
const { results, meta } = await stmt.bind(companyName1).run();
29+
const d1Duration1 = Date.now() - tsStart1;
30+
return Response.json({ results, meta, d1Duration1 });
31+
}
32+
33+
// With Sessions API + read replication
34+
else if (pathname === `/withsession`) {
35+
const tsStart2 = Date.now();
36+
const { results, meta } = await session.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`).bind(companyName1).run();
37+
const d1Duration2 = Date.now() - tsStart2;
38+
return Response.json({ results, meta, d1Duration2 });
39+
}
40+
41+
// Welcome text
42+
return new Response(
43+
`Welcome to the D1 read replication demo!
44+
Add one of the following slugs below to see the effects of using D1 read replication.
45+
/run - Queries the table without using read replication
46+
/withsession - Queries the table using read replication (using "first-unconstrained")`
47+
);
48+
}
49+
};
50+
```
51+
52+
{/* Deploy to Workers button */}
53+
1354
## Primary database instance vs read replicas
1455

1556
When using D1 without read replication, D1 routes all queries (both read and write) to a specific database instance in [one location in the world](/d1/configuration/data-location/), known as the primary database instance. The request latency is dependent on the physical closeness of a user to the primary database instance - it takes less time for light (information) to travel between the user and the primary database instance if that distance is shorter. Users located further away from this database instance experience the longest request latency.
1657

17-
When using read replication, D1 introduces multiple “almost up-to-date” copies of the primary database instance which only serve read requests, called <GlossaryTooltip term="read replica"> read replicas </GlossaryTooltip>. D1 creates the read replicas in multiple regions throughout the world [across the Cloudflare network](/d1/concepts/read-replication/#read-replica-locations).
58+
When using read replication, D1 introduces multiple “almost up-to-date” copies of the primary database instance which only serve read requests, called <GlossaryTooltip term="read replica"> read replicas </GlossaryTooltip>. D1 creates the read replicas in multiple regions throughout the world [across the Cloudflare network](/d1/features/read-replication/#read-replica-locations).
1859

1960
A user may be located far away from the primary database instance but close to a read replica. By sending their read requests to the read replica instead of the primary database instance, the user enjoys shorter read request response times. For example, the request latency when using the primary database instance may be 250 ms, versus 20 - 50 ms when using a read replica.
2061

2162
![D1 read replication concept](/images/d1/d1-read-replication-concept.png)
2263

2364
:::note
24-
All write requests are still forwarded to the primary database instance. Read replication only improves the query response time for read requests.
65+
All write queries are still forwarded to the primary database instance. Read replication only improves the query response time for read requests.
2566
:::
2667

2768
| Type of database instance | Description | How it handles write queries | How it handles read queries |
@@ -44,13 +85,11 @@ Sequential consistency has properties such as:
4485
- **Writes follow reads**: If you read a value, then perform a write, the subsequent write must be based on the value that was just read.
4586
- **Read my own writes**: If you write to the database, all subsequent reads will see the write.
4687

47-
## How D1 read replication works
88+
## Use Sessions API
4889

4990
### Use read replication via Sessions API
5091

51-
You can use D1 read replication by using D1 Sessions API. 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.
52-
53-
By using Sessions API for read replication, all of your queries from a single Session read from a version of the database which is as up-to-date as your query. This ensures that the version of the database you are reading is logically consistent with your queries when using read replicas.
92+
By using <GlossaryTooltip term="session">Sessions API</GlossaryTooltip> for read replication, all of your queries from a single Session read from a version of the database which is as up-to-date as your query. This ensures that the version of the database you are reading is logically consistent with your queries when using read replicas.
5493

5594
### Bookmarks
5695

@@ -94,16 +133,18 @@ When continuing an existing Session started with `withSession`, you can provide
94133

95134
By default (when the `<constraint>` is either `null` or unspecified), D1 Session uses `first-primary` as the starting condition.
96135

97-
### Example of starting a new Session with `first-primary`
136+
For more information on Sessions API, refer to the [D1 Workers Binding API documentation](/d1/worker-api/d1-database#withsession).
137+
138+
### Examples
139+
140+
#### Start a new Session with `first-primary`
98141

99142
Suppose you want to develop a webpage for an electricity provider which lists the electricity bill statements. An assumption here is that each statement is immutable. Once issued, it never changes.
100143

101144
In this scenario, you want the first request of the page to show a list of all the statements and their issue dates. Therefore, the first request starts a new D1 Session using the constraint `first-primary` to get the latest information (ensuring that the list includes all issued bill statements) from the primary database instance.
102145

103146
Then, when opening an individual electricity bill statement, we can continue using the same Session by passing the `bookmark` from the first query to subsequent requests. Since each bill statement is immutable, any bill statement listed from the first query is guaranteed to be available in subsequent requests using the same Session.
104147

105-
#### Example Worker code
106-
107148
```ts
108149
async function listBillStatements(accountId: string, db: D1Database): ListBillStatementsResult {
109150
const session = db.withSession("first-primary");
@@ -119,16 +160,14 @@ async function getBillStatement(accountId: string, billId: string, bookmark: str
119160
}
120161
```
121162

122-
### Example of starting a new Session with `first-unconstrained`
163+
#### Start a new Session with `first-unconstrained`
123164

124165
Suppose you want to develop a feature for displaying “likes” on a social network application.
125166

126167
The number of likes is a good example of a situation which does not require the latest information all the time. When displaying the number of likes of a post, the first request starts a new D1 Session using the constraint `first-unconstrained`, which will be served by the nearest D1 read replica.
127168

128169
Subsequent interactions on the application should continue using the same Session by passing the `bookmark` from the first query to subsequent requests. This guarantees that all interactions will observe information at least as up-to-date as the initial request, and therefore never show information older than what a user has already observed. The number of likes will be updated with newer counts over time with subsequent requests as D1 asynchronously updates the read replicas with the changes from the primary database.
129170

130-
#### Example Worker code
131-
132171
```js
133172
async function getLikes(postId: string, db: D1Database, bookmark: string | null): GetLikesResult {
134173
// NOTE: Achieve sequential consistency with given bookmark,
@@ -160,6 +199,13 @@ A system with multiple read replicas located around the world improves the perfo
160199
161200
To see the impact of read replication, check the latency for your read-only queries. Refer to the [queryBatchTimeMs](/d1/observability/metrics-analytics/) metric or [view your metrics through the dashboard](/d1/observability/metrics-analytics/#view-metrics-in-the-dashboard).
162201
202+
## Known limitations
203+
204+
There are some known limitations for D1 read replication.
205+
206+
- The lifecycle of a read replica is tied to the lifecycle of the primary database instance. For example, iof the primary database instance is inactive, the read replicas are inactive too.
207+
- TBC
208+
163209
## Supplementary information
164210
165211
You may wish to refer to the following resources:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ languages:
1414

1515
import { Render, Steps, Tabs, TabItem, FileTree } from "~/components";
1616

17-
In this tutorial, you will create a basic Worker script to test the effect of [D1 read replication](/d1/concepts/read-replication/), and see the difference in the request latency.
17+
In this tutorial, you will create a basic Worker script to test the effect of [D1 read replication](/d1/features/read-replication/), and see the difference in the request latency.
1818

1919
## Prerequisites
2020

@@ -144,6 +144,6 @@ By completing this tutorial, you have:
144144

145145
## Related resources
146146

147-
- [D1 read replication](/d1/concepts/read-replication)
147+
- [D1 read replication](/d1/features/read-replication)
148148
- [D1 Sessions Workers Binding API](/d1/worker-api/d1-database#withsession)
149149
- [D1 read replication demo application](/d1/demos/)

src/content/glossary/d1.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ entries:
1616
- term: "query planner"
1717
general_definition: |-
1818
A component in a database management system which takes a user query and generates the most efficient plan of executing that query (the query plan). For example, the query planner decides which indices to use, or which table to access first.
19+
- term: "session"
20+
general_definition: |-
21+
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.

0 commit comments

Comments
 (0)