AWS Certified Developer – Associate
SQS visibility timeout and dead-letter queues for DVA-C03
According to the Amazon SQS Developer Guide, a received message stays in the queue but is hidden from other consumers for the visibility timeout, 30 seconds by default. If it is not deleted in time it becomes visible again. A redrive policy with maxReceiveCount moves a message that keeps failing to a dead-letter queue.
How does the visibility timeout work?
The timeout starts when a message is delivered to a consumer. The consumer is expected to process the message and delete it with DeleteMessage before the timeout ends. If it does not, the message becomes visible again and the same or another consumer can receive it.
The queue default is 30 seconds. A consumer can change the timeout of one message with ChangeMessageVisibility without changing the queue setting. For work whose duration varies, AWS recommends a heartbeat that extends the timeout while processing continues. The limit is 12 hours from the first receive, and extending does not reset it.
Setting VisibilityTimeout to 0 through ChangeMessageVisibility ends the timeout and makes the message available to other consumers at once. A timeout that is too high delays retries, because a failed message stays hidden until it expires.
Messages that are received but not yet deleted are in flight. A standard queue allows about 120,000 in-flight messages. At the limit, short polling returns an OverLimit error, and long polling returns no error and no new messages until the count drops.
How does a message reach a dead-letter queue?
A dead-letter queue (DLQ) is an ordinary queue that you create first and then name in the redrive policy of the source queue. It must be in the same AWS account and Region as the source queue.
The redrive policy sets maxReceiveCount, the number of times a consumer can receive a message before SQS moves it to the DLQ. A value of 1 moves a message after a single failed attempt, so set it high enough to allow real retries.
A redrive allow policy on the DLQ controls which source queues may use it. The default allows all source queues. byQueue lists up to 10 source queue ARNs, and denyAll stops the queue being used as a DLQ.
Retention and redrive from the DLQ
In a standard queue, a message expires based on its original enqueue timestamp, and moving it to a DLQ does not change that timestamp. A message that spent 1 day in the source queue and lands in a DLQ with 4 days of retention is deleted after 3 more days. AWS advises setting the DLQ retention period longer than the source queue's. In a FIFO queue the timestamp resets on the move.
DLQ redrive moves messages back out. StartMessageMoveTask sends them to the source queue by default, or to another queue of the same type. You can set a custom maximum velocity up to 500 messages per second. A redrive task can run for at most 36 hours, and redriven messages count as new messages with a new message ID and enqueue time.
When an SQS queue is a Lambda event source, the Lambda Developer Guide says to configure the dead-letter queue on the SQS queue itself and not on the function.
Where the exam catches people
- Believing long polling or a longer retention period stops a second consumer receiving a slow job: the visibility timeout controls that, and
ChangeMessageVisibilityextends it for one message. - Believing
ChangeMessageVisibilitycan keep a message hidden for as long as needed: the maximum is 12 hours from the first receive, and extending does not reset that limit. - Believing
maxReceiveCountis configured on the dead-letter queue: it is part of the redrive policy on the source queue. - Believing a standard-queue message gets a fresh retention clock in the DLQ: expiry still counts from the original enqueue timestamp. Only FIFO queues reset it.
- Believing redrive needs a custom consumer to copy messages back:
StartMessageMoveTaskmoves them to the source queue by default, with an optional rate cap.