KatInterview

Token-Based Rate Limiting for Variable-Cost API Operations

System DesignModel answer
MediumModel answerSystem Designrate-limitingdistributed-systemsapi-design

Last updated

Build a distributed rate limiter that enforces consumption budgets measured in variable billing units rather than fixed request counts.

Context

You're building infrastructure for a platform that processes variable-cost operations. Each operation consumes a different number of billing units—some might use 300 units, others 5,000—and you won't know the exact cost until after the operation completes. Operations can also run for several seconds, with costs accumulating gradually for streaming workloads.

Traditional rate limiters count requests (1 request = 1 unit), but that doesn't work here. You need to enforce budgets measured in consumption units across multiple time windows: per-minute capacity limits, hourly throttles, and monthly spending caps.

Design a distributed rate limiting system that prevents budget overruns while keeping check latency under 5 ms at p99.

Your Task

Build a service that supports this consumption model:

Two-phase flow:

  1. Reserve an estimated number of units before starting the operation
  2. Commit the actual units consumed after the operation finishes (which may differ from the estimate)

Enforcement scopes:

  • Individual resource level (per-agent, per-API-key)
  • User level (all resources belonging to one user)
  • Account level (organization-wide pool)

Policy flexibility:

  • Hard rejection when budgets are exhausted
  • Soft limits that allow small overages for high-priority operations
  • Queuing for lower-priority work that can wait

Streaming operations: Ten percent of operations stream results over 60+ seconds. Costs accumulate as the stream progresses, and you need to reconcile usage incrementally.

Scale Parameters

  • 100,000 active accounts
  • 50,000 reserve/commit operations per second at peak
  • Average time between reserve and commit: 4 seconds
  • 90% non-streaming (quick operations), 10% streaming (up to 60 seconds)

What to Address

  1. API contracts: Define the endpoints and request/response shapes for reserving, committing, and adjusting consumption.

  2. Storage and algorithms: Choose your data store and explain which rate limiting algorithm you'll adapt for variable-unit costs. Show how you'll track consumption across minute, hour, and month windows.

  3. Reservation lifecycle: Walk through what happens when an operation crashes midway. How do you prevent leaking reserved units or losing track of actual consumption?

  4. Distribution strategy: Explain how multiple limiter instances share quota state. What happens when one account generates far more traffic than others?

  5. Consistency trade-offs: Compare strict enforcement (zero overages) against eventual consistency with bounded overages. Which approach fits which use case?

  6. Failure modes: If your rate limiter becomes unreachable, should requests fail-open (allow traffic, risk overages) or fail-closed (reject traffic, preserve limits)?

Hints

Hint 1

Memory fits easily: 250k concurrent operations × 200 bytes + counters for 100k accounts across scopes = under 100 MB. Consider in-memory stores.

Hint 2

The commit step happens after work completes, so the service performing the work—not the caller—should drive commits. This prevents leakage when clients crash.

Hint 3

Token bucket naturally handles variable costs: instead of decrementing by 1, you decrement by N units. Refill rate stays constant.