INFRASTRUCTURE LEARNINGINFRASTRUCTURE LEARNING
インフラ大全インフラ大全IT INFRASTRUCTURE / LEARNING
Infrastructure learning guide
Find a lesson or lab/

Esc to close · Search terms stay on your device

LEARNING日本語

Topic lessonTopic: SQS: redelivery, retention and recovery

SQS visibility timeout: redelivery, deletion and processing time

An SQS visibility timeout temporarily hides a received message from other consumers. It does not stop the worker or report that the work succeeded. Follow receipt, application work and deletion separately to understand how the same job can arrive again.

What you will learn

Read fixed delivery timelines, distinguish the visibility deadline from work completion and deletion, and explain what to check when a deadline expires or a job repeats.

Prerequisite

A queue holds requests for work; a consumer receives and processes them. The first section explains these roles. All timings are fixed teaching examples. You do not enter an AWS account, message body or credentials, create a queue, or call an API.

Receipt, application work and deletion are separate events

Amazon SQS (Simple Queue Service) provides message queues. A producer sends a request, and a consumer receives it and does the work. Receiving through ReceiveMessage and removing a message through DeleteMessage are separate actions.

Consider a request to save an image. Distinguish saving the image, recording the result, and deleting the message from the queue. A record that the application finished its work does not prove that deletion also succeeded.

Separate what each event tells you
EventWhat it tells youWhat it does not establish
ReceiptThe consumer received the message; its visibility interval starts.Whether the application work has finished.
Work completionCheck the intended result in the application and its storage.Whether the message has been deleted from the queue.
DeletionA deletion action concerns the corresponding receipt.That a standard queue can never deliver a copy again.

Compare three examples with a 30-second visibility deadline

In these examples, the first receipt is at 0 seconds and the visibility deadline is at 30 seconds. The calculation excludes network delays, retention expiry and moves to a DLQ. The second receipt times are chosen to explain the sequence; they do not predict actual delivery timing.

Compare work duration and the visibility interval in three fixed examples

Fixed teaching examples / not predictions of delivery timing or processing results

A: delete after completing the work

A: delete after completing the work — Read with the event list below020253060sA: work20Not visible25
  1. 0 sConsumer A receives
  2. 20 sApplication work finishes
  3. 25 sDeletion completes

This example deletes before the deadline, so it does not proceed to redelivery caused by the 30-second timeout.

This does not guarantee that a standard queue will never deliver another copy.

B: finish the work but do not delete

B: finish the work but do not delete — Read with the event list below020303560sA: work20Not visible30
  1. 0 sConsumer A receives
  2. 20 sApplication work finishes
  3. 30 sAvailable for receipt again
  4. 35 sExample second receipt by B

The work may be done, but an undeleted message can become available again.

Handle the repeated request without unintentionally creating the result twice.

C: the deadline expires while work continues

C: the deadline expires while work continues — Read with the event list below030354560sA: work45Not visible30
  1. 0 sConsumer A receives
  2. 30 sDeadline expires while A works
  3. 35 sExample second receipt by B
  4. 45 sA finishes its application work

Expiry does not stop A. Another receipt can overlap the original application work.

A longer visibility timeout alone cannot make an external write or notification happen exactly once.

Both B and C become available again at 30 seconds. The difference is whether the original work has finished. B illustrates repeating work that already succeeded; C illustrates overlapping workers. In both cases, queue state alone cannot tell you the application result.

When changing a duration, check when the clock starts

Allow time for processing and deletion. Too short an interval can expose a message while work continues; too long an interval can delay another attempt at unfinished work. When processing time varies, the consumer can adjust visibility while it works.

A fixed example: the initial interval and a later change are different
TimeAction in this exampleResulting deadline
0 secondsReceive with a 60-second visibility interval.Initially, at 60 seconds.
15 secondsChange the duration for this receipt to 10 seconds.10 seconds from the change: at 25 seconds.
Next receiptDo not assume the previous 10-second override persists.Check the queue setting or override that applies to that receipt.

ChangeMessageVisibility counts the new duration from the change. A per-message override is not a permanent setting for subsequent receipts. Extending visibility also does not restart the overall 12-hour bound measured from the first receipt.

