Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

An API gateway enforces 1,000 requests per second per customer by giving each of its 10 nodes a local limit of 100 per second, with no shared state. After autoscaling to 40 nodes, large customers got four times their limit, while on quiet nights some were throttled far below it.

Explain both effects and compare the ways to enforce a global limit across many nodes.

Local limits silently assume a fixed node count and an even spread of traffic: with 40 nodes each allowing 100 per second, the global limit became 4,000, and when a customer's traffic lands on only a few nodes, those nodes throttle at 100 per second while the others sit idle. To enforce a global limit we can divide the limit by the current healthy node count, use a central counter such as Redis with an atomic script, or take tokens from the central store in batches, and we can route each customer to few nodes with consistent hashing so local decisions become accurate.

Background to this question

Local, shared-nothing rate limiting is attractive because it needs no extra infrastructure and adds no latency. It works as long as two hidden assumptions hold, and autoscaling breaks both. This question is popular because it combines distributed systems reasoning with practical trade-offs: accuracy, latency, availability and cost. In this answer, we will explain both effects from the scenario, then compare four ways to enforce a global limit, from cheapest and least accurate to exact and most expensive, and finally decide which fits which kind of limit.

1Why Local Limits Broke in Both Directions

The design gave each of 10 gateway nodes a limit of 100 requests per second per customer, assuming that 10 x 100 = 1,000 and that the load balancer spreads each customer's traffic evenly. The first assumption breaks when the node count changes: after autoscaling to 40 nodes, each still allows 100, so a customer whose traffic is spread over all of them can send 40 x 100 = 4,000 per second, four times the contract. Large customers, whose traffic touches every node, saw exactly that.

The second assumption breaks when traffic is not spread evenly. At night, a customer might send 300 requests per second over a few long-lived HTTP/2 or keep-alive connections, and the load balancer pins each connection to one node. If two connections land on two nodes, the customer is capped at 2 x 100 = 200 per second, well below the 1,000 they pay for, while 38 nodes see none of their traffic. The same happens if the load balancer itself is uneven. So the same design over-admits at peak and under-admits at night.

Effective limit with local counters of 100 per node
SituationNodes carrying the customerEffective global limit
Design assumption10, even spread1,000 per s
After autoscaling40, even spread4,000 per s
Quiet night, 2 long-lived connections2200 per s
The contract is 1,000 per second in every row.

2Option 1: Local Limits Derived From the Node Count

The cheapest fix keeps local counters but recomputes the local limit whenever the cluster changes: local limit = global limit / healthy nodes. Each node learns the count from service discovery, for example the number of ready endpoints in Kubernetes, and with 40 nodes sets 1,000 / 40 = 25 per second per customer.

This fixes the autoscaling over-admission at no runtime cost. It does nothing for uneven traffic: the customer on two connections at night would now be capped at 2 x 25 = 50 per second, even worse. So it suits cases where traffic per customer is high and spread over many connections, and where roughly right is good enough.

3Option 2: A Central Counter

The exact option is a shared counter that every node checks on every request, typically Redis. A Lua script runs atomically on the Redis server, so a token bucket or sliding window can be read and updated in one round trip without races between nodes. Every request now sees the same count, so the limit is exactly 1,000 per second whatever the node count or distribution.

SQL
-- KEYS[1] = bucket key, ARGV = capacity, rate per second, now in ms
local b = redis.call('HMGET', KEYS[1], 'tokens', 'ts')
local tokens = tonumber(b[1]) or tonumber(ARGV[1])
local ts = tonumber(b[2]) or tonumber(ARGV[3])
tokens = math.min(tonumber(ARGV[1]), tokens + (tonumber(ARGV[3]) - ts) / 1000 * tonumber(ARGV[2]))
local allowed = 0
if tokens >= 1 then tokens = tokens - 1; allowed = 1 end
redis.call('HSET', KEYS[1], 'tokens', tokens, 'ts', ARGV[3])
redis.call('PEXPIRE', KEYS[1], 60000)
return allowed -- 1 pass, 0 reject with 429

The costs are real, though. Every request adds a network round trip, around half a millisecond inside a data centre. Redis must handle the platform's whole request rate, so it needs a cluster sharded by customer key. And the limiter becomes a dependency: when Redis is slow or down, we must choose to fail open (let traffic through, perhaps with a generous local fallback limit) or fail closed (reject). That decision should be made deliberately, not discovered during an outage.

