Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Per-customer API rate limiting

Super
November 16, 2026

Per-customer API rate limiting

What to key a limit on when you charge for an API, why rate limits and quotas need different answers, four common algorithms and their trade-offs, and how to count usage so it can be billed.

The longer write-up, with response headers and code: API rate limiting algorithms and headers.

Super

November 16, 2026

Transcript

6 slides
  1. Per-customer API rate limiting Picking an algorithm, a key and

    the right response for an API you charge for UsageRail · usagerail.com 1/6
  2. What to limit by Customer (API key): key the bucket

    by customer, not by key string, or a customer with five keys gets five times the limit IP address: for sign-up, login and failed key checks, at the edge Global: for a shared upstream dependency, paired with per-customer limits UsageRail · usagerail.com 2/6
  3. Rate limits and quotas are different tools A rate limit

    works in seconds or minutes and recovers on its own A quota covers a billing period. Going past it is a billing decision Answer both with 429, but say which one it was and set Retry-After to match UsageRail · usagerail.com 3/6
  4. Algorithms Fixed window: simple, but a client can send 2x

    the limit across a window boundary Sliding window log: exact, but stores a timestamp per request Sliding window counter: two counters, approximate, usually good enough Token bucket: two numbers per customer. Burst size and refill rate read naturally in a plan UsageRail · usagerail.com 4/6
  5. Counting usage for billing Count in memory and write in

    batches, not one database write per request Give each batch an ID so a retry after a lost reply doesn't count twice Flush on graceful shutdown. A hard kill loses counts that weren't written UsageRail · usagerail.com 5/6
  6. Read more Algorithms, headers and code: per-customer API rate limiting

    From counts to invoices: metered billing for APIs: setup and pitfalls I work on UsageRail, which does this inside your own server with an SDK. Building it yourself is a reasonable choice too. UsageRail · usagerail.com 6/6

More Decks by Super

Other Decks in Programming