Scrivio Interview library
DesignIntermediate7 min read · updated 13 November 2026

Three consumer services read different topics and all call the same partner API, which allows 2,000 requests per second in total and returns HTTP 429 above it. Each service runs 6 to 12 pods.

During peaks the partner rejects 30 percent of calls, and the services' retries make it worse. Design how the consumers share the limit and slow down without losing records.

Enforce the limit where the calls start: a shared token bucket, for example in Redis or a small rate-limiting service, with per-service quotas that add up to 2,000 requests per second, checked by every pod before it calls. When no token is available or the partner returns 429, slow the consumer instead of failing records: pause the container or partitions, keep polling, and resume when capacity returns, so lag absorbs the peak; respect Retry-After, back off with jitter, and never send rate-limited records to retry topics. Reserve a share of the quota for urgent work in its own topic or group, and monitor tokens, 429s, lag and time to drain per service.

Why this question is asked

A shared external limit is a classic distributed systems problem in disguise. Each of the three services behaves sensibly on its own, but none of them knows what the others are doing, so together they exceed the partner's limit, get rejected, and retry into an even bigger overload. Interviewers ask this to see whether we can coordinate independent consumers around one budget, turn rejections into backpressure rather than failures, and keep the important work flowing. In this answer, we will see why the current setup fails, build a shared rate limiter, connect it to the consumers so they slow down without losing records, handle the partner's signals, add priorities and set up monitoring.

1Why the Current Setup Makes It Worse

Three services with 6 to 12 pods each means 18 to 36 independent callers. If each pod limits itself to a fixed rate, the total depends on how many pods happen to be running, and autoscaling adds pods exactly when traffic peaks. Without any limit, each pod simply calls as fast as it consumes.

When the partner returns 429 Too Many Requests, the services retry, often immediately or through retry topics. Each retry is another request in the same overloaded second, so the rejection rate rises, which causes more retries. This is a retry storm: the system spends its capacity on requests that will be rejected. Rejecting 30 percent of calls during peaks is the visible part; the hidden part is that a large share of the remaining calls are retries.

2A Shared Token Bucket

A token bucket is a simple rate-limiting model. A bucket holds tokens, refilled at a fixed rate, say 2,000 per second, up to a maximum burst size. Each call takes one token; if the bucket is empty, the call must wait. To share one limit across 36 pods, the bucket must live in one place that every pod can reach, such as Redis, with the take-a-token step done atomically in a Lua script, or a small rate-limiting service in front of the partner.

We divide the 2,000 into per-service quotas that add up to the total, for example 1,000 for orders, 600 for refunds and 400 for catalogue sync, each with its own bucket. That way one service's peak cannot consume another's share. Quotas can also allow borrowing unused capacity, but a fixed split is easier to reason about first. To keep Redis traffic reasonable, each pod can take tokens in small batches, say 20 at a time, and spend them locally.

SQL
-- Redis Lua: take n tokens from a bucket refilled at rate per second, up to burst
local tokens = tonumber(redis.call('HGET', KEYS[1], 't') or ARGV[3])
local last = tonumber(redis.call('HGET', KEYS[1], 'ts') or ARGV[4])
local now, rate, burst, n = tonumber(ARGV[4]), tonumber(ARGV[1]), tonumber(ARGV[3]), tonumber(ARGV[2])
tokens = math.min(burst, tokens + (now - last) * rate / 1000)
local granted = 0
if tokens >= n then tokens = tokens - n; granted = n end
redis.call('HSET', KEYS[1], 't', tokens, 'ts', now)
redis.call('PEXPIRE', KEYS[1], 60000)
return granted -- 20 when tokens were available, 0 when the service's quota is used up

Here the caller passes the current time in milliseconds; in production, using Redis TIME inside the script avoids clock differences between pods. Libraries such as Bucket4j with a Redis backend implement the same idea if we would rather not maintain the script.

3Slowing the Consumer Instead of Failing Records

The key decision is what a pod does when it gets no token. Failing the record, or sending it to a retry topic, turns a capacity problem into an error-handling problem. Instead, we use Kafka's pull model: the pod stops taking new records and lets them wait in the topic as lag. In Spring Kafka, the listener pauses its container, or the partitions it is working on, through the KafkaListenerEndpointRegistry. The container keeps polling, so it stays in the group without a rebalance, but receives no records. When tokens are available again, it resumes from the same offset.

The record being processed when the token ran out is not failed either. The listener waits for a token for a short time, or throws an exception that the error handler treats as a back-off: Spring Kafka's DefaultErrorHandler seeks back to that record and retries it after the back-off interval, so it is processed again in order without leaving the partition. During a peak, lag grows in Kafka, and after the peak the services drain it at their quota rate. No record is lost, and the partner sees a steady 2,000 per second at most.

