Quick revision
The short answer
At the gateway we identify each client by its API key, look up its tier, and apply that tier's per-minute limit using a token bucket whose state lives in a shared store such as Redis, so all 20 instances enforce one limit instead of twenty. Spring Cloud Gateway's RequestRateLimiter with the Redis rate limiter, or Bucket4j with a Redis backend, does this. Over-limit requests get 429 with Retry-After and rate limit headers, we decide per route whether to fail open or closed when Redis is down, and a coarse per-instance limit protects the platform while the shared store is unavailable.
What to remember
In this answer, we designed rate limiting for a tiered public API on 20 gateway instances. We key limits by API key, resolve the tier from a short-lived cache, and keep token buckets in Redis so all instances share one limit, configured with Spring Cloud Gateway's RequestRateLimiter or Bucket4j. Clients see rate limit headers and get 429 with Retry-After, and when Redis is unavailable we fail open or closed per route, with a coarse per-instance limit as a safety net.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
At the gateway we identify each client by its API key, look up its tier, and apply that tier's per-minute limit using a token bucket whose state lives in a shared store such as Redis, so all 20 instances enforce one limit instead of twenty. Spring Cloud Gateway's RequestRateLimiter with the Redis rate limiter, or Bucket4j with a Redis backend, does this. Over-limit requests get 429 with Retry-After and rate limit headers, we decide per route whether to fail open or closed when Redis is down, and a coarse per-instance limit protects the platform while the shared store is unavailable.
Why this question is asked
A public API with free and paid tiers needs rate limiting for two reasons at once: to protect the platform from overload and to enforce what each customer pays for. The difficulty is that with 20 gateway instances behind a load balancer, a client's requests are spread across all of them, so no single instance sees the whole picture. Interviewers ask this design to see whether we can make limits accurate across a cluster, tell clients what is happening, and stay safe when the shared store fails. In this answer, we will identify clients and tiers, share the counters, configure it in Spring Cloud Gateway, design the response headers, and decide what happens when Redis is down.
1Identifying the Client and Its Tier
Limits are only fair if we count requests per paying customer, so the key is the API key, not the IP address. IP addresses are shared behind corporate proxies and mobile networks, and one customer may call from many of them. The gateway reads the key from a header, validates it, and looks up the customer's tier, for example free with 60 requests per minute and paid with 6,000 per minute.
The tier lookup happens on every request, so it must be fast. We cache the key-to-tier mapping in each gateway instance for a short time, such as one minute, and refresh it from the account service in the background. A plan upgrade then takes effect within a minute, which is acceptable for billing purposes.
2Why the Counters Must Be Shared
Suppose each of the 20 instances kept its own counter of 60 requests per minute for a free key. The load balancer spreads the client's requests roughly evenly, so the client could make 60 requests on each instance, 1,200 per minute in total, twenty times its quota. Dividing the limit by 20 gives each instance 3 per minute, which breaks as soon as traffic is uneven or the instance count changes with autoscaling.
So the bucket state lives in a shared store. Redis is the usual choice, because it is fast, and a small Lua script can read, refill and decrement a bucket atomically in one round trip. Every instance then takes tokens from the same bucket for a given key, and the limit is accurate regardless of which instance a request reaches.
7 messages in order. Play them, or step through them one at a time.
Any of the 20 instances would take from the same bucket in Redis.
3Configuring It in Spring Cloud Gateway
Spring Cloud Gateway has a RequestRateLimiter filter with a Redis-backed token bucket. We give it a KeyResolver, a bean that extracts the key from each request, and the bucket parameters: replenishRate is tokens added per second, burstCapacity is the bucket size, and requestedTokens is how many tokens each request costs.
Notice that the limiter works per second, so a per-minute limit becomes a per-second refill rate with a burst capacity. For the free tier, a capacity of 60 lets a quiet client spend its whole minute's allowance at once, and a refill of 1 per second keeps the average at 60 per minute. Separate routes per tier are one simple way to apply different limits; a custom RateLimiter bean, or Bucket4j, which lets us build each key's bucket configuration from its tier, is cleaner when there are many tiers. Property names have changed across Spring Cloud Gateway releases, so check your version.
We also put the gateway's own authentication filter before the limiter and reject requests without a valid key, so anonymous traffic cannot create buckets for random keys.
4Telling Clients Where They Stand
A good limit is visible to the client before it is hit. Every response carries headers with the limit and the remaining tokens, and Spring Cloud Gateway's Redis limiter adds X-RateLimit-Remaining, X-RateLimit-Replenish-Rate and X-RateLimit-Burst-Capacity by default. When a request is rejected, the gateway returns 429 Too Many Requests, and we add a Retry-After header with the seconds until a token is available, through a small filter if the version does not add it. The API documentation explains the limits per tier and how to read the headers, so integrations can slow down instead of retrying blindly.
5When Redis Is Down: Fail Open or Fail Closed
Now the hard question. If Redis becomes unreachable, the limiter cannot read buckets. Failing open means letting requests through without a limit; failing closed means rejecting them. Spring Cloud Gateway's Redis limiter fails open by default, logging the error and allowing the request (check your version). For most read endpoints that is the right choice, because an outage of a rate limiter should not become an outage of the API. For endpoints where abuse is expensive, such as sending SMS, creating accounts or costly searches, failing closed is safer.
Failing open completely is risky, though, because it removes all protection exactly when something is already wrong. So each gateway instance also enforces a coarse local limit per key, held in memory, for example generous enough that normal clients never notice, but low enough that one runaway client cannot overload the API while Redis is down. Redis itself runs replicated, and its latency and error rate are watched with alerts.
- Resolve API key and tier
- Redis reachable?YesUse the shared bucketNoUse the local fallback limit
- Token available?YesForward with rate limit headersNoReturn 429 with Retry-After
- Record metrics per tierallowed, rejected, Redis errors
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why a token bucket rather than a fixed one-minute window?
A fixed window resets at the minute boundary, so a client can send a full minute's quota at 12:00:59 and another at 12:01:00, twice the limit in two seconds. A token bucket refills continuously, so the average holds at every moment while still allowing a bounded burst.
Does the Redis call add much latency?
Usually under a millisecond within the same data centre, since it is one round trip running a small script. If that is too much, we can batch, for example taking tokens in groups locally, at the cost of slightly less accurate limits.
Should paid customers ever be throttled?
Yes, at their contracted limit, because the platform's capacity is shared. Large customers may get higher limits, a dedicated quota agreed in their contract, or their own capacity, but no customer should be unlimited.
7Conclusion
In this answer, we designed rate limiting for a tiered public API on 20 gateway instances. We key limits by API key, resolve the tier from a short-lived cache, and keep token buckets in Redis so all instances share one limit, configured with Spring Cloud Gateway's RequestRateLimiter or Bucket4j. Clients see rate limit headers and get 429 with Retry-After, and when Redis is unavailable we fail open or closed per route, with a coarse per-instance limit as a safety net.
Open with
I would identify the client by API key at the gateway, not by IP, validate it, and look up its tier from a short-lived local cache, so for example free gets sixty requests per minute and paid six thousand.
Then, if they want more
Because there are twenty gateway instances, local counters would let each client get twenty times its quota, so I would keep a token bucket per key in Redis, updated atomically with a Lua script, which is what Spring Cloud Gateway's RequestRateLimiter with the Redis rate limiter does, or Bucket4j with a Redis backend.
That limiter works per second, so sixty per minute becomes a replenish rate of one with a burst capacity of sixty, and paid becomes a hundred per second. For different tiers I would use separate routes or a custom limiter that reads the tier.
Every response would carry remaining-quota headers, and rejections would be 429 with Retry-After, documented per tier so clients can back off. The hard part is Redis failing. The gateway's limiter fails open by default, which I think is right for most reads, but for expensive endpoints like SMS or sign-up I would fail closed.
And because fully open is risky, each instance would also keep a coarse in-memory limit per key as a safety net. I would monitor allowed and rejected requests per tier and Redis errors.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Cloud documentation, Resilience4j documentation
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 with free and paid tiers across 20 gateway...": https://scrivio-interview-library.netlify.app/spring-boot/resilience/35473f312e/