SQS visibility, retries and DLQs
A queue separates work from the request
A queue stores messages until workers can handle them. In a bookstore, accepting order O-17 can enqueue a packing request instead of waiting for the warehouse. A producer sends the request; a consumer receives it and performs the work. With Amazon SQS, multiple consumers can share one queue. They compete for work rather than each receiving an independent copy. Standard queues allow duplicate deliveries and do not guarantee order. The application must tolerate both.
Send, receive, then delete
SendMessage accepts a message body and optional attributes; ReceiveMessage returns available messages and receipt handles; DeleteMessage uses the latest receipt handle, not the business order ID. Receiving is not deleting. The visibility timeout temporarily hides a received message. If work finishes and the message is deleted, it normally leaves the queue. If the worker crashes or takes too long, the message can become available again. ChangeMessageVisibility can extend the timeout for legitimate longer work, within service limits. Even during visibility, standard delivery can duplicate. A delivery delay instead postpones initial availability; retention limits how long the queue stores messages.
Follow a crash after a successful write
At second 0 a worker receives O-17 with a 30-second visibility timeout. At second 5 it records the packing request in durable storage, but crashes before deletion. At second 30 the message is eligible for another receive. The next worker must detect O-17 was already recorded and delete the new receipt without packing again. Use a unique business operation key and commit the deduplication marker and the local state change atomically, such as a database transaction. A marker stored only in process memory disappears on restart. External side effects require their own idempotency contract or reconciliation; a database transaction alone cannot make a remote shipment atomic.
Bound failures and polling costs
A poison message repeatedly fails, for example because its schema is invalid. Configure a dead-letter queue (DLQ) and a suitable maxReceiveCount to isolate repeated failures. Set DLQ retention long enough for investigation; for standard queues the original enqueue timestamp still matters. Alarm on backlog age and DLQ messages, inspect sanitized evidence, fix the cause, then redrive at a controlled rate. Do not retry forever. Long polling waits for available work and reduces empty receives; batch operations reduce calls but each result can succeed or fail independently. Use bounded workers and check individual batch results. In an illustrative request-count model, 100 small messages sent, received and deleted individually require 300 successful calls; batching ten per call reduces that ideal count to 30. Real charges also depend on payload units, empty polls, retries and region.
Practice: choose the acknowledgment point
For O-18, the database write fails before commit. Should the worker delete the message? No: leave it for a bounded retry. If the write committed but deletion failed, a later delivery must not repeat the committed action. Try these two cases in the offline messaging lab. It models time and local state, not the live SQS service.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SQS visibility, retries and DLQs — knowledge check
Reason about fictional messaging workflows without AWS access.
A worker commits a packing record but crashes before deleting the message. What should the next worker do?
What happens when visibility expires without deletion?
A malformed message fails repeatedly. What is the appropriate next step?
Which setting postpones a new message’s first availability?
In the ideal model, 100 small messages are sent, received and deleted in batches of ten. How many calls?