// Stack

D1

Last updated ·

D1 is Cloudflare's serverless SQL database, built on SQLite and queried from Workers through a binding. EOXLABS uses D1 for relational data that many requests read and few requests write: tenant records, content, catalogs and metadata about files stored in R2, often as one small database per tenant rather than one large shared database.

How is a D1 database built, and why does it matter?

Cloudflare's guidance on D1 concurrency and throughput is unusually candid about the architecture. Each D1 database is backed by a single Durable Object and is single-threaded: it processes queries one at a time. Maximum throughput is therefore a function of query duration. If the average query takes 1 ms, the database can serve at most about 1,000 queries per second; at 100 ms it serves 10. Writes take longer than indexed reads because D1 must persist them durably across several locations.

The D1 limits table adds the hard ceilings EOXLABS designs to on Workers Paid: up to 1,000,000 databases per account, 10 GB per database (a limit Cloudflare says cannot be raised), 30 seconds per query, 2 MB per row, string or BLOB, and 1,000 queries per Worker invocation.

The single-threaded design drives most of the EOXLABS schema rules for D1:

  • An index exists for every column that appears in a hot-path WHERE clause. Cloudflare's guideline is that an indexed lookup takes less than a millisecond of SQL time; an unindexed one can take far longer.
  • No long scans or reports on the request path. A slow query does not just slow its own caller; it queues every other query behind it.
  • Large UPDATE or DELETE migrations run in chunks of about 1,000 rows, as Cloudflare recommends, instead of one statement that touches millions of rows.
  • Several statements that belong together go in one batch() call, which pays the per-request overhead once.

How does EOXLABS use D1 in practice?

One database per tenant. D1 is designed to scale out across many small databases rather than up, which explains both the high database count and the fixed 10 GB ceiling. Per-tenant databases keep each tenant's queries from competing with another tenant's on the same single thread, and make deletion or export of one tenant's data a whole-database operation. The trade-off is routing: a Worker script can bind roughly 5,000 databases, so very large tenant counts need a routing layer.

batch() as the unit of atomicity. The D1 Workers API runs a batch as a SQL transaction: statements execute sequentially, and if one fails, the whole sequence is rolled back. A batch also counts as a single query against the per-invocation limit.

Sessions for read replication. With read replication enabled, D1 keeps asynchronously updated replicas in several regions, and queries reach them only through the Sessions API. A session carries a bookmark, and every query in it is served by an instance at least as current as that bookmark. Cloudflare documents the result as sequential consistency: monotonic reads, monotonic writes, writes that follow reads, and reads that see your own writes. Replication adds no storage or compute charge.

const session = env.DB.withSession(request.headers.get("x-d1-bookmark") ?? "first-primary");

// Both rows commit, or neither does.
await session.batch([
  session.prepare("INSERT INTO orders (id, tenant_id, total_cents) VALUES (?, ?, ?)").bind(orderId, tenantId, total),
  session.prepare("INSERT INTO order_events (order_id, kind) VALUES (?, 'created')").bind(orderId),
]);

// Returning the bookmark lets this client's next read see its own write, even on a replica.
const response = Response.json({ orderId });
response.headers.set("x-d1-bookmark", session.getBookmark() ?? "");
return response;

Time Travel before every migration. D1 Time Travel restores a database to any minute in the last 30 days on Workers Paid. It is always on and costs nothing extra. EOXLABS records the current bookmark before applying a schema change, so the rollback point is written down rather than reconstructed under pressure.

Location decided at creation. A database can be restricted to a jurisdiction (eu, us or fedramp) only when it is created; the setting cannot be added later. A location hint, by contrast, is best effort. Residency requirements are therefore settled during design, not after launch.

How does D1 compare with Postgres and Durable Object storage?

D1SQLite inside a Durable ObjectPostgres through Hyperdrive
EngineSQLiteSQLitePostgreSQL
Size ceiling10 GB per database, fixed10 GB per objectSet by the Postgres provider
Who can query itAny Worker with a bindingOnly code running inside that objectAny Worker, through a standard Postgres driver
Write concurrencyOne primary, one query at a timeOne object, one event at a timePostgres concurrency control at the origin
Read scalingReplicas with sequentially consistent sessionsNone beyond the object itselfHyperdrive query cache, which can return a result cached before a recent write
Point-in-time recoveryTime Travel, 30 days30 days per objectDepends on the provider
Best fitMany small relational datasetsState owned by a single entityLarge or existing systems of record

The choice usually comes down to who needs to read the data. If only one entity's logic touches it, it belongs inside that entity's Durable Object. If many Workers query it relationally and it partitions cleanly below 10 GB, D1 fits. If it is one large dataset, or it already lives in Postgres, EOXLABS keeps it there and reaches it through Hyperdrive, typically on Neon Postgres.

When not to use D1

  • One dataset that will outgrow 10 GB and cannot be partitioned. The ceiling is fixed; plan for Postgres from the start.
  • Write-heavy hot spots. A counter updated on every request serializes the whole database behind it. A Durable Object owns that state better.
  • Long analytical queries. A single query may run for at most 30 seconds, and it blocks every other query on that database while it runs. Analytics belong in a separate pipeline, as described under data pipelines.
  • Large binary values. Rows and values are capped at 2 MB. Files go to R2, and D1 keeps the key, size and checksum.
  • Workloads built on PostgreSQL-specific features. D1 speaks SQLite's dialect; a system designed around Postgres extensions or types stays on Postgres.

D1 is part of the EOXLABS stack wherever a product needs relational data at the edge, from product engineering work to per-practice data in dental technology platforms.

→

Related

+

More stack

Have a system that can’t go dark?

Start a project →