Design a Quota Management Service
Understanding the Problem
📦 What is a quota management service? A quota service tracks how much of a limited resource each customer is using and says yes or no when they ask for more. The classic example is a single storage allowance (say 15 GB) shared across several products: files in Drive, photos in Photos and attachments in Gmail all count against the same number. Every product asks the quota service before it stores something, and tells it when something is deleted.
Interviewers sometimes mean a different kind of quota: API request quotas ("1,000 calls per minute"). That's rate limiting, a separate problem covered in /system-design/rate-limiter. Storage quota is a running total that changes slowly and must be accurate over months. Rate limits are windows that reset every second or minute and can afford to be approximate. It's worth saying which one you're designing.
Functional Requirements
Core Requirements
- Products (Drive, Photos, Gmail) should be able to ask whether a user can store N more bytes, and get a yes or no before they accept the upload.
- Products should be able to report usage changes as items are created, edited, trashed and permanently deleted, so the total stays accurate.
- Users should be able to see their total usage, their limit, and a breakdown by product.
- Users should be able to change plans (upgrade to 100 GB, downgrade) or share one pool with a family group, and have the new limit apply right away.
Below the line (out of scope)
- Billing and payments for plans. We receive "user X is now on the 2 TB plan" from a billing system.
- The products' own file storage and metadata.
- API request rate limits.
Non-Functional Requirements
Ask about scale and about how strict the limit is. For our design:
Core Requirements
- The system should support 2 billion users and on the order of 500,000 usage changes per second at peak, summed across all products.
- The quota check is on the upload path, so it should add under 20 ms at p99.
- Usage must stay accurate over time. Drift of a few megabytes is tolerable; drift of gigabytes is a support ticket.
- A user must not be able to exceed their quota by a meaningful amount, even by uploading from many devices at once. A small, temporary overage (a few MB, or one in-flight file) is acceptable.
- The service should be highly available. If it's down, products shouldn't stop accepting uploads entirely.
Below the line (out of scope)
- Exact, transactional consistency between the quota counter and each product's storage. We'll get close and reconcile.
Some math. 2B users with a few counters each (one per product, plus totals and limit) at around 100 bytes per user is about 200 GB. That fits in a sharded database comfortably; it's not a big-data problem. The write rate is high in aggregate (500k/s) but tiny per user: most people store a few things a day. Even a phone backing up 5,000 photos is a few hundred writes over several minutes.
So throughput isn't the problem. The two hard parts are keeping one number consistent with what several independent products actually store, and making a check-then-store decision without letting concurrent uploads blow through the limit.
A useful thing to say early: "per user, this is a low-volume counter, so we can shard by user and make each check a strongly consistent single-row operation. The difficulty is correctness across systems, not scale."