Cloud
Per-Tenant Quotas
Checking and updating a tenant's quota is O(1): one lookup, one comparison, maybe one addition. The design value is isolation. No matter how many tenants share the system, one tenant's usage can never be counted as another's.
The idea, in plain English
Imagine an apartment building where every unit has its own water meter and its own monthly cap. If one tenant runs the tap all day, it only counts against their own meter. It doesn't use up water that belongs to the tenant next door, and it doesn't get shut off because someone else went over. A 'tenant' in cloud software means the same thing as in the building: one customer, or one company's account, using a shared system. A 'quota' is that customer's personal cap: a fixed amount of some resource, such as storage, API calls, or uploaded video minutes, that they can use before the system says no more, at least until the cap resets.
How it works
- 1Give every tenant their own running usage counter, starting at zero, and their own quota: the cap they're allowed to reach.
- 2When a tenant makes a request that consumes some amount of the resource, check: would adding this amount push them over their quota?
- 3If there's room, allow it and add the amount to that tenant's counter. Only that tenant's counter changes.
- 4If it would go over, deny the request and leave their counter untouched. One tenant hammering the system can't eat into anyone else's allowance.
When you'd use it
Use this for any system serving multiple customers off shared infrastructure — a SaaS product, a multi-tenant API, cloud storage sold by the gigabyte. Per-tenant quotas keep one customer's traffic spike or runaway script from starving everyone else. They're also how usage-based billing tiers get enforced.
Common beginner mistakes
- Tracking one shared counter for everyone instead of one per tenant. That turns a single noisy customer into an outage for every other customer sharing the pool.
- Letting a request partially succeed when it would go over quota, using up whatever room is left, instead of cleanly allowing it in full or denying it in full. Partial writes get confusing fast.
- Forgetting to handle a tenant that doesn't exist in the system at all. That should be denied outright, not silently treated as having unlimited quota.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
tenantA requests 40 units -> allowed (usage 40/100)
tenantB requests 30 units -> allowed (usage 30/50)
tenantA requests 50 units -> allowed (usage 90/100)
tenantB requests 25 units -> denied, quota exceeded (usage 30/50, would be 55)
tenantA requests 20 units -> denied, quota exceeded (usage 90/100, would be 110)
tenantC requests 10 units -> denied, unknown tenant
Final usage: tenantA 90/100, tenantB 30/50Not sure this is the right topic? See the learning paths → or where this leads →