Scrivio Interview library
ScenarioAdvanced6 min read · updated 13 November 2026

A SaaS API limits each customer to 100 requests per second. One customer stays within the limit but calls a report export endpoint that runs 20-second database queries, and at peak those calls use 70 percent of database CPU, slowing everyone.

How do you change rate limiting so that expensive requests are limited fairly, and how do you roll the change out to existing customers?

Requests differ in cost by orders of magnitude, so counting requests cannot be fair: we should limit by cost units, where each endpoint, or each GraphQL query by its computed complexity, consumes a weight measured from real database time and CPU. Heavy endpoints also get their own per-customer limits and concurrency caps, very expensive work moves to asynchronous jobs with per-tenant fair queues and onto a replica or separate pool, and the change rolls out in shadow mode first, with affected customers contacted before enforcement and clear 429 responses after it.

Background to this question

A rate limit of 100 requests per second sounds fair until one request costs a thousand times more than another. Then a single customer can stay inside the rules and still degrade the service for everyone, which is exactly the scenario here. The question has two halves: the technical design of fair limits, and the customer-facing rollout, which is where many good designs fail. In this answer, we will see why counting requests is the wrong unit, design cost-based limits and measure the weights, isolate heavy work with separate limits, asynchronous jobs and separate pools, and then plan a rollout that does not surprise existing customers.

1Why Counting Requests Is Not Fair

A request to GET /products/42 might take 2 milliseconds of database time. A report export that runs a 20-second query takes 20,000 milliseconds, ten thousand times as much. A limit of 100 requests per second treats them as equal. The customer in the scenario is well within the limit, perhaps sending a handful of exports per minute, and yet at peak those exports use 70 percent of database CPU. Every other customer's interactive requests then queue behind them, so everyone gets slower.

So the unit is wrong. What we actually want to share fairly is the expensive resource, here database time. The limit should be expressed in something proportional to that cost.

2Limiting by Cost Units

With cost-based limiting, each customer has a token bucket of cost units instead of requests, and each request consumes a number of tokens according to its weight. A simple read might cost 1 unit, a search 5, a report export 500. The customer gets, for example, 1,000 units per second with a burst of 5,000. Light traffic fits easily; a few exports per second exhaust the bucket.

The weights come from measurement, not guesses. We record actual database time and CPU per endpoint (and for parameterised endpoints, by significant parameters such as date range), and set the weight proportional to the median or p90 cost. For GraphQL, where one endpoint can run any query, the server computes a query complexity before execution, counting fields and multiplying by requested list sizes, and charges that, a model several public GraphQL APIs use. Weights drift as queries and indexes change, so we review them regularly, for example monthly from the same metrics. A refinement is to charge an estimated cost upfront and correct it with the measured cost afterwards, which keeps the limit honest for queries whose cost varies a lot.

Example cost weights from measured database time
EndpointMedian DB timeWeight (units)
GET /products/{id}2 ms1
GET /orders?filter15 ms5
POST /search60 ms20
POST /reports/export (sync, today)20,000 ms500 and moved to async
Weights follow measured cost and are reviewed as queries change.

3Isolating Heavy Work

Cost limits alone still let a customer spend their whole allowance on heavy queries during peak. Heavy endpoints therefore get their own limits on top: for example at most 2 concurrent exports per customer, and a separate per-customer rate for the export endpoint, so even a large customer cannot run 50 at once.

Better still, very expensive operations should not run inside a request at all. A report export becomes an asynchronous job: POST /reports/exports returns 202 Accepted with a job id, the job enters a queue, and workers process queues with per-tenant fairness, taking jobs round robin across tenants so one tenant's hundred jobs do not delay everyone else's one. The total number of workers caps how much database capacity exports can ever use.

Finally, run heavy work where it cannot hurt interactive traffic: on a read replica dedicated to reporting, or on a separate connection pool with a small size, so even legitimate heavy use is physically limited. Then even if every weight were wrong, interactive requests on the primary would be unaffected.

