Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Design rate limiting for a public API served from three regions, where each customer has a plan with a monthly quota and a per-second limit.

I would enforce the per-second limit at the gateway in each region with local or regional counters, accepting a small over-allowance across regions instead of coordinating globally on every request. The monthly quota is tracked asynchronously from usage events, aggregated per customer and synced between regions every minute or so. Spring Cloud Gateway's RequestRateLimiter with Redis is one option for the regional layer, and limits and remaining quota are published in response headers so clients can back off.

Why this question is asked

A public API with paid plans makes rate limiting part of the product: customers pay for a quota and a speed, and they expect both to be enforced fairly in whatever region serves them. Doing this in three regions raises a real trade-off between accuracy and latency, because a perfectly global counter would put a cross-region round trip of 70 to 150 ms on every request. Interviewers ask this question to see whether we separate the two kinds of limit and pick the right consistency for each. In this answer, we will separate the per-second limit from the monthly quota, design the regional enforcement, accept and bound the over-allowance, build the quota pipeline, and finish with what clients see.

1Two Limits With Different Needs

A per-second limit, say 50 requests per second for a Pro plan, protects our capacity from bursts. It has to be decided in a millisecond, on every request, and a small error does not matter: if a customer briefly gets 55 per second, nothing breaks. A monthly quota, say 10 million requests per month, is a billing and fairness promise. It changes slowly, it is checked against a total built up over weeks, and being off by a few thousand requests at the end of the month is acceptable as long as the bill is right.

Because their needs differ, they should be enforced by different mechanisms. Trying to make one global counter serve both leads either to slow requests or to an inaccurate bill.

Two limits, two designs
AspectPer-second limitMonthly quota
Purposeprotect capacity from burstsplan and billing promise
Decision timeunder 1 ms, every requestcan lag by about a minute
Where countedregional Redis near the gatewayusage events, aggregated
Acceptable errorbrief over-allowancesmall, settled in billing
Cross-region syncnone, or a split of the limitperiodic, about every minute

2Per-Second Limits at the Regional Gateway

Each region runs an API gateway in front of the services, and the per-second check happens there, so a rejected request never touches a service. The gateway authenticates the API key, looks up the plan (cached), and checks a token bucket in a Redis cluster in the same region, which answers in well under a millisecond. Spring Cloud Gateway provides this with its RequestRateLimiter filter and RedisRateLimiter, which runs a token bucket as a Lua script in Redis. A KeyResolver decides what to limit by, here the customer id.

