Quick Answer: Amazon SQS uses an at-least-once delivery model. When a worker takes longer to process a job than the queue's configured Visibility Timeout, SQS assumes the worker crashed and makes the message visible to another consumer. To prevent duplicate side effects, you must make your message consumers idempotent and extend message visibility during long-running tasks.
If you have ever had a user get billed twice for a single order or receive two identical confirmation emails, there is a good chance Amazon SQS was doing exactly what it was designed to do.
SQS has a specific behavioral quirk that catches teams off guard: messages can and will be processed multiple times if your workers run too slowly. Here is why that happens under the hood and how to make sure your architecture handles it cleanly.
Why Does Amazon SQS Deliver Duplicate Messages?
Amazon SQS delivers duplicate messages because it operates on an "at-least-once" delivery guarantee, relying on a timer called the Visibility Timeout to detect failed consumers. SQS does not know if a worker is actively processing a job or if it crashed; it only knows whether the message was deleted before the timer expired.
When a worker pulls a message from SQS, that message does not get removed from the queue immediately. Instead, SQS hides the message from every other worker for a set window—by default, 30 seconds. The queue assumes your worker will pick up the payload, finish the job, and send a delete request back to SQS well within that window.
If the worker process dies midway through, this mechanism works as intended: 30 seconds pass, the message becomes visible again, and a fresh worker picks it up to keep your pipeline moving. But if the worker does not die and instead gets bogged down, SQS assumes the worst.
What Happens When Worker Processing Exceeds the Visibility Timeout?
When worker processing exceeds the visibility timeout, SQS automatically returns the in-flight message to the queue while the original worker is still processing it. A second worker then picks up the exact same payload, resulting in concurrent execution on the same job.
Imagine a scenario where a worker picks up an order processing event. Part of its job involves calling a third-party payment gateway. If that payment provider is having an outage or responding with high latency, your worker might sit waiting on an HTTP response for 35 seconds.
Here is how that race condition plays out across your system:
| Timestamp | Worker A Activity | SQS Message State | Worker B Activity |
|---|---|---|---|
| T + 00s | Receives message, starts payment call | Invisible to consumers | Idle / waiting for work |
| T + 30s | Still awaiting payment API response | Visibility Timeout expires; becomes visible | Checks queue, receives message |
| T + 31s | Receives API success, executes charge | Invisible to consumers | Starts duplicate payment call |
| T + 35s | Deletes original message from queue | Deleted | Still running duplicate charge |
Because Worker A took 35 seconds instead of 30, both Worker A and Worker B run the exact same operation simultaneously, leading to duplicated database writes, duplicated payments, or corrupted application state.
How Do You Prevent Duplicate Processing in SQS Workers?
To prevent duplicate processing in SQS workers, you must design your downstream handlers to be strictly idempotent using unique message identifiers. Additionally, long-running tasks should actively heartbeat back to SQS to extend their Visibility Timeout before it expires.
Idempotency means that processing the same event multiple times yields the exact same outcome as processing it once. When a message arrives, check the unique business ID (such as an order_id or payment_id) against a fast, consistent data store like Redis or DynamoDB. If that ID is already flagged as "in-flight" or "completed," your worker can short-circuit, skip the heavy work, and safely delete the message from SQS.
For tasks that legitimately take longer than your visibility window, do not simply crank the queue timeout up to 15 minutes. Instead, have your worker periodically call ChangeMessageVisibility to tell SQS: "I am still alive, give me another 30 seconds." If the worker actually crashes, the message returns to the queue within seconds instead of minutes.
Frequently Asked Questions
Does an SQS FIFO Queue Guarantee Exactly-Once Processing?
No. SQS FIFO queues guarantee exactly-once delivery to the consumer pool, but consumer failures or timeouts can still result in a message being re-dispatched. Your application code must still remain idempotent even when using FIFO queues.
How Long Should You Set the SQS Visibility Timeout?
Set your Visibility Timeout to roughly six times the expected average processing time of your tasks. This buffer gives workers ample headroom for network blips without leaving failed messages trapped in an invisible state for too long.
What Is the Difference Between Message Retention and Visibility Timeout in SQS?
Message retention defines how long an unconsumed message lives in the queue before SQS permanently deletes it (up to 14 days). Visibility timeout is simply the temporary hiding period applied to a message while a worker is currently processing it.
