Usage billing
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Metering and usage-based billing: the 60-second story

    Metering and billing

    The ack got lost, so 1,840 tokens of usage were sent twice.

    Metering must count every unit once through retries, never slow the request it measures, and survive a line-by-line check.

    “Billing” is really four jobs on four clocks

    Enforcement must be fast and can be slightly wrong. Invoices must be exact and can be slightly late. Same usage, two systems.

    When is the event ID minted?

    Bad: In the send function

    Each retry gets a fresh ID: a second charge.

    Good: Once, when the work completes

    Store it with the event in the spool; resend it unchanged.

    Delivered twice, counted once

    No network delivers exactly once. So deliver at least once, record the ID and add the quantity in one transaction, and skip IDs already seen.

    23k/s

    usage events, at 2 billion calls a day

    That’s 23× Stripe’s standard limit. Roll up at the source: per-minute totals per tenant, each with a deterministic ID, about 28× fewer.

    Used in October, arrived in November. Which invoice?

    Event time picks the period. Usage arriving within the grace period still makes October’s invoice; later usage becomes a labelled line on the next.

    Volume tiers price every call at the tier the total lands in. 1,000,000 calls, ₹3,00,000. One more?

    One call later

    ₹2,00,000.20. Every call drops to ₹0.20. Customers pad usage to cross cliffs; graduated bills never drop.

    Why quota counters can’t write invoices

    1. Nodes sync every few seconds, so quotas overshoot
    2. A Redis failover loses recent increments
    3. Counter store down? The gateway fails open
    4. They count attempts, even requests that fail

    Remember

    Mint each event’s ID once, dedupe into exact totals, invoice after a grace period. Quotas run on separate, approximate counters.