Step throughOne record, with the shared limit
  1. Record polled
  2. Token from the shared bucket?
    YesCall the partnerNoWait briefly, then pause the container
  3. Partner returns 429?
    YesBack off per Retry-After, seek backNoCommit the offset
  4. Resume when tokens returnlag drains at the quota rate

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

4Respecting the Partner's Signals

Even with a shared bucket, the partner may return 429 occasionally, because its own clock and window differ from ours or because its capacity changed. When it sends a Retry-After header, we honour it: no calls from that pod, or ideally from that service, until the time has passed. Otherwise we use exponential back-off with jitter, for example 200 ms, 400 ms, 800 ms with a random factor, so pods do not all retry at the same instant.

Rate-limited records must not go to retry topics. A retry topic delays them by a fixed time and then releases them all together, which recreates the peak a few minutes later and adds the load of producing and consuming them again. A 429 is a signal to slow down, not a sign that the record is bad. Retry and dead-letter topics remain the right place for records that fail for their own reasons, such as invalid data.

5Priority for Urgent Work

Not all calls are equal. A refund that a customer is waiting for matters more than a nightly catalogue sync. If urgent and bulk work share a topic and a quota, a bulk backlog can delay urgent records for hours. So we put urgent work in its own topic or consumer group with a reserved share of the quota, for example 300 of the 2,000 per second always kept for urgent calls, with bulk services allowed to use the rest. Then a backlog in catalogue sync can never starve refunds.

Example quota split
ConsumerQuota per secondMay fall behind at peak?
Urgent refunds300 (reserved)No, alert after 1 minute
Order confirmations1,000Up to 15 minutes
Refund batch300Up to 1 hour
Catalogue sync400Hours, drains overnight
Total2,000Matches the partner limit

6Monitoring and Agreements

Per service, we track tokens used against quota, the 429 rate (which should be near zero once the shared bucket is in place), consumer lag and time to drain, which is lag divided by the quota rate. Time to drain tells the business what the numbers mean: catalogue sync is 40 minutes behind and will catch up by 21:00.

Finally, agree with product owners in advance which flows may fall behind during peaks and by how much, so a growing lag in catalogue sync is an expected state rather than an incident, and alerts fire only when a flow passes its agreed limit. If the agreed limits cannot be met, the data supports asking the partner for a higher limit.

7Follow-up questions

What happens if Redis is unavailable?

The pods should fall back to a conservative local limit, such as their service's quota divided by the maximum pod count, rather than calling without limit. The total then stays under 2,000 even if it is below what we could use.

Why not scale the pods down instead of pausing?

Scaling changes group membership and causes rebalances, and it reacts in minutes. Pausing reacts in milliseconds and keeps assignments stable. The shared limit, not the pod count, should decide the call rate.

Can the partner limit be enforced in a gateway instead?

Yes, an egress gateway or proxy in front of the partner can hold the shared bucket. It still needs to tell callers to wait rather than fail, and the consumers still need to pause, otherwise they just move the queue into the gateway.

8Conclusion

In this answer, we saw that independent services retrying 429s create a retry storm against the partner's 2,000 per second limit. We moved the limit to where calls start, with a shared token bucket in Redis and per-service quotas, and turned an empty bucket or a 429 into backpressure: pause the container, keep polling, and let lag absorb the peak. We honoured Retry-After with jittered back-off, kept rate-limited records out of retry topics, reserved quota for urgent work, and monitored tokens, 429s, lag and time to drain against agreements with product owners.

What to say in the interviewAbout 90 seconds aloud

Open with

The services are each behaving sensibly on their own, but nobody owns the shared limit, so together they exceed 2,000 a second, and their retries add load at exactly the wrong moment.

Then, if they want more

I would rate-limit where the calls start: a shared token bucket, for example in Redis with an atomic Lua script or a small rate-limiting service, with per-service quotas that add up to 2,000, and every pod takes a token before calling.

When there is no token, or the partner returns 429, the consumer should slow down rather than fail the record: pause the listener container or the partitions through the registry, keep polling so it stays in the group, and resume when tokens come back.

The record in hand is retried in place after a back-off, so nothing is lost and lag absorbs the peak. I would honour Retry-After and use exponential back-off with jitter, and I would not send rate-limited records to retry topics, because they would come back together and recreate the peak.

For priority, urgent work like customer refunds gets its own topic or group with a reserved part of the quota, so a bulk backlog cannot starve it. And I would monitor tokens used, the 429 rate, lag and time to drain per service, and agree with product owners which flows may fall behind during peaks.

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 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
Apache Kafka 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 "During peaks the partner rejects 30 percent of calls, and the services' retries make it...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/12f81cc714/