Skip to content

Commit d83e3b7

Browse files
Apply suggestions from code review
Co-authored-by: Lambros Petrou <[email protected]>
1 parent 0418154 commit d83e3b7

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/content/changelog/d1/2025-09-11-d1-automatic-read-retries.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ The retry success ratio among all retryable errors is lower, indicating that the
1717

1818
![D1 automatically query retries success ratio](~/assets/images/changelog/d1/d1-auto-retry-success-ratio.png)
1919

20-
D1 ensures that any retry attempt does not cause database writes, making the automatic retries safe from side-effects, even if a query slips through the read-only detection. D1 achieves this by checking for modifications after every query execution, and if any write occurred due to a retry attempt, the query is rolled back.
20+
D1 ensures that any retry attempt does not cause database writes, making the automatic retries safe from side-effects, even if a query causing changes slips through the read-only detection. D1 achieves this by checking for modifications after every query execution, and if any write occurred due to a retry attempt, the query is rolled back.
2121

2222
The read-only query detection heuristics are simple for now, and there is room for improvement to capture more cases of queries that can be retried, so this is just the beginning.

src/content/docs/d1/best-practices/retry-queries.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ D1 automatically retries read-only queries up to 2 more times when it encounters
1818

1919
Consider the following example, taken from the [D1 read replication starter template](https://github.com/cloudflare/templates/blob/main/d1-starter-sessions-api-template/src/index.ts#L108).
2020

21+
You should make sure your retries apply an exponential backoff with jitter strategy for more successful retries.
22+
You can use libraries abstracting that already like [`@cloudflare/actors`](https://github.com/cloudflare/actors), or [copy the retry logic](https://github.com/cloudflare/actors/blob/9ba112503132ddf6b5cef37ff145e7a2dd5ffbfc/packages/core/src/retries.ts#L18) in your own code directly.
23+
2124
```ts
25+
import { tryWhile } from "@cloudflare/actors";
26+
27+
function queryD1Example(d1: D1Database, sql: string) {
28+
const result = await tryWhile(async () => {
29+
return await d1.prepare(sql).run();
30+
}, shouldRetry);
31+
}
32+
2233
function shouldRetry(err: unknown, nextAttempt: number) {
2334
const errMsg = String(err);
2435
const isRetryableError =

src/content/docs/d1/observability/debug-d1.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Before retrying any failed operation:
8383

8484
D1 detects read-only queries and automatically attempts up to 2 retries to execute those queries in the event of failures with retryable errors.
8585

86-
D1 ensures that any retry attempt does not cause database writes, making the automatic retries safe from side-effects, even if a query slips through the read-only detection. D1 achieves this by checking for modifications after every query execution, and if any write occurred due to a retry attempt, the query is rolled back.
86+
D1 ensures that any retry attempt does not cause database writes, making the automatic retries safe from side-effects, even if a query causing modifications slips through the read-only detection. D1 achieves this by checking for modifications after every query execution, and if any write occurred due to a retry attempt, the query is rolled back.
8787

8888
:::note
8989
Only read-only queries (queries containing only the following SQLite keywords: `SELECT`, `EXPLAIN`, `WITH`) are retried. Queries containing any [SQLite keyword](https://sqlite.org/lang_keywords.html) that leads to database writes are not retried.

0 commit comments

Comments
 (0)