SaaS usage quota enforcement AWS is the check that stops a tenant from consuming more of a contracted unit than their plan includes, before the expensive work is queued. The unit is whatever the contract sells: pages extracted, minutes encoded, messages sent, seats occupied. A request-per-second limit is a different control. It keeps one tenant from melting the API. It does not know that this tenant bought 10,000 pages this period and has used 10,000. Rutagon builds the period meter on AWS, in the request path, for products that already have tenants. On a production SaaS platform the meter runs before the job is enqueued, and a later pass reconciles it to work that actually finished.
The period id is the one the billing system already uses. A calendar month in UTC and a subscription anniversary are different periods. The meter stores the period id it is given. It does not invent a second calendar and then disagree with the invoice.
Where SaaS usage quota enforcement AWS runs
The reserve happens in the API process, in the same transaction as the idempotency record, and it happens before the message is sent to the worker queue. A worker that checks the quota after it has already done the work can only apologize. The customer was billed in compute for a unit you then refuse to count, or you count it and blow the plan. Reserving first means the deny is cheap. The work that would have exceeded the quota never starts.
API Gateway usage plans meter requests. They are the right tool when the contracted unit is a request. They are the wrong tool when one request extracts forty pages and the contract is in pages. Burst limits and usage plans still belong on the edge, and they are the subject of API Gateway rate limiting. The business quota is a counter keyed by tenant and period. How that usage becomes an invoice line is the neighboring build in usage metering and billing. How the tenant id got onto the row in the first place is SaaS tenant provisioning.
The counter lives in DynamoDB. The row is created at the start of the period with used set to zero. The reserve adds the requested amount only while used is still within the remaining quota. A conditional write is the check. Two concurrent requests cannot both read the same remaining balance and both succeed past the limit.
def reserve(client, table, tenant_id, period, request_id, amount, quota):
room = quota - amount
client.transact_write_items(
TransactItems=[
{
"Put": {
"TableName": table,
"Item": {
"pk": {"S": "req#" + request_id},
"sk": {"S": tenant_id},
},
"ConditionExpression": "attribute_not_exists(pk)",
}
},
{
"Update": {
"TableName": table,
"Key": {
"pk": {"S": "meter#" + tenant_id},
"sk": {"S": period},
},
"UpdateExpression": "ADD used :amount",
"ConditionExpression": "attribute_exists(used) AND used <= :room",
"ExpressionAttributeValues": {
":amount": {"N": str(amount)},
":room": {"N": str(room)},
},
}
},
]
) The request id makes a retry safe. The put fails if that request was already reserved, and the whole transaction rolls back, including the add. The caller then reads the stored decision for that request id and returns it again. A retry that only repeats the add will charge the tenant twice for one piece of work, and the client's SDK will do exactly that when it sees a timeout. The meter row must already exist. The condition attribute_exists(used) rejects a reserve against a missing period rather than creating a counter with no quota beside it.
tenant_id is a required argument. The key is built from it. There is no default tenant, and there is no code path that updates meter# with an empty id. A missing tenant id is a rejected request, not a charge against a shared row that every buggy caller shares.
Soft limits, hard limits, and release
A soft limit and a hard limit read the same used value. The soft limit, often at a fraction of the quota the contract names, writes a warning event and still reserves. The hard limit is the condition above: the transaction fails, and the API returns a stable error code, quota_exceeded. Using the same 429 that the edge rate limit uses, with no distinct code, teaches every SDK to retry a condition that will not clear until the next period. The error body carries quota_exceeded. The idempotency key returns that same denial on retry, so the client can stop.
When reserved work fails before it finishes, the unit has to come back. The release is a second transaction with its own idempotency key. It subtracts the amount and writes a release record. It does not SET used to a number a process computed in memory. A set wipes concurrent reserves that happened after the read. The release record is what finance and support look at when a customer says the dashboard and the invoice disagree.
Reconciliation compares the sum of completed billable events for that tenant and period with used. Drift in either direction becomes an adjustment record: a reason, an amount, a request id. A crash after reserve and before the worker runs looks like reserved-but-not-completed, and the release path should already have handled it. Reconciliation catches the releases that never ran. It also catches a worker that completed and failed to record the event, which is the opposite bug and the one customers notice when they are under quota on the invoice and out of quota in the product. The adjustment is appended. The counter is corrected by the same add and subtract paths, not by an operator editing the item in the console.
Seats are a slightly different unit. A seat quota changes when a person is added or removed, and the "amount" is one seat, reserved at invite time and released when the person is removed. The same conditional write works. A nightly recount of active people is the reconciliation. The invite path still has to reserve, or two admins can invite past the seat cap in the same minute and the nightly job can only send an apologetic email.
What the customer and the examiner can see
The tenant-facing response on a deny includes the period id, the quota, and the used amount at the time of the deny. Support will be asked for those three numbers, and they should match the meter row. A generic "limit reached" message produces a ticket you cannot answer from the meter. The admin view shows the same row plus the recent reserves, releases, and adjustments. That view is enough for a billing dispute. A log line that says the check failed, with no amounts, is not.
The evidence that the control exists is the code path, the table, and a sample of denies and releases. For a SOC 2 conversation about whether one tenant can affect another, the key structure is the answer: every meter key contains the tenant id, and the reserve function has no cross-tenant update. A test that runs two tenants to the same quota and shows one tenant's deny leaving the other tenant's used unchanged belongs in the pack. We keep that test next to the table definition.
Rutagon builds this into the product API rather than into a spreadsheet the finance team maintains after the fact. The spreadsheet can still be the commercial source of the quota number. The API reads that number into the meter at period open. A quota change mid-period updates the quota attribute the condition compares against. It does not reset used. Resetting used on a plan upgrade gives the tenant the new plan's full allowance plus what they already consumed, which is a commercial bug wearing an engineering fix.
This is a full-stack build on AWS: the API, the conditional write, the worker, and the reconciliation job. The edge rate limit stays. The period meter is the piece the contract actually describes. We implement it per product, with the unit that product sells, on the tenant id that product already has.
Frequently Asked Questions
Why not use an API Gateway usage plan as the quota?
Use it when the unit you sell is a request. A usage plan counts requests over a period, which is the right meter for that contract. When the unit is pages, minutes, or seats, one request is not one unit. The usage plan can still cap request rate. The DynamoDB counter caps the contracted unit. Running only the usage plan will enforce the wrong quantity.
What stops a client retry from consuming quota twice?
The reserve transaction writes the request id with a condition that the id does not already exist, in the same transaction as the counter update. A retry fails the put, the add rolls back, and the API returns the decision already stored for that request id. A timeout is then safe. A new request id is a new unit of work and consumes quota on purpose.
How do failed jobs return unused quota?
A release transaction subtracts the amount and stores its own idempotency record. It uses ADD with a negative amount, not a SET of a freshly read total, so a concurrent reserve is preserved. Reconciliation later compares completed events to used and writes an adjustment if a release never ran. Console edits of the counter are not part of the path.
When is the meter row created?
At the start of the period, with used at zero and the quota from the billing source. The reserve condition requires the attribute to exist, so a missing row is a deny rather than a silent create. A mid-period quota change updates the quota attribute and leaves used as it is. The period id matches the billing period. A second calendar inside the meter will not match the invoice.
Talk to us about quota enforcement in your product API. Contact Rutagon at rutagon.com/contact, 907-841-8407, or contact@rutagon.com.