DeleteMessage uses a ReceiptHandle returned on receipt, rather than the MessageId. A repeated receipt has a different handle, so use the latest corresponding handle. A deletion request using an old handle can succeed without deleting the message. Do not paste a real handle or run an API call for this lesson.

Follow external effects when considering redelivery

Idempotent handling means that repeating the same request does not produce unintended extra effects. A standard queue can deliver a stored copy even after deletion. Adjusting a visibility deadline and handling the application effects of duplicate requests are separate concerns.

Image-saving example: questions to answer before resuming
Observed recordWhat to checkDo not assume
The worker stopped just after saving the image.How the saved result relates to the record marking the request complete.No completion record means no image was saved.
The same work request arrives again.Whether you can identify the same business request and find its existing result.A different ReceiptHandle means a different job.
The visibility interval was increased.Time required for work and deletion, and the delay before recovery after failure.A longer interval removes the need to handle duplicate writes.

With Lambda, identify who receives and retries the work

A Lambda event source mapping polls SQS and invokes the function synchronously with a batch of messages. When the batch succeeds, Lambda deletes its messages. This assigns receipt and deletion differently from an application that calls SQS directly.

By default, an error processing a batch makes its messages visible again after the visibility timeout, including items whose work already succeeded. Partial batch responses let the function report individual failures to avoid retrying successful items. Check both the mapping configuration and the function response.

Identify the unit behind each kind of retry
SituationUnit to distinguish
SQS redeliveryReceive an undeleted message again.
Lambda asynchronous invocationRetry an asynchronous function invocation; its conditions differ from SQS event source mapping.
Step Functions RetryRetry the relevant workflow state. It does not share a counter with the SQS visibility setting.

Investigate in order and plan before changing anything

  1. Identify direct application receipt versus a Lambda event source mapping.
  2. Correlate receipt, application results and deletion for the same request.
  3. Check whether the original work was still running when visibility expired.
  4. Inspect how repeated requests affect external writes, notifications and updates.
  5. Before changing timing or retry behavior, identify the owner, impact, verification method and recovery conditions.

A request that repeatedly cannot complete may need isolation in another queue so its cause can be investigated. That is the role of a DLQ, covered next. A visibility timeout alone does not establish how many application failures occurred or exactly when a message will move to a DLQ.

Change the time or actor and predict the next step

Choose an option to see the result and explanation.

A message is received at 0 seconds with 30-second visibility. Work finishes at 20 seconds, but it has not been deleted when 30 seconds arrives. How do you read this fixed example?
Read the answer and reason

Answer: The work is done, but the message becomes available again. · Work completion and deletion are separate. An undeleted message becomes available for receipt again after the interval.

Receive with a 60-second interval. At 15 seconds, change visibility to 10 seconds. Where is the new deadline in this example?
Read the answer and reason

Answer: 25 seconds after the initial receipt. · Count 10 seconds from the change at 15 seconds: the deadline is at 25 seconds. Do not add 10 to the original deadline.

An image-saving request arrives again with a different ReceiptHandle. Is that enough to treat it as a new job and save again?
Read the answer and reason

Answer: Correlate the business request with any saved result. · ReceiptHandle identifies a receipt. Separately identify the business request and check whether its result already exists.

A Lambda function receives an SQS batch of three messages; one item fails. With the default behavior and no partial batch response, which work must you consider for retry?
Read the answer and reason

Answer: All three, including the successful items. · By default, the batch error makes all its messages visible after the timeout. Account for effects of the items that already succeeded.

Check, then explain in your own words

  1. Separate receipt, application work and deletion.
  2. At visibility expiry, check whether the original work is still running.
  3. Distinguish deadline adjustments from handling duplicate application effects.
  4. Identify the responsible actor and failure unit for direct receipt versus Lambda batches.
Check your understanding: If you extend visibility until the work finishes, can you rule out the same job running twice?

No. Check deadlines and deletion, and handle the application effects of repeated requests.

Visibility is a temporary delivery state. Standard queues do not guarantee exactly-once delivery. Also distinguish how far writes or notifications have progressed.

Sources and conditions checked

The official references used in this lesson are collected here with their scope, conditions and corresponding sections. Independent expert review and live AWS comparison have not been performed.