teluzia.Cloud Engineer · Day 66
CHAPTER 10 · SERVERLESS AND MESSAGING

SQS and asynchronous workers

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

SQS is a shared work tray: workers borrow tickets briefly, acknowledge completed work, and send repeatedly failing tickets to a separate tray. SQS decouples producers from workers. Standard queues provide at-least-once delivery and best-effort ordering, so consumers must handle duplicates. Receiving a message temporarily hides it through the visibility timeout. Deleting the message after successful processing acknowledges completion; merely receiving it does not permanently remove it. A worker should recognize already-completed business work, not assume transport delivers once. Use a stable application key and a durable uniqueness check or conditional write. FIFO queues improve ordering and deduplication semantics but do not make an arbitrary external business transaction magically atomic with queue acknowledgement.

Think of it this way

SQS is a shared work tray: workers borrow tickets briefly, acknowledge completed work, and send repeatedly failing tickets to a separate tray.

TOPIC 01 / 03Step by step

Queue delivery and visibility timeout

SQS decouples producers from workers. Standard queues provide at-least-once delivery and best-effort ordering, so consumers must handle duplicates. Receiving a message temporarily hides it through the visibility timeout. Deleting the message after successful processing acknowledges completion; merely receiving it does not permanently remove it. For example, if work lasts 40 seconds but visibility lasts 10, another worker may receive the same message while the first is still processing.

WORKED EXAMPLE
If work lasts 40 seconds but visibility lasts 10, another worker may receive the same message while the first is still processing.

A producer sends a thumbnail job to SQS and receives confirmation that the message was accepted. A worker polls the queue, receives the message body plus a receipt handle, and begins processing. During the visibility timeout, the message is temporarily hidden from ordinary receives. After storing the thumbnail durably, the worker deletes the message using that receipt handle.

Visibility timeout is a lease-like processing window, not permanent ownership. A receipt handle identifies a particular receive attempt; it is not the stable business ID inside the message. If the worker crashes without deletion, the message can become available again. Standard queues can also deliver duplicates, so an apparently generous timeout does not establish exactly-once application effects. Long polling reduces empty receive calls compared with repeatedly polling immediately. Set visibility from realistic processing duration, extend it for valid long jobs where necessary, and retain a maximum recovery bound so failed workers do not hide useful work forever.

Why this matters

A correctly sized visibility window prevents avoidable concurrent duplicates while preserving eventual recovery when a worker crashes before it can acknowledge successful work.

A common trap

Deleting a message before its durable side effect finishes can lose work permanently; receiving a message should never be mistaken for completing it.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Queue delivery and visibility timeout

SQS decouples producers from workers. Standard queues provide at-least-once delivery and best-effort ordering, so consumers must handle duplicates. Receiving a message temporarily hides it through the visibility timeout. Deleting the message after successful processing acknowledges completion; merely receiving it does not permanently remove it. For example, if work lasts 40 seconds but visibility lasts 10, another worker may receive the same message while the first is still processing.

WORKED EXAMPLE
If work lasts 40 seconds but visibility lasts 10, another worker may receive the same message while the first is still processing.

A producer sends a thumbnail job to SQS and receives confirmation that the message was accepted. A worker polls the queue, receives the message body plus a receipt handle, and begins processing. During the visibility timeout, the message is temporarily hidden from ordinary receives. After storing the thumbnail durably, the worker deletes the message using that receipt handle.

Visibility timeout is a lease-like processing window, not permanent ownership. A receipt handle identifies a particular receive attempt; it is not the stable business ID inside the message. If the worker crashes without deletion, the message can become available again. Standard queues can also deliver duplicates, so an apparently generous timeout does not establish exactly-once application effects. Long polling reduces empty receive calls compared with repeatedly polling immediately. Set visibility from realistic processing duration, extend it for valid long jobs where necessary, and retain a maximum recovery bound so failed workers do not hide useful work forever.

Why this matters

A correctly sized visibility window prevents avoidable concurrent duplicates while preserving eventual recovery when a worker crashes before it can acknowledge successful work.

A common trap

