Visibility Timeout, DLQ, and Long Polling
Set visibility timeouts so messages are not processed twice, route failed messages to a dead-letter queue, and reduce costs with long polling.
Visibility Timeout, DLQ, and Long Polling is a free AWS Solutions Architect lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Visibility Timeout Mechanism
When a consumer receives a message from SQS, the message is hidden from all other consumers for a period called the visibility timeout. During this window, the consumer processes the message and then deletes it. If the consumer crashes or doesn't finish in time, the visibility timeout expires and the message becomes visible again, allowing another consumer to pick it up. This is the core mechanism behind SQS's at-least-once delivery guarantee.
Configuring Visibility Timeout
The default visibility timeout is 30 seconds. It can be set from 0 seconds to 12 hours. Set it to comfortably exceed your maximum expected processing time—if processing takes up to 2 minutes, set the timeout to at least 3-4 minutes. You can also change the timeout per-receipt using change-message-visibility, which is useful when a consumer detects it needs more time to finish processing a specific message.
# Extend visibility timeout for a specific message
aws sqs change-message-visibility \
--queue-url 'https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue' \
--receipt-handle 'AQEBwJnKyrHigUMZj...' \
--visibility-timeout 300Too Short vs Too Long Timeout
Setting the timeout too short causes messages to reappear before the consumer finishes, leading to duplicate processing. Setting it too long delays another consumer from picking up a message if the original consumer fails silently (e.g., EC2 instance crash without graceful cleanup). The optimal timeout is: slightly longer than your 99th-percentile processing time but short enough to recover quickly from consumer failures. Monitor the ApproximateNumberOfMessagesNotVisible CloudWatch metric to spot timeout issues.
Dead-Letter Queues Explained
A Dead-Letter Queue (DLQ) is a separate SQS queue where messages are sent after they fail processing a configurable number of times (the maxReceiveCount). When a message's receive count exceeds maxReceiveCount, SQS moves it to the DLQ automatically. DLQs prevent poison pill messages (messages that always fail) from blocking the queue indefinitely. Messages in the DLQ can be inspected, debugged, and replayed after fixing the processing bug.
Configuring a Dead-Letter Queue
A DLQ is just a regular SQS queue (Standard for Standard source, FIFO for FIFO source). You configure the Redrive Policy on the source queue to specify which queue is the DLQ and the maxReceiveCount threshold. Ensure the DLQ's retention period is longer than the source queue's retention period—messages arrive at the DLQ late, and you need time to investigate before they expire.
aws sqs set-queue-attributes \
--queue-url 'https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue' \
--attributes '{
"RedrivePolicy": "{\"deadLetterTargetArn\": \"arn:aws:sqs:us-east-1:123456789012:MyDLQ\", \"maxReceiveCount\": \"5\"}"
}'DLQ Monitoring and Alarm Setup
Set a CloudWatch alarm on the DLQ's ApproximateNumberOfMessagesVisible metric. Any message arriving in the DLQ indicates a processing failure that needs attention. Configure the alarm to trigger an SNS notification that pages your on-call engineer immediately. Treat every DLQ message as a bug that needs investigation—DLQ messages should not accumulate silently. After fixing the bug, use DLQ Redrive to send messages back to the source queue for reprocessing.
DLQ Redrive: Replaying Messages
Once you have fixed the bug that caused processing failures, use SQS DLQ Redrive to move messages from the DLQ back to the source queue for reprocessing. The console provides a built-in redrive capability. You can filter by message attributes to replay only specific messages. Alternatively, write a Lambda function to poll the DLQ and forward messages back to the source queue if you need custom filtering logic.
# Start DLQ message move task
aws sqs start-message-move-task \
--source-arn 'arn:aws:sqs:us-east-1:123456789012:MyDLQ' \
--destination-arn 'arn:aws:sqs:us-east-1:123456789012:MyQueue' \
--max-number-of-messages-per-second 5Short Polling vs Long Polling
By default, SQS uses short polling: a receive-message call samples a random subset of servers and returns immediately—even if no messages are available. This results in many empty responses and wastes API calls. Long polling waits up to 20 seconds for a message to arrive before returning an empty response. Long polling reduces costs significantly (fewer API calls) and decreases latency (message is received as soon as it arrives, up to 20s earlier than the next poll cycle).
Enabling Long Polling
Enable long polling at the queue level (applies to all receive calls) or per-request. Queue-level configuration with ReceiveMessageWaitTimeSeconds of 20 is the recommended setting for most applications. When Lambda uses SQS as an event source, it uses long polling automatically. For EC2-based consumers, set WaitTimeSeconds in the receive-message call.
# Enable long polling at queue level (recommended)
aws sqs set-queue-attributes \
--queue-url 'https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue' \
--attributes '{"ReceiveMessageWaitTimeSeconds": "20"}'
# Or per-request
aws sqs receive-message \
--queue-url 'https://...' \
--wait-time-seconds 20 \
--max-number-of-messages 10Message Attributes and Filtering
SQS messages can carry message attributes—metadata key-value pairs separate from the message body. Attributes have a type (String, Number, Binary) and a value. When SQS is subscribed to an SNS topic, you can use SNS subscription filter policies applied to message attributes to route only relevant messages to each queue. Without filtering, every SQS subscriber receives every SNS publication regardless of content.
Delay Queues and Message Timers
A Delay Queue makes every new message invisible for a delay period (0 to 15 minutes) after it is sent. This is useful for workflows where a consumer should not process a message immediately—for example, waiting for a dependent process to complete first. You can also set a per-message delay using DelaySeconds in the send call, which overrides the queue-level delay. Note: delay queues are not available for FIFO queues.
# Create a delay queue (5 minute delay)
aws sqs create-queue \
--queue-name 'DelayedProcessingQueue' \
--attributes '{"DelaySeconds": "300"}'Quick Check
Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.
Lesson Recap
In this lesson you learned: Visibility Timeout hides a message from other consumers during processing, enabling at-least-once delivery with automatic re-delivery if the consumer fails, Dead-Letter Queues capture repeatedly failing messages for debugging and replay after the bug is fixed, and Long Polling (up to 20 seconds wait time) reduces API costs and latency compared to short polling. Next up we explore SNS topics and fan-out architecture.
Frequently asked questions
Is the “Visibility Timeout, DLQ, and Long Polling” lesson free?
Yes — the full text of “Visibility Timeout, DLQ, and Long Polling” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.
What will I learn in “Visibility Timeout, DLQ, and Long Polling”?
Set visibility timeouts so messages are not processed twice, route failed messages to a dead-letter queue, and reduce costs with long polling. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AWS Solutions Architect?
No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Visibility Timeout, DLQ, and Long Polling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AWS Solutions Architect lesson?
Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- SQS Standard vs FIFO Queues
- Visibility Timeout, DLQ, and Long Polling
- SNS Topics and Fan-Out Architecture
- SQS Message Filtering and SNS + SQS Integration