Quick revision
The short answer
When a message takes 90 seconds but the visibility timeout is 30, the message becomes visible again at 30 seconds and another consumer starts on it, so it is processed twice. Each of those extra receives counts toward maxReceiveCount, so after three receives a slow but healthy message moves to the dead-letter queue even though the first attempt succeeded. We fix it by setting the visibility timeout above the worst-case processing time or extending it with ChangeMessageVisibility as a heartbeat, making processing idempotent, using smaller batches and alerting on dead-letter queue depth.
What to remember
In this answer, we replayed one slow message: every time the 30-second visibility timeout expired, another consumer received it, which caused duplicate processing and raised the receive count until the fourth attempt moved it to the dead-letter queue despite eventual success. The fixes are a visibility timeout above the worst case or a ChangeMessageVisibility heartbeat, idempotent processing with a dedup key, smaller batches so one slow message does not delay the rest, and an alert on DLQ depth.
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.
When a message takes 90 seconds but the visibility timeout is 30, the message becomes visible again at 30 seconds and another consumer starts on it, so it is processed twice. Each of those extra receives counts toward maxReceiveCount, so after three receives a slow but healthy message moves to the dead-letter queue even though the first attempt succeeded. We fix it by setting the visibility timeout above the worst-case processing time or extending it with ChangeMessageVisibility as a heartbeat, making processing idempotent, using smaller batches and alerting on dead-letter queue depth.
Why this question is asked
This bug is a classic because both symptoms look unrelated. Duplicates suggest a broken producer, and messages in the dead-letter queue suggest failures, yet the logs show success. The cause is one setting that does not match the real processing time. Interviewers like it because it tests whether we understand how SQS delivery actually works rather than treating it like a broker that pushes messages. In this answer, we will recap the two settings involved, replay the timeline of one slow message second by second, explain why it lands in the dead-letter queue, and then go through the fixes, from the quick one to the robust ones.
1A Quick Recap of the Two Settings
The visibility timeout is how long SQS hides a message after a consumer receives it. During that time no other consumer can receive it. If the consumer deletes the message in time, it is gone; if not, it becomes visible again. The setting is on the queue, here 30 seconds, and can be overridden per receive.
maxReceiveCount is part of the redrive policy. SQS counts how many times a message has been received, in the ApproximateReceiveCount attribute. When a receive would push that count past maxReceiveCount, here 3, SQS moves the message to the dead-letter queue instead of delivering it. The count goes up on every receive, whether or not the earlier attempt failed. SQS cannot know that a consumer is still busy; it only sees that no delete arrived in time.
2Replaying the Timeline of a 90-Second Message
Let us follow one message that needs 90 seconds, with three consumers polling the same queue.
| Time | What happens | Receive count |
|---|---|---|
| 0 s | Consumer A receives it and starts working | 1 |
| 30 s | Timeout expires; consumer B receives it too | 2 |
| 60 s | Timeout expires; consumer C receives it as well | 3 |
| 90 s | A finishes and deletes with its old receipt handle | 3 |
| 90 s | Timeout expires; next receive exceeds 3 -> moved to DLQ | 4th attempt |
| 120 to 150 s | B and C finish: the work was done three times | - |
Two things go wrong at once. First, B and C process a message A is still handling, so the side effect, such as an email or a charge, happens up to three times. Second, A's delete at 90 seconds uses the receipt handle from the first receive. SQS documents that we should delete with the most recent receipt handle; an older one may not remove the message, so A's success does not reliably stop the cycle. At the next expiry SQS sees a fourth receive, which exceeds maxReceiveCount of 3, and moves the message to the dead-letter queue. That is exactly why the team sees messages in the DLQ although processing eventually succeeded.
Messages that take 10 seconds never hit this, which is why the problem is occasional. Only the slow ones, the 90-second outliers, cross the 30-second line.
3Confirming the Diagnosis
Before changing settings, we confirm. We log the message id, ApproximateReceiveCount and processing duration for every message. Duplicates will show the same message id with receive counts 1, 2 and 3 at roughly 30-second intervals, and processing times above 30 seconds. The DLQ messages will show the same pattern, plus a successful completion log from the first consumer. CloudWatch's ApproximateAgeOfOldestMessage and NumberOfMessagesReceived versus NumberOfMessagesDeleted give the same picture at queue level: many more receives than deletes.
4Fix 1: Match or Extend the Visibility Timeout
The quick fix is to set the visibility timeout above the worst-case processing time with a margin, for example 180 seconds for a 90-second worst case. The cost is slower recovery: if a consumer really crashes, its message waits up to three minutes before another consumer retries it.
The more robust fix is a heartbeat. We start with a moderate timeout, say 60 seconds, and while the work is running, a scheduled task calls ChangeMessageVisibility every 30 seconds to push the deadline out. A crashed consumer stops sending heartbeats, so its message comes back quickly, while a slow but healthy one keeps it. In Spring Cloud AWS we can set the visibility timeout on the listener and inject a Visibility parameter to change it from our code; check whether your version also extends visibility automatically for long-running listeners.
7 messages in order. Play them, or step through them one at a time.
The message never becomes visible while the consumer is alive.
5Fix 2: Idempotency, Smaller Batches and Alerts
Even with the right timeout, a standard queue is at-least-once: a crash after processing but before deleting causes a redelivery. So the handler must be idempotent. A common pattern is a dedup table keyed by the message id, or better by a business key such as the order id plus the event type, inserted in the same database transaction as the work. A second delivery hits the unique constraint and is skipped.
Batch size matters too. If we receive 10 messages and process them one after another, the visibility clock runs for all 10 from the moment of receipt, so one 90-second message pushes the other nine past their timeout. Receiving fewer messages per poll, or processing them in parallel, keeps one slow message from delaying the rest. Finally, we alert on the dead-letter queue's ApproximateNumberOfMessagesVisible above zero, so the next mismatch is caught in minutes, not discovered weeks later.
6Follow-up questions
How would you redrive messages from the dead-letter queue safely?
First we fix the cause, here the timeout, or the messages will fail again. Then we look at a sample of DLQ messages to separate those that were really processed from those that failed, and since our handler is idempotent, reprocessing the already-done ones is harmless. SQS offers a built-in redrive that moves messages back to the source queue, through the console or the StartMessageMoveTask API, with a configurable rate so we do not flood consumers. We move a small batch first, watch errors and duplicates, then move the rest. Messages with bad payloads stay in the DLQ for manual handling.
Why not just raise maxReceiveCount to 10?
It would hide the symptom in the DLQ but make duplicates worse, because the message would be processed up to ten times. The DLQ count is a useful signal here; the real problem is the timeout.
Would a FIFO queue prevent the duplicates?
Not this kind. A FIFO queue deduplicates sends within five minutes, but redeliveries after a visibility timeout still happen, and within one message group the next message waits until the current one is deleted. The timeout must still match the processing time.
7Conclusion
In this answer, we replayed one slow message: every time the 30-second visibility timeout expired, another consumer received it, which caused duplicate processing and raised the receive count until the fourth attempt moved it to the dead-letter queue despite eventual success. The fixes are a visibility timeout above the worst case or a ChangeMessageVisibility heartbeat, idempotent processing with a dedup key, smaller batches so one slow message does not delay the rest, and an alert on DLQ depth.
Open with
The visibility timeout is shorter than the slowest processing. When a consumer receives a message, SQS hides it for 30 seconds. If processing takes 90 seconds, at 30 seconds the message becomes visible again and a second consumer picks it up, and at 60 seconds a third one.
Then, if they want more
That is the double processing. Every one of those receives increases the receive count, and with maxReceiveCount at 3, the fourth receive attempt moves the message to the dead-letter queue, even though the first consumer finished successfully. Its delete also uses the original receipt handle, which may not remove the message once it has been received again.
The 10-second messages never hit this, which is why it is occasional. I would confirm by logging message id, ApproximateReceiveCount and processing time. To fix it, the simple option is a visibility timeout above the worst case, say three minutes, at the cost of slower retries after a crash.
The better option is a heartbeat: start with 60 seconds and call ChangeMessageVisibility every 30 seconds while working, so a live consumer keeps the message and a dead one releases it quickly. Spring Cloud AWS lets me set and change visibility from the listener.
On top of that, the handler must be idempotent, with a dedup table keyed by message id or business key, because standard queues are at-least-once anyway. I would also receive smaller batches so one slow message does not push others past their timeout, and alert when the dead-letter queue is not empty.
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
- AWS 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 "A Java SQS consumer occasionally processes the same message twice, and some messages end...": https://scrivio-interview-library.netlify.app/spring-boot/aws/87c5b5dff8/