Certopolis

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 ChangeMessageVisibility extends it for one message.
  • Believing ChangeMessageVisibility can 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 maxReceiveCount is 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: StartMessageMoveTask moves them to the source queue by default, with an optional rate cap.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A worker application reads video transcoding jobs from an Amazon SQS queue. Most jobs finish within a minute, but some run for several minutes. The long jobs are picked up and processed a second time by another worker before the first worker finishes them. Which solution will resolve this issue?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: D is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Calling ChangeMessageVisibility extends the timeout for that message while processing continues, so a long job is not handed out a second time.

    Written from docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html

  2. A processing application consumes an Amazon SQS standard queue. A small number of malformed messages fail on every attempt and are received again and again, which blocks throughput and floods the logs. The team must isolate these messages after a few failed attempts so that engineers can inspect them. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: D is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

  3. An Amazon SQS standard queue has a MessageRetentionPeriod of 14 days and a redrive policy that moves a message to a dead-letter queue after five failed receives. The dead-letter queue keeps the default retention period. Support engineers find that failed messages vanish from the dead-letter queue long before they are analysed. Every failed message must stay in the dead-letter queue for seven days. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: D is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A message that spent thirteen days failing in the source queue therefore has one day left even if the dead-letter queue is set to the maximum. The second constraint is that the maximum is a hard ceiling: "The maximum is 1,209,600 seconds (14 days)." The only way to guarantee seven days of shelf life after arrival is to shorten the time a message can spend in the source queue first, so 1 day in the source plus 14 days of dead-letter retention leaves at least thirteen.

    Written from docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html

Ask for something