Deleting a message before its durable side effect finishes can lose work permanently; receiving a message should never be mistaken for completing it.

Idempotent asynchronous workers

A worker should recognize already-completed business work, not assume transport delivers once. Use a stable application key and a durable uniqueness check or conditional write. FIFO queues improve ordering and deduplication semantics but do not make an arbitrary external business transaction magically atomic with queue acknowledgement. For example, order ID o-17 is the deduplication key; a second delivery finds its completed record and avoids generating another invoice.

WORKED EXAMPLE
Order ID o-17 is the deduplication key; a second delivery finds its completed record and avoids generating another invoice.

Two workers receive separate deliveries of the same fulfillment request. Both check whether shipment s-204 is complete; both initially see no record. A simple read-then-write check is insufficient because the checks race. Each worker must attempt an atomic claim, such as a conditional database write that succeeds only when the operation key is absent or in an allowed state.

The idempotency key identifies the business operation across transport attempts. A conditional write performs a change only if its stated condition still holds atomically. A completed record can let later deliveries return the existing result. However, a worker might claim processing and crash before completing it. Design expiration or recovery for abandoned claims rather than treating every existing record as successful work. External effects require particular care: recording completion before sending a payment can lose work, while recording it afterward can allow duplicate payment unless the payment provider also accepts a stable idempotency key.

Why this matters

Application-level duplicate handling protects customers and data even when a retry occurs after the effect succeeds but before the message acknowledgement reaches the queue.

A common trap

Using a freshly generated random key for every delivery defeats duplicate detection because retries of the same business event appear to be unrelated jobs.

Dead-letter queues and batch failures

A dead-letter queue holds messages that repeatedly fail under the configured redrive policy. It is a diagnostic boundary, not automatic business recovery. Lambda SQS integrations can report failed batch items so successful items need not be retried with the entire batch. Review retention, retry counts, and alerting together. For example, a batch has jobs A, B, C; B is malformed. Partial batch failure reports B’s message ID while A and C remain completed.

WORKED EXAMPLE
A batch has jobs A, B, C; B is malformed. Partial batch failure reports B’s message ID while A and C remain completed.

A worker receives ten messages. Nine contain valid image references; the tenth contains an unsupported format and fails every attempt. If the consumer reports the entire batch as failed, all ten can be retried, wasting successful work. With a correctly implemented partial batch response, only the failing item's identifier is reported for retry, subject to the integration's configured behavior.

A batch is one delivery containing multiple messages. A poison message repeatedly fails because its content or required condition cannot currently be processed. A dead-letter queue receives messages after the configured redrive threshold, helping separate repeated failure from the normal backlog. It still requires monitoring and an owner. Inspect why the item failed, fix code or data, then redrive deliberately. Moving messages back blindly can recreate the same loop. Retention choices matter because time spent in the source queue and dead-letter queue affects how long diagnostic evidence and recoverable work remain available.

Why this matters

Isolating poison messages and acknowledging successful batch items reduces wasted processing and gives operators a clear recovery workflow with evidence of the failing input.

A common trap

Redriving every dead-letter message without fixing its cause creates a failure loop, repeated load, and possibly more duplicate effects for downstream systems.

KEEP THESE WITH YOU

Today’s takeaways

Queue delivery and visibility timeout: SQS decouples producers from workers. Standard queues provide at-least-once delivery and best-effort ordering, so consumers must handle duplicates. Receiving a message temporarily hides it through the visibility timeout. Deleting the message after successful processing acknowledges completion; merely receiving it does not permanently remove it.

Idempotent asynchronous workers: A worker should recognize already-completed business work, not assume transport delivers once. Use a stable application key and a durable uniqueness check or conditional write. FIFO queues improve ordering and deduplication semantics but do not make an arbitrary external business transaction magically atomic with queue acknowledgement.

Dead-letter queues and batch failures: A dead-letter queue holds messages that repeatedly fail under the configured redrive policy. It is a diagnostic boundary, not automatic business recovery. Lambda SQS integrations can report failed batch items so successful items need not be retried with the entire batch. Review retention, retry counts, and alerting together.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed