Skip to content

Workers KV vs D1 vs R2: pick the right storage

A decision matrix for Cloudflare's three primary edge data stores — KV for read-heavy global config, D1 for transactional SQLite, R2 for blobs with no egress fees — with workload examples and binding setup.

You’re building on Workers and need state. The platform offers three distinct primitives — Workers KV, D1, and R2 — and the documentation makes them all sound usable for “everything.” They aren’t. Each has a shape that suits a narrow class of workloads, and the wrong choice shows up as latency in the wrong place, a bill in the wrong category, or a consistency bug that’s hard to reproduce. This article is the decision matrix we use when scoping a Worker that needs to persist something.

How to verify

The three CRUD surfaces from Wrangler look like this. KV writes propagate to the edge over seconds, so the read-after-write below may need a beat:

wrangler kv:namespace create CONFIG
wrangler kv:key put --binding=CONFIG "feature-flag-new-checkout" "true"
wrangler kv:key get --binding=CONFIG "feature-flag-new-checkout"

D1 is SQLite. You can ship migrations and run ad-hoc queries:

wrangler d1 create app-db
wrangler d1 execute app-db --command "CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT);"
wrangler d1 execute app-db --command "SELECT count(*) FROM users;"

R2 is S3-API-compatible — use any S3 client with an R2 endpoint:

wrangler r2 bucket create assets
wrangler r2 object put assets/logo.png --file ./logo.png
aws s3 ls s3://assets --endpoint-url https://${ACCOUNT_ID}.r2.cloudflarestorage.com

Each of those commands establishes a different binding pattern in wrangler.toml; the binding name (e.g. env.CONFIG, env.DB, env.ASSETS) is how the Worker reads and writes.

What’s happening

The three stores are intentionally different shapes because they solve different problems.

Workers KV is an eventually-consistent global key-value store optimised for reads. A write goes to the central plane and is asynchronously replicated to every edge cache; a read at the edge hits a local copy. That means reads are fast everywhere (single-digit ms after warm-up), writes can take tens of seconds to be visible globally, and consistency is “eventual.” KV is the right place for feature flags, runtime configuration, JWT public keys, A/B test routing tables, anything that’s read 100x more than it’s written and where a 60-second propagation delay is fine.

D1 is SQLite running at the edge. It’s a real relational database with transactions, indexes, foreign keys, and SQL. It’s accessed via a binding and a prepared-statement API; under the hood it currently runs in a single primary region with read replicas (the platform is evolving on this — check the Cloudflare blog for current replica topology). D1 fits small-to-medium transactional workloads: user accounts for a side project, a feedback widget’s submissions, the metadata for an asset library. It is not a substitute for Postgres on a busy app — there are per-database size and per-query duration limits — but for a few-million-row OLTP workload, it removes a service from your stack.

R2 is object storage. The API surface is S3-compatible, so any client library that speaks S3 will work. R2 charges for storage and for Class A/B operations, but not for egress — moving a TB out to a CDN, a Worker, or a user costs zero in transfer fees. That makes R2 the right home for user uploads, large media, build artifacts, log archives, anything where the bill on AWS S3 would be dominated by data transfer. Pair R2 with a Worker to gate access (signed URLs, auth checks) or expose it through a custom domain.

Storage decision treeWhat’s the shape of the data?size + access patternsmall + read-heavyrelational + writesblobs / large objectsWorkers KVeventually consistentglobal edge cacheD1SQLite, transactionalprepared statementsR2S3-compatible objectsno egress feesExamplesfeature flagsJWKS keysrouting tablessmall session cacheExamplesuser accountsform submissionsasset metadataaudit logsExamplesuser uploadsimages and videobuild artifactslog archivesCombine freely — a single Worker can bind to all three at once.
KV for read-heavy config, D1 for relational state, R2 for blobs. A Worker can bind all three.

Decision

Match the workload to the store by walking these four questions in order.

  1. Is this a binary blob over a few hundred KB, or counted in MB-to-GB? Then R2. Don’t put images, video, PDFs, or build artifacts in KV (the value size limit makes it impractical) or D1 (BLOBs are a SQLite anti-pattern at any meaningful scale). Bind it as [[r2_buckets]] binding = "ASSETS" bucket_name = "assets" and read with env.ASSETS.get('key').

  2. Does the data need transactions, queries with joins or filters, or referential integrity? Then D1. Bind as [[d1_databases]] binding = "DB" database_name = "app-db" database_id = "..." and use the prepared-statement API: env.DB.prepare("SELECT * FROM users WHERE id = ?").bind(id).first(). Treat it like a real SQL database — ship migrations through wrangler d1 migrations apply, write tests, don’t issue 200 sequential queries per request.

  3. Is the data tiny, read 100x more than written, and tolerant of seconds of staleness? Then KV. Bind as [[kv_namespaces]] binding = "CONFIG" id = "..." and use env.CONFIG.get('flag'). The headline workloads are feature flags, JWKS keys, edge-config snapshots, A/B test buckets, IP-to-region tables. Avoid KV for anything where read-after-write must be immediate.

  4. Is the data write-heavy, hot per-key, and needs strong consistency? None of the three is the right primary, and Durable Objects is the platform’s answer for that shape — a chat room state, a rate limiter that needs accurate counts, a leaderboard. Don’t force-fit KV or D1 onto a Durable-Object workload.

For attaching a Worker to a hostname so it can use these bindings, see the first-script and routes article.

Common pitfalls

  • KV’s “eventual” propagation is real — a write you make in one request can take up to 60 seconds to be visible from an edge in another region; don’t build write-then-read flows on KV.
  • D1’s single-write-primary means high-frequency writes from many regions queue at the primary; benchmark before assuming the edge latency of reads applies to writes.
  • R2 has no egress fees, but Class A operations (PUT, LIST, COPY) cost more than Class B (GET, HEAD) — a write-heavy crawler workload can still surprise you on the bill.
  • D1 query duration is capped per request; long analytical queries belong elsewhere (export to a warehouse) — D1 is for OLTP shapes.
  • Binding names in wrangler.toml and env.* references in code must match exactly; a typo gives you undefined, not an error.

For teams shipping on Workers, the storage decision is one of the first design calls and one of the easiest to get wrong; in the engagements Stack Harbor runs, we make that call explicitly per workload as part of the managed-operations practice and revisit it whenever the access pattern shifts.