Spring microservices · senior
Kafka consumers, retries and duplicates
Each partition goes to one consumer in a group. Delivery is at least once, so handlers must be idempotent. Poison messages need a dead-letter plan. Slow processing causes rebalances.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- One group id is one logical consumer. Each partition goes to exactly one consumer in the group. 3 pods with concurrency 6 is 18 threads for 12 partitions, so 6 sit idle.
- Order is kept only within a partition. Use the order id as the key so all events of one order go to one partition.
- Spring Kafka turns off
enable.auto.commitand uses AckMode BATCH: offsets are committed after all records from onepoll()are processed. - At-least-once: a pod stopped during a deploy after processing part of a batch repeats those records. Handlers must be idempotent: a unique constraint on the event or payout id and an insert that skips duplicates.
- Kafka's exactly-once covers read-process-write inside Kafka. It does not cover database writes or SMS.
- Graceful shutdown: make
terminationGracePeriodSecondslonger than the Spring shutdown timeout. - A message that always fails: the default
DefaultErrorHandlerusesFixedBackOff(0, 9): 10 deliveries with no delay, then logs and moves past it. The message is lost. - Do not retry what can never succeed (mark the exception not retryable). Send it to a dead-letter topic. Wrap the deserializer in
ErrorHandlingDeserializerfor poison pills. Temporary failures: exponential backoff, or non-blocking retries with@RetryableTopic. The dead-letter topic needs an owner, an alert and a replay tool. Rebalances: a pod joins or leaves, or a consumer goes longer thanmax.poll.interval.msbetween polls (default 5 minutes). Fix: lowermax.poll.records, speed up processing with batch writes. More pods do not help beyond the partition count.
The red pen
Common traps: mistake → fix
Mistake: More consumers than partitions give more speed
Fix: extra consumers sit idle.
Mistake: Exactly-once covers my database writes
Fix: only within Kafka.
Mistake: A failing message blocks the topic forever
Fix: by default it is retried ten times and then skipped.
Easy to hard
Questions interviewers ask, easy to hard
- How are 12 partitions shared by 3 pods with concurrency 6?
- When are offsets committed?
- What happens to a message that always fails?
- Sellers got two payout messages during a deploy. Why?
- Consumers keep rebalancing during a sale. What do you check?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.