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
Copy file name to clipboardExpand all lines: src/content/docs/d1/configuration/data-location.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,6 +73,6 @@ D1 location hints are not currently supported for South America (`sam`), Africa
73
73
74
74
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.
75
75
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.
77
77
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.
D1 read replication is a feature which reduces the <GlossaryTooltipterm="request latency">request latency</GlossaryTooltip> for read requests for users who may be located far away from the <GlossaryTooltipterm="primary database instance">primary database instance</GlossaryTooltip>.
12
12
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
+
exportdefault {
19
+
asyncfetch(request, env) {
20
+
const { pathname } =newURL(request.url);
21
+
constcompanyName1=`Bs Beverages`;
22
+
conststmt=env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`);
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
+
13
54
## Primary database instance vs read replicas
14
55
15
56
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.
16
57
17
-
When using read replication, D1 introduces multiple “almost up-to-date” copies of the primary database instance which only serve read requests, called <GlossaryTooltipterm="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 <GlossaryTooltipterm="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).
18
59
19
60
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.
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.
25
66
:::
26
67
27
68
| 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:
44
85
-**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.
45
86
-**Read my own writes**: If you write to the database, all subsequent reads will see the write.
46
87
47
-
## How D1 read replication works
88
+
## Use Sessions API
48
89
49
90
### Use read replication via Sessions API
50
91
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 <GlossaryTooltipterm="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.
54
93
55
94
### Bookmarks
56
95
@@ -94,16 +133,18 @@ When continuing an existing Session started with `withSession`, you can provide
94
133
95
134
By default (when the `<constraint>` is either `null` or unspecified), D1 Session uses `first-primary` as the starting condition.
96
135
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`
98
141
99
142
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.
100
143
101
144
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.
102
145
103
146
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.
###Example of starting a new Session with `first-unconstrained`
163
+
#### Start a new Session with `first-unconstrained`
123
164
124
165
Suppose you want to develop a feature for displaying “likes” on a social network application.
125
166
126
167
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.
127
168
128
169
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.
// NOTE: Achieve sequential consistency with given bookmark,
@@ -160,6 +199,13 @@ A system with multiple read replicas located around the world improves the perfo
160
199
161
200
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).
162
201
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.
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.
18
18
19
19
## Prerequisites
20
20
@@ -144,6 +144,6 @@ By completing this tutorial, you have:
Copy file name to clipboardExpand all lines: src/content/glossary/d1.yaml
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -16,3 +16,6 @@ entries:
16
16
- term: "query planner"
17
17
general_definition: |-
18
18
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