Corporate API cost model — why pushes win
Numbers computed for 10 corporate tenants × 50 accounts × 100 tx/day, Workers Paid ($5/mo: 10M requests + 30M CPU-ms included, then ~$0.30/M req, ~$0.02/M CPU-ms), D1 ~$0.001 per 1.000 rows read, Queues ~$0.40/M ops, R2 $0.015/GB-month with free egress. Verify current pricing before committing.
1. Three designs, same product
| Design | Requests/mo | Workers cost | D1 rows read/mo | D1 cost | Verdict |
|---|---|---|---|---|---|
| A. Naive poll: client GETs every account every 60s | 21.600.000 | $11,94 | 1,08 miliar | ~$1.080 | kills margin |
| B. Batched poll: 50 accounts/request, every 5 min | 86.400 | $5,01 | 4,32 juta | $4,32 | acceptable |
| C. Webhook push + 1 daily reconcile | 36.300 | $5,01 | 1,82 juta | $1,81 | best |
Punchline: Worker requests are almost free. Row reads are the cost. A 250× difference in requests produced an 11× difference in dollars only because D1 row reads dominate — and it would be identical with Postgres behind Hyperdrive. So the architecture decision is not "how do I reduce requests", it is "never query the ledger per request".
2. The design
WRITE PATH (containers)
robot/SNAP adapter → normalise → Postgres ledger → outbox
→ R2 snapshot per (tenant, account) [immutable, versioned]
→ Queue → webhook delivery
READ PATH (edge Worker)
auth (API key → KV, 1 read) → check R2 manifest → stream snapshot
if stale (>1 refresh) → rebuild from Postgres once, write R2, serve
balance: KV with 60s TTL (one upstream call per minute per account, not per client)
mutations: cursor endpoint returns only rows newer than `since` — client stores cursor
Why it works:
- R2 Class B reads (~$0.36/M) + zero egress replace row reads entirely.
- Immutable snapshot keyed by (tenant, account, watermark) means no rebuild on repeat reads — cache hit rate approaches 1 for pollers.
- ETag/If-None-Match → 304 with no body; still one request, ~0 CPU.
3. Contract-level guardrails (these are the real cost control)
| Control | Value | Reason |
|---|---|---|
| Default channel | webhook | polling is opt-in, priced |
| Polling floor | 1 request / 60s / key | below that, use webhooks |
| Batch endpoint | up to 50 accounts per request | 50× request reduction |
| Cursor pagination | ?since=<cursor> |
no full refetch, no duplicate billing |
| Cached snapshot read | GET /accounts/{id}/snapshot |
immutable, cheap, no DB |
| Balance cache | 60s TTL | prevents per-client upstream storms |
| Per-key rate limit | 120 req/min (tenant), 10 req/min (trial) | KV token bucket |
| Hard quota | 250k req/mo included per tenant, then Rp5.000/100k | overage pays for itself |
| 429 contract | Retry-After + X-RateLimit-* |
clients back off instead of retry-storming |
Internal cost at those quotas: a tenant at the full 250k = ~$0,07 of Workers + ~$0,09 of R2 — while the plan is Rp6jt/mo. Overage at Rp5.000/100k is ~10× cost. The pricing is not the risk; an unbounded poll loop is.
4. What to avoid
| Anti-pattern | Cost | Fix |
|---|---|---|
SELECT * FROM mutations WHERE tenant=? ORDER BY time DESC per request |
D1/Postgres row reads explode | snapshot in R2 / KV |
| WebSocket stream per client | Durable Objects hours + complexity | webhooks |
| Full-history JSON export per cron | CPU + egress + memory | signed R2 download URL, generate once |
| Per-client upstream balance call | hits bank rate limits, not CF cost | one cache, 60s TTL, shared across clients |
| Retrying failed webhooks inline | request time + duplicate work | Queue + exponential backoff + DLQ |
5. Cost at scale (webhook-first)
| Tenants | Accounts | Requests/mo | CF bill/mo |
|---|---|---|---|
| 10 | 500 | ~36k | $5 |
| 50 | 2.500 | ~180k | $5 |
| 200 | 10.000 | ~730k | $5–6 |
| 1.000 | 50.000 | ~3,6M | $5–6 |
Edge stays effectively free until request volume crosses 10M/mo — roughly 4.000 tenants. Ingestion containers and Postgres are the cost that grows, not the API.
6. Metering
Count requests per key in a Durable Object (or Analytics Engine dataset) — one write per request, not per row. Monthly job aggregates to usage table, feeds billing and the tenant dashboard ("API calls this month: 41.233 / 250.000"). Do not meter in D1 per-call.
That metering table is also the sales artefact: it proves the tenant is getting value, which is what renews a Rp6jt contract.