YAML
spring:
cloud:
gateway:
routes:
- id: public-api
uri: lb://api-backend
predicates: [ Path=/v1/** ]
filters:
- name: RequestRateLimiter
args:
key-resolver: "#{@customerKeyResolver}" # limit per customer, not per IP
redis-rate-limiter.replenishRate: 50 # tokens per second
redis-rate-limiter.burstCapacity: 100 # bucket size
redis-rate-limiter.requestedTokens: 1

Per-plan limits need more than static configuration: either one route per plan, or a custom RateLimiter bean that reads the customer's plan and applies its rate. Many teams use a managed gateway or Envoy's rate limit service instead; the design is the same. The property names here follow recent Spring Cloud Gateway versions, which have changed over time, so check your version.

3Accepting Over-Allowance Across Regions

With independent regional counters, a customer whose traffic hits all three regions could get up to 3 x 50 = 150 requests per second. There are three ways to deal with that. We can accept it, because most customers are served by one region (DNS routing sends them to the nearest one) and capacity planning can absorb the rare exception. We can split the limit, giving each region a share such as 50 percent, 30 percent and 20 percent based on recent traffic, and rebalance the shares every few minutes. Or we can coordinate globally, which adds a cross-region round trip or a globally replicated store to every request.

I would choose regional enforcement with an optional split for the few customers who really are multi-region. Global synchronous coordination costs every request 70 ms or more and makes all regions depend on one store, which defeats the point of running three regions. The worst case of over-allowance is bounded and short-lived, and the monthly quota, which is global, still caps the total.

4Monthly Quotas From Usage Events

Every gateway emits a usage event per request, or better, a per-customer count per 10 seconds, to a regional Kafka topic. A regional aggregator sums usage per customer and period and writes it to a usage store. Every minute, regional totals are replicated to the other regions (or to one global billing store), so each region knows the customer's global usage for the month to within a minute or two.

The gateway then needs only a cheap local check: a flag per customer, "quota exhausted", kept in the regional Redis. The aggregator sets the flag when global usage crosses the quota and clears it on the first of the month or after an upgrade. A customer at 10,000,000 requests might get a few thousand more before the flag propagates; we accept that, and billing uses the precise event totals, so overage can be charged or forgiven according to the contract.

Step throughQuota tracking across three regions
  1. Gateway in each regionper-second bucket in regional Redis, checks quota flag
  2. Usage eventscounts per customer every 10 seconds to regional Kafka
  3. Regional aggregatorsums usage per customer and month
  4. Cross-region syncregional totals exchanged about every minute
  5. Quota flagset in each regional Redis when the global total exceeds the plan
  6. Billinguses exact totals from the events

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

The request path only reads local state; global totals arrive asynchronously.

5Telling Clients Where They Stand

Clients can only behave well if they know the limits. Every response carries the current state in headers: the limit, the remaining requests in the current window and the time until reset, for example X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset, or the RateLimit fields being standardised by the IETF. A rejected request returns 429 with Retry-After for the per-second limit, and a clear error code such as quota_exceeded for the monthly quota, so the client knows waiting a second will not help.

We also publish the plans and limits in the API documentation, send emails when a customer reaches 80 and 100 percent of the monthly quota, and show usage in the customer dashboard from the same aggregated data. Most support tickets about throttling disappear when customers can see their own usage.

6Follow-up questions

What happens if the regional Redis fails?

The gateway should fail open for the per-second limit with a short timeout, perhaps falling back to an in-memory bucket per gateway instance, because rejecting all traffic over a limiter outage would turn a minor failure into a full outage. The quota flag can be cached in gateway memory for a minute, so a brief Redis outage does not change quota decisions. We alert on the fallback being used.

How would you handle a customer who upgrades mid-month?

The plan service publishes a plan change event. The gateways' plan cache picks up the new per-second limit within its cache time, and the aggregator recalculates whether the quota is exhausted with the new quota and clears the flag if needed. Billing prorates according to the contract. Because the request path only reads cached plan data and a flag, the upgrade takes effect within a minute without any redeploy.

7Conclusion

In this answer, we separated the per-second limit, which protects capacity and must be decided locally in a millisecond, from the monthly quota, which is a billing promise that can lag by a minute. We enforced per-second limits at each region's gateway with a regional Redis token bucket, for example Spring Cloud Gateway's RequestRateLimiter, accepted a bounded over-allowance across regions, built the monthly quota from usage events synced between regions, and published limits and remaining quota in headers so clients can back off.

What to say in the interviewAbout 100 seconds aloud

Open with

I would start by separating the two limits, because they need different designs. The per-second limit protects our capacity, has to be decided on every request in about a millisecond, and can tolerate a small error.

Then, if they want more

The monthly quota is a billing promise; it can lag by a minute but the final count must be right. For the per-second limit, each region's API gateway checks a token bucket in a Redis cluster in the same region, keyed by customer.

Spring Cloud Gateway's RequestRateLimiter with the Redis rate limiter does exactly this, or Envoy's rate limit service in a mesh. I would not coordinate globally on every request, because a cross-region round trip adds 70 milliseconds or more and couples the regions.

That means a customer who sends traffic to all three regions could briefly get up to three times the limit; for the few customers who really are multi-region, I can split the limit between regions based on recent traffic.

For the monthly quota, gateways emit usage counts as events, regional aggregators sum them, and totals are synced between regions about every minute. When a customer goes over, the aggregator sets a quota-exhausted flag in each region's Redis, which the gateway checks cheaply. Billing uses the exact event totals.

Finally, every response includes limit, remaining and reset headers, 429 responses include Retry-After, and customers get usage dashboards and alerts at 80 percent so they can back off before they hit the wall.

About this answer

Last updated
12 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 "Design rate limiting for a public API served from three regions, where each customer has...": https://scrivio-interview-library.netlify.app/spring-boot/scalability/fd7e084757/