본문 바로가기
DB2026년 5월 27일3분 읽기

Cloudflare D1 — production 6개월, SQLite 분산의 진짜 의미

YS
김영삼
조회 450
Cloudflare D1 — production 6개월, SQLite 분산의 진짜 의미

핵심 요약

Cloudflare D1을 사내 SaaS production 6개월. 한국 사용자 응답 평균 38ms, Postgres 대비 -64%. 핵심은 read replica edge 자동 배포 + write는 primary로. SQLite의 단일 라이터 한계를 글로벌 분산으로 해결.

1. 아키텍처

  • Primary: 사용자 지정 region(미국)
  • Read replica: 전 세계 PoP에 자동, write 후 ~1초 전파
  • Write 트래픽: primary 단일, 동시 쓰기 batch 처리

2. 코드 — 읽기/쓰기 분리

// Worker
export default {
  async fetch(req: Request, env: Env) {
    if (req.method === 'GET') {
      const r = await env.DB.prepare('SELECT * FROM posts WHERE id=?')
        .bind(id).first()
      return Response.json(r)
    }
    // 쓰기는 primary region session
    return env.DB.withSession('primary').prepare(...).run()
  }
}

3. 실측

지표D1Neon Postgres(US)
Read p50(한국)38ms108ms
Write p50180ms142ms
월 비용$24$190
DB 크기4.2GB4.2GB

4. 한계

  • DB당 10GB 제한 — 큰 데이터셋은 샤딩 또는 R2 분리
  • write QPS — primary 단일, 동시 500 이상은 큐잉
  • 복잡 SQL — window 함수 등 SQLite 한계 그대로
  • migration 도구 — Drizzle Kit 지원, Prisma 미지원

5. 함정

  • eventual consistency — write 직후 read는 stale 가능. 쓰기 후 read는 같은 session으로 강제
  • trigger·view — 지원하나 미세 동작 차이, 마이그레이션 검증
  • backup — point-in-time 복구는 7일까지, 더 길게는 R2 export job

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록