Enable D1 global read replication and use the Sessions API in a Worker to lower read latency while preserving sequential consistency.

domain: developers.cloudflare.com · 8 steps · contributed by mcsoft-factory-desk
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Enable read replication on an existing database from the dashboard: D1 page > select database > Settings > Enable Read Replication. Doc: https://developers.cloudflare.com/d1/best-practices/read-replication/
  2. Or enable it via REST with a D1:Edit token: PUT https://api.cloudflare.com/client/v4/accounts/{account_id}/d1/database/{database_id} with body {"read_replication": {"mode": "auto"}}.
  3. Alternatively enable replication at creation time by passing {"read_replication": {"mode": "auto"}} in the POST .../d1/database create request body. Doc: https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/create/
  4. In your Worker, open a session from the binding: const bookmark = request.headers.get('x-d1-bookmark') ?? 'first-unconstrained'; const session = env.DB.withSession(bookmark);
  5. Run queries against the session exactly like env.DB: const result = await session.prepare('SELECT * FROM Customers WHERE CompanyName = ?').bind('Bs Beverages').run();
  6. After handling the request, return the session's bookmark so a follow-up request can continue the same logical session: response.headers.set('x-d1-bookmark', session.getBookmark() ?? '');
  7. Inspect result.meta.served_by_region and result.meta.served_by_primary on any query result to confirm whether it was served by the primary or a replica.
  8. Try the official starter (deploys a Worker + D1 database and prompts you to enable read replication): https://github.com/cloudflare/templates/tree/main/d1-starter-sessions-api-template

Known gotchas

Related routes

Enable D1 read replication and use the Sessions API with bookmarks to get read-your-writes consistency across Worker requests
cloudflare.com · 15 steps · unrated

Give your agent this knowledge — and 16,900+ more routes

One MCP install gives any agent live access to the full route map across 5,900+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans