Skip to content

Phase 2 — Usage R2 & Zuplo KV (구현 참조)

Ref: implementation plan §4, §4.6, §9.6.

R2 Raw 경로 및 레코드 형식

  • 경로: usage_raw/tenant_id=<uuid>/dt=YYYY-MM-DD/hour=HH/part-<timestamp>.jsonl
  • 필드: ts (ISO8601), tenant_id, meter_key, qty, path, status

Aggregator Worker는 위 파티션을 읽어 D1 usage_rollups에 Upsert (멱등 키: tenant_id, meter_key, dt, hour).

Zuplo Soft Quota KV (결정 ②)

  • 키 구조: usage:{tenant_id}:{meter_key}:{cycle_start}
  • cycle_start: 해당 Billing Cycle 시작일 (예: 2026-02-01)
  • TTL: Cycle 종료 후 정리 (예: 35일). Zuplo 설정에서 KV 백엔드 지정.

Zuplo에서 Soft Quota를 KV로 전환하면 재시작 후에도 카운트가 유지됨.

파이프라인

  1. API 요청 → Zuplo (Soft Quota KV 갱신) → (선택) 동일 요청을 prego-usage-writer로 전달하거나, Zuplo 로그/엣지에서 usage_raw에 JSONL 적재.
  2. usage-writer Worker: POST /usage body { tenant_id, meter_key?, qty?, path?, status? } → R2 usage_raw/... 에 한 줄 JSONL 추가.
  3. usage-aggregator Worker: Cron 15분마다 R2 List → 집계 → D1 usage_rollups Upsert.
  4. cycle-close Worker: 월말 Cron → usage_rollups 합계, included 차감, overage → Stripe Usage Record (또는 Invoice).

생성된 Worker

Worker역할
workers/usage-writerPOST /usage → R2 usage_raw
workers/usage-aggregatorCron: R2 → usage_rollups
workers/cycle-closeCron 월말: usage_cycle_totals, Stripe 연동

R2 버킷 prego-usage-raw는 Pulumi prego-pulumi에서 export됨.

Help