Skip to content
Back to Cloud Services

Cloudflare D1 SQLite Database

Edge SQLite database with read replicas worldwide, query from Workers with sub-10ms latency.

CloudflareIntermediate

Why

D1 puts SQLite at the edge with automatic read replicas — reads are 5-10ms globally vs 50-200ms to a single-region Postgres. Free tier: 5GB storage, 5M rows read/day.

Perfect for read-heavy apps.

When

Use D1 for blogs, catalogs, config, session stores, and read-heavy APIs. Avoid for heavy writes (single primary), large joins (>5MB results), or apps needing Postgres-specific features.

How

1. Create DB — wrangler d1 create mydb (returns database_id) 2. Bind — [[d1_databases]] binding="DB" database_name="mydb" database_id="..." 3. Migrate — wrangler d1 migrations create mydb init, wrangler d1 migrations apply mydb 4. Query — env.DB.prepare("SELECT * FROM users WHERE id=?").bind(id).first() 5. Batch — env.DB.batch([stmt1, stmt2]) for transactional multi-statement 6. Backup — wrangler d1 backup create mydb (point-in-time restore) 7. Read replicas — automatic, no config; writes go to primary, reads hit nearest replica

Example

// migrations/0001_init.sql CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT UNIQUE, created_at TEXT DEFAULT CURRENT_TIMESTAMP); CREATE INDEX idx_users_email ON users(email); // src/index.ts — D1 query with prepared statement export default { async fetch(request: Request, env: Env): Promise<Response> { const url = new URL(request.url); if (url.pathname === "/api/users") { const { results } = await env.DB.prepare("SELECT id, email FROM users ORDER BY created_at DESC LIMIT 20").all(); return Response.json(results); } const id = url.pathname.split("/").pop(); const user = await env.DB.prepare("SELECT * FROM users WHERE id = ?").bind(id).first(); return user ? Response.json(user) : new Response("Not found", { status: 404 }); }, };

Common Problems

  • ⚠Write latency 50-200ms — single primary region, writes aren't edge-local like reads
  • ⚠5MB query result limit — large SELECTs truncate, paginate with LIMIT/OFFSET
  • ⚠No real-time triggers — D1 doesn't emit change events, poll or use Durable Objects

Best Practices

  • ✓Use read replicas for reads — D1 auto-routes reads to nearest replica, don't cache in KV
  • ✓Run migrations in CI — wrangler d1 migrations apply --remote in deploy step
  • ✓Use batch() for transactions — atomic multi-statement, avoids partial writes

FAQ

What is Cloudflare D1 SQLite Database and what problem does it solve?

Edge SQLite database with read replicas worldwide, query from Workers with sub-10ms latency. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with Cloudflare D1 SQLite Database in 5 minutes?

Two steps to run it: (1) Create DB — wrangler d1 create mydb (returns database_id); (2) Bind — [[d1_databases]] binding="DB" database_name="mydb" database_id="...". Validate on the free tier first, then wire into production.

What is the #1 pitfall with Cloudflare D1 SQLite Database?

The most frequent issue is: Write latency 50-200ms — single primary region, writes aren't edge-local like reads. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for Cloudflare D1 SQLite Database?

Best practices for Cloudflare D1 SQLite Database include: Use read replicas for reads — D1 auto-routes reads to nearest replica, don't cache in KV; Run migrations in CI — wrangler d1 migrations apply --remote in deploy step; Use batch() for transactions — atomic multi-statement, avoids partial writes. The first one — "Use read replicas for reads — D1 auto-routes reads to nearest replica, don't cache in KV" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with Cloudflare D1 SQLite Database?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most Cloudflare D1 SQLite Database options offer a free tier — validate within free quota before scaling.