4Option 3: Hybrid Batches and Periodic Sync

The hybrid keeps most decisions local but coordinates through the central store occasionally. In one form, each node takes a batch of tokens, say 20, from the central bucket and spends them locally, asking for more only when it runs out. In another, each node counts locally and every 100 to 500 milliseconds pushes its counts to the central store and reads back the global total, adjusting its local share. Remote calls drop from one per request to a few per second per node and customer.

The trade-off is a small overshoot: between syncs, nodes act on slightly stale information, so a burst can exceed the limit by up to roughly one batch per node or one sync interval's traffic. For most API limits a few percent of overshoot is a fair price for removing a round trip from every request.

Step throughHybrid: nodes take tokens in batches
Node ANode BCentral store
take 20 tokens for customer C
granted 20 (bucket 980 -> 960)
serve 20 requests locally
take 20 tokens for customer C
granted 20
take 20 more
granted 5: bucket nearly empty

7 messages in order. Play them, or step through them one at a time.

One remote call per batch instead of per request.

5Option 4: Route Each Customer to Few Nodes

Accuracy problems come from one customer's traffic being spread over many nodes. If the load balancer routes by customer key with consistent hashing, a hash ring where each customer maps to a small, stable set of nodes, then each customer's traffic lands mostly on, say, two or three nodes. Those nodes see nearly all of that customer's traffic, so their local counters are close to the truth, and the central store is needed only for the small remainder. Consistent hashing also moves only a fraction of customers when nodes are added. It is not always possible, for example with an external load balancer that cannot hash on an API key, but where it is, it makes every other option cheaper.

6Follow-up questions

Which option would you choose for limits that protect the platform, and which for billing quotas?

For limits that protect the platform from overload, approximate is fine and availability matters most, so I would use the hybrid with local fallback, or node-count-derived limits plus per-instance concurrency limits, and fail open if the central store is down. For billing quotas, such as 10 million calls a month on a paid plan, accuracy matters and a short delay does not, so I would count every request centrally, asynchronously if needed (for example aggregating usage events in a stream and reconciling), and enforce the quota from that exact count, failing closed only for the hard quota.

What about many gateway regions?

A single global Redis would add cross-region latency to every request, so each region usually gets its own share of the limit or its own hybrid store, with regions exchanging counts every few seconds. Exact global limits across regions are rarely worth their cost.

7Conclusion

In this answer, we saw that per-node limits assume a fixed node count and even traffic, so autoscaling to 40 nodes quadrupled the effective limit while uneven routing at night throttled customers far below it. We compared deriving local limits from the node count, a central Redis counter updated by an atomic script, a hybrid that takes tokens in batches or syncs periodically, and consistent-hash routing of customers to few nodes, and matched approximate options to platform protection and exact counting to billing quotas.

What to say in the interviewAbout 90 seconds aloud

Open with

Local limits only work under two assumptions: a fixed number of nodes and an even spread of each customer's traffic. Autoscaling broke the first one. With 40 nodes each still allowing 100 per second, a big customer spread across all of them could send 4,000 per second, four times the limit.

Then, if they want more

At night the second one broke: a customer with a couple of long-lived connections lands on two nodes, each throttles at 100, so they are capped at 200 while 38 nodes sit idle. There are a few ways to enforce a global limit.

The cheapest is to divide the limit by the number of healthy nodes from service discovery, which fixes autoscaling but still fails with uneven traffic.

The exact one is a central counter, for example a token bucket in Redis updated by a Lua script on every request, but that adds a round trip and makes the limiter a dependency, so we have to decide whether to fail open or closed.

The hybrid is usually my choice: nodes take tokens from the central store in batches, or sync counts every few hundred milliseconds, which accepts a small overshoot for far fewer remote calls. And where possible I would route by customer key with consistent hashing so each customer hits only a few nodes, which makes local decisions accurate. For platform protection, approximate is fine; for billing quotas, I would count exactly.

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 "Explain both effects and compare the ways to enforce a global limit across many nodes.": https://scrivio-interview-library.netlify.app/architecture/api-design-at-scale/dde440ae8b/