Expand description
D1 read replicas: reads go to a nearby copy of the database, writes to the primary, and each visitor still reads what they wrote (Rails’ automatic role switching, without a second database to configure).
Turn it on in two places:
- enable read replication on the database (Cloudflare dashboard, D1, Settings), free on every plan;
- set the Worker variable
D1_REPLICAStoon(D1_REPLICAS: bindings.text("on")in cloudflare.config.ts).
Each request handled by serve then queries its
databases through one D1 session: a GET or HEAD may start on any
replica, other methods start on the primary, and later statements of the
request see the earlier ones. After a request that wrote, the response
sets a short-lived cookie holding the session’s bookmark
(ocre_d1_<binding>, 5 minutes); the visitor’s next requests resume
from it, so a page shown after a form never misses the new row.
Responses of read-only requests set no cookie and stay cacheable.
Jobs, cron runs and email handlers query the primary. Without the variable, or with replication off on the database, queries behave as before: sessions reach the primary.
§Free plan
No extra cost: replication and sessions are free, and a replica answers reads that would otherwise cross the world to the primary.
§Examples
assert_eq!(ocre::replicas::REPLICAS_VAR, "D1_REPLICAS");Constants§
- REPLICAS_
VAR - Worker variable turning replicas on when set to
on:D1_REPLICAS.