D1 Read Replicas

Read replicas let you query data from servers closer to you, which means faster response times and less waiting. Speed up your queries in just a few clicks.

Your query was   0ms faster using the closest replica
Compared to querying a primary database in the furthest region (-ms)

Enable Replication

Today you can create new or enable existing D1 databases with global read replication. Requests will be routed to the nearest replica and benefit from the fastest read speeds.

Enable Read replication
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/d1/database/{database_id}" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"read_replication": {"mode": "auto"}}'

Read Consistency

D1's Session API allows you to maintain read consistency across multiple queries. By storing and reusing the session bookmark, you can ensure that subsequent reads see all changes from previous operations. This is particularly useful when you need to maintain a consistent view of the database across multiple requests.

Session API Example
// Retrieve bookmark from previous session stored in HTTP header
const bookmark = request.headers.get('x-d1-bookmark') ?? 'first-unconstrained';
const session = env.DB.withSession(bookmark)

// Since this is a write query, D1 will transparently forward the query.
await session
    .prepare("INSERT INTO Orders VALUES (?, ?, ?) ON CONFLICT DO NOTHING")
    .bind(order.customerId, order.orderId, order.quantity)
    .run();

// In order for the application to be correct, this SELECT
// statement must see the results of the INSERT statement above.
const result = await session.prepare("SELECT * FROM Orders").all();

// Store bookmark for a future session
response.headers.set('x-d1-bookmark', session.getBookmark() ?? "")

Ready to get started?

Click the button to deploy read replicas now or read the docs to learn more.