KatInterview

Design a Distributed Usage Enforcement System

System DesignModel answer
HardModel answerSystem Designrate-limitingdistributed-systemsconsistency

Last updated

Design a quota enforcement service that handles millions of checks per second across many tenants, covering APIs, consistency trade-offs, hot-key handling, and billing accuracy.

Distributed Usage Enforcement System

Many large platforms expose APIs and managed resources — storage buckets, virtual machines, network bandwidth — and need to prevent any single tenant from consuming more than their allocated share. Your task is to design a service that other internal services call to check and record usage in real time.

Context

The service must handle two meaningfully different kinds of limits:

  • Throughput limits — e.g., 5,000 API calls per second, 10M requests per day. These refill over time. A small, bounded overage is acceptable.
  • Capacity limits — e.g., 500 GB of stored data, 20 virtual machines. These are held until explicitly released. Exceeding them is not acceptable.

What to design

Define the core API. What operations does the enforcement service expose? Think about what inputs callers need to provide, what responses they get back, and how the interface handles retries safely.

Address concurrency correctness. At very high request rates, multiple callers will race to consume from the same quota simultaneously. How do you prevent incorrect over-admission without serializing everything through a single bottleneck?

Compare strong and eventual consistency. Explain when each is appropriate for this system, what the trade-offs are in latency and accuracy, and how you bound the error in the eventual case.

Handle hot keys. A single large tenant might generate over one million requests per second against a single quota key. How does your architecture remain accurate and fast in that case?

Reconcile usage for billing. Your enforcement counters may drift or reset. How do you ensure that the billing system sees an accurate, deduplicated view of every unit consumed, including any overage that slipped through enforcement?

Discuss availability and latency targets. State reasonable numbers and explain the design choices that let you meet them.

Constraints and scale

  • Roughly 10M quota checks per second globally, across roughly 50M distinct quota keys
  • p50 latency under 1 ms, p99 under 5 ms for throughput checks; ~10–20 ms is acceptable for capacity changes
  • 99.99% availability — the enforcement service must not be a single point of failure for every downstream caller
  • Hot-key traffic: a single key may see over 1M QPS
  • Overage on throughput quotas should be bounded to roughly 1–5% of the configured limit
  • Billing must be exact and idempotent, even when enforcement is approximate

Expected output

A structured walkthrough covering the API, data model, architecture, and a deep dive on each of the five areas above. Discuss trade-offs explicitly and call out common mistakes.

Hints

Hint 1

Split the problem in two before touching architecture: throughput quotas (refilling, eventual consistency is fine) and capacity quotas (held until released, need strong consistency). The right design for each is quite different, and mixing them causes subtle bugs.

Hint 2

The classic concurrency mistake is a read-check-write race where two callers both read 'used=99, limit=100' and both succeed. Think about how a single-writer shard, a conditional atomic update, or a client-side lease each eliminate this race — and what each one costs in latency or complexity.

Hint 3

Enforcement counters and the billing ledger should be separate concerns. Every served request should emit a durable, deduplicated usage event. Billing reads only from that log, so overage that slips through enforcement still shows up accurately — and a reconciliation job can detect counter drift after failovers.