Three ways a message comes back#
A received message is not removed from the queue. SQS hides it for the visibility timeout, 30 seconds by default, and expects the consumer to delete it within that time. If nobody deletes it, it becomes visible again and the same or another consumer receives it. Visibility timeout
That gives three nearby causes for a charge sent twice or an order email repeated:
- Processing outlasts the timeout. The first worker is still working when the message reappears, and a second worker receives it. Both finish.
- The consumer fails before the delete. The worker completes a side effect, such as calling a payments API, then crashes, times out or throws before
DeleteMessage. After the timeout the message is delivered again and the side effect repeats. - At-least-once delivery. SQS keeps copies of each message on several servers. If one is unavailable during a receive or delete, its copy is not deleted and can be delivered again. AWS asks consumers of standard queues to be idempotent for this reason. At-least-once delivery · DeleteMessage
Lambda adds a fourth case. By default, when the function returns an error for a batch, every message in that batch returns to the queue after the visibility timeout, including the ones the function handled successfully. Partial batch responses report only the failed items. Lambda SQS error handling
Why they look alike#
From the side effect's point of view, all of them are "the same message, processed twice". The queue's metrics do not help much: NumberOfMessagesReceived counts every redelivery, but it cannot say whether the first attempt was still running or had died. SQS metrics
The first worker's own log can mislead too. When its processing outlasted the timeout, its delete uses a receipt handle from an earlier receive. AWS documents that a delete with an old receipt handle can succeed without deleting the message, so a logged successful delete does not rule out a duplicate. DeleteMessage
The evidence that separates them#
Each delivery carries system attributes the consumer can log: ApproximateReceiveCount (how many times the message has been received but not deleted), ApproximateFirstReceiveTimestamp and SentTimestamp, requested with MessageSystemAttributeNames on the receive. If your consumer logs them along with MessageId, you have everything you need. ReceiveMessage
Read the queue's timeout without touching any message: get-queue-attributes
aws sqs get-queue-attributes \
--profile PROFILE --region REGION --queue-url QUEUE_URL \
--attribute-names VisibilityTimeout RedrivePolicy \
--output json --no-cli-pager
A consumer can override the queue's timeout per receive with VisibilityTimeout, or extend it per message with ChangeMessageVisibility, so check the consumer's code or SDK settings as well. For a Lambda consumer, compare the function's Timeout with the queue's: the function timeout must not exceed the visibility timeout, and AWS recommends a visibility timeout of at least six times the function timeout. Lambda checks this when the event source mapping is created or updated, but not when the function timeout changes later. get-function-configuration · Configuring a queue for Lambda
aws lambda get-function-configuration \
--profile PROFILE --region REGION --function-name FUNCTION \
--query '{timeout:Timeout,lastModified:LastModified}' --output json --no-cli-pager
Then find both attempts in the consumer's logs. In CloudWatch Logs Insights, assuming the consumer logs messageId, receiveCount and an event field:
fields @timestamp, @logStream, messageId, receiveCount, event
| filter messageId = "MESSAGE_ID"
| sort @timestamp asc
Do not call aws sqs receive-message to inspect the message. A receive changes state: the message becomes invisible to your consumers for the visibility timeout, and its receive count goes up by one, which moves it toward the dead-letter queue. ReceiveMessage
Read the timeline#
| First attempt | Second attempt | Points to |
|---|---|---|
| Still running when the second starts | Receive count 2, about one timeout after the first receive | Processing outlasts the timeout |
| Logs an error or stops with no completion line | Receive count 2, about one timeout later | Consumer failed before the delete |
| Completed and deleted well inside the timeout | Same MessageId, later | A redelivered copy |
Different MessageId, same payload | Receive count 1 | The producer sent it twice |
The last row is not an SQS redelivery at all: each send gets its own MessageId, so two IDs mean two sends. Look at the producer's retries.
A worked example: two duplicate charges#
This example is illustrative. The fictional payments worker consumes the queue payments-capture in account 111122223333. The queue's VisibilityTimeout is 30 seconds. Both incidents start with a customer charged twice for one order.
In the first incident, the worker's log for message 5f1c... reads:
| Time (UTC) | Task | Receive count | Event |
|---|---|---|---|
| 14:02:00 | task A | 1 | received |
| 14:02:31 | task B | 2 | received |
| 14:02:41 | task A | 1 | capture succeeded, deleted |
| 14:02:44 | task B | 2 | capture succeeded, deleted |
The payment provider's API was answering in about 40 seconds from 14:00. Task A was still waiting when the message reappeared 31 seconds after its receive. The timeout is too short for the provider's current latency, and the worker does not extend it. The fix is on the consumer: extend visibility with a heartbeat while waiting, and make the capture idempotent by order ID. Why the provider slowed down is a separate question: is the latency the client's or the server's?
In the second incident:
| Time (UTC) | Task | Receive count | Event |
|---|---|---|---|
| 14:02:00 | task A | 1 | received |
| 14:02:02 | task A | 1 | capture succeeded |
| (no line) | task A | task stopped, exit code 137 | |
| 14:02:31 | task C | 2 | received, capture succeeded, deleted |
Task A captured the payment in two seconds and was killed before it deleted the message. The timeout was long enough. The cause is the task kill and the gap between the side effect and the delete; a longer timeout would only delay the duplicate.
Limits#
ApproximateReceiveCount is approximate, and AWS defines it as the number of receives across all queues, not only this one. FIFO queues prevent duplicates from producer retries within a five-minute deduplication interval, but a message whose visibility expires is still delivered again. FIFO exactly-once processing
Without message IDs and receive counts in the consumer's logs, you can only infer the case from timing. Add them before the next incident. If the same messages keep coming back without ever succeeding, the problem is no longer duplicates: see why SQS messages pile up and a dead-letter queue that is filling. Other dependency failures are in diagnosing runtime dependency failures.
Questions#
Does raising the visibility timeout stop duplicate processing?
Only for the first case, where processing outlasts the timeout. A consumer that crashes after its side effect still produces a duplicate, and at-least-once delivery still applies. Idempotent processing covers all three.
What is the maximum SQS visibility timeout?
Twelve hours from the first receive. Extending visibility with ChangeMessageVisibility does not reset that limit. Visibility timeout
Why does my Lambda function reprocess messages that succeeded?
By default a failed batch returns every message in it to the queue, including the ones that were processed. Report failures per item with partial batch responses, and keep the handler idempotent.