Skip to main content

ADR-010: Database - D1 (SQLite) on Cloudflare

Status: Accepted

Parent: ADR-008 - Phase 3 Cost Architecture

Context

We need a database for Phase 3 features: customer records, subscriptions, payment history, printer configs, and photo metadata. The database should integrate with our existing Cloudflare Pages/Workers infrastructure.

Decision

We will use Cloudflare D1 (SQLite) as our primary database.

Justification

1. Single Vendor Benefits

Staying within Cloudflare ecosystem:

  • Single dashboard for all infrastructure
  • No egress costs between Workers and D1
  • Integrated with existing deployment pipeline
  • Wrangler CLI for migrations

2. Cost Efficiency

DatabaseMonthly CostNotes
D1Rp 0-200,0005GB free, paid from Rp 500K
SupabaseRp 400,000+Higher tier needed
NeonRp 150,000+Additional vendor

3. Performance for Indonesia

  • D1 read latency from Jakarta: ~80ms
  • Global replication available
  • Edge caching potential

Data Model

API Layer

D1 will be accessed via Cloudflare Workers:

// workers/api/payments.ts
export const onRequest: PagesFunction = async (context) => {
const db = context.env.DB;

// Create payment record
await db
.prepare(
`
INSERT INTO payments (id, customer_id, amount, status)
VALUES (?, ?, ?, ?)
`,
)
.bind(crypto.randomUUID(), customerId, amount, "pending");

// ...
};

Migrations

Using Wrangler CLI:

# Create migration
wrangler d1 migrations create photobooth_db create_customers

# Apply migrations
wrangler d1 migrations apply photobooth_db --local
wrangler d1 migrations apply photobooth_db --remote

Consequences

Positive

  • Zero/low cost (5GB free tier)
  • Single vendor (Cloudflare)
  • Fast from Jakarta (~80ms)
  • Simple SQLite - no connection pooling issues
  • Integrated with Workers

Negative & Risks

  • SQLite limitations (no stored procs, triggers)
  • Less tooling compared to Postgres
  • D1 is relatively new
  • Need to handle migrations carefully

Alternatives Considered

Supabase (PostgreSQL)

  • Pros: Full Postgres, better ORM support
  • Cons: Higher cost, additional vendor, egress fees

Neon (PostgreSQL)

  • Pros: Serverless auto-scaling, good tooling
  • Cons: Higher latency from Indonesia, additional vendor