Step throughWhere heavy requests go after the change
  1. API gatewayper-customer token bucket in cost units, 429 with Retry-After
  2. Interactive endpointsprimary database, main connection pool
  3. Export endpoint202 Accepted, job queued, max 2 running per customer
  4. Export workersround robin across tenants, fixed worker count
  5. Reporting replicalong queries run here, never on the primary

5 layers, from the top down. Step through them one at a time.

4Rolling Out Without Surprising Customers

Changing limits for existing customers is a product change as much as a technical one. We start in shadow mode: the new cost-based limiter runs on every request and logs what it would have done, but enforces nothing. After two to four weeks we know exactly which customers would be throttled, how often, and on which endpoints.

Then we act on that list. Customers who would never be affected need nothing more than a changelog entry. The handful who would be affected, including the one in this scenario, are contacted directly by their account managers, with the data, the date and the alternative: use the asynchronous export endpoint, or buy a higher tier. We publish the cost table and the new headers in the documentation, start sending rate limit headers that show remaining units before enforcement, so clients can see their consumption, and enforce on the announced date with clear 429 responses that name the exhausted resource and carry Retry-After. Large customers may get a temporary higher limit while they migrate, with an end date agreed in advance.

Step throughRollout plan
  1. Measure cost per endpointset initial weights
  2. Shadow mode for 2 to 4 weekslog who would be throttled
  3. Contact affected customersdata, date, async export, higher tier
  4. Publish docs, send headers earlyremaining units visible before enforcement
  5. Enforce with clear 429stemporary raises only with an end date

5 steps in order. Play them, or step through them one at a time.

5Follow-up questions

How do you explain cost-based limits in a public pricing page?

Keep the unit simple and the table public. Say that each plan includes a number of request units per second, that most calls cost one unit, and list the few endpoints that cost more and why, for example 'a search costs 20 units because it scans many records'. Show the remaining units in response headers and in the dashboard, so customers can see their consumption rather than guessing. Many developers are used to this from public GraphQL APIs that publish point costs, so a clear table and a calculator usually answer most questions.

What if the measured cost of an endpoint varies with its parameters?

Charge by an estimate from the parameters, for example the date range of a report, or charge an upfront estimate and adjust with the measured cost when the request finishes, so the next request sees the true remaining budget.

6Conclusion

In this answer, we saw that counting requests ignores their cost, so one customer within 100 requests per second could take 70 percent of database CPU. We limited by cost units with weights measured from real database time, gave heavy endpoints their own concurrency caps, moved exports to asynchronous jobs with per-tenant fair queues on a reporting replica, and rolled the change out in shadow mode, contacting affected customers before enforcing with clear 429 responses.

What to say in the interviewAbout 90 seconds aloud

Open with

The customer is within the rule, so the rule is wrong. A request limit treats a 2-millisecond read and a 20-second report query as equal, even though one costs ten thousand times more.

Then, if they want more

So I would limit by cost instead of count. Each customer gets a token bucket of cost units, and each endpoint consumes a weight based on measured database time and CPU, or for GraphQL a computed query complexity. I would measure real cost per endpoint to set the weights and review them as queries change.

Then I would isolate heavy work. Expensive endpoints get their own per-customer limits and concurrency caps, like two exports at a time.

The report export should not run inside a request at all: it becomes an asynchronous job, workers take jobs round robin across tenants so one tenant cannot hold all of them, and the queries run on a reporting replica or a separate small pool, so even legitimate heavy use cannot hurt interactive traffic.

For the rollout, I would run the new limiter in shadow mode for a few weeks and log who would be throttled. Then account managers contact those customers with data, a date and an alternative, we publish the cost table, send remaining-unit headers before enforcement, and finally enforce with clear 429 responses and Retry-After.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you change rate limiting so that expensive requests are limited fairly, and how...": https://scrivio-interview-library.netlify.app/architecture/api-design-at-scale/a564a1c5cb/