Skip to main content

Amazon SQS (Simple Queue Service)

Core concept: SQS decouples producers from consumers. If the consumer is down, messages wait in the queue.


Standard vs FIFO Queueโ€‹

FeatureStandardFIFO
ThroughputUnlimited300 TPS (3,000 with batching)
OrderingBest-effortGuaranteed (first-in, first-out)
DeliveryAt-least-onceExactly-once processing
DeduplicationโŒโœ… (5-minute dedup window)
Message GroupsโŒโœ… (parallel per group)
NamingAnyMust end in .fifo
When to use FIFO
  • Financial transactions (order matters)
  • Sequential processing (step A must complete before step B)
  • Deduplication needed (prevent double-charge)

Key Parametersโ€‹

ParameterDefaultMaxDescription
Message retention4 days14 daysHow long messages stay in queue
Visibility timeout30 sec12 hoursHides message from other consumers while being processed
Max message size256 KB256 KBUse S3 Extended Client for larger messages
Delivery delay0 sec15 minDelay before message becomes visible (Delay Queue)
Receive wait time0 sec20 secLong polling duration
Max receive countโ€”โ€”Before moving to DLQ

Visibility Timeoutโ€‹

Producer โ†’ [Message in Queue]
โ”‚
Consumer receives message
Message is INVISIBLE for 30s (default)
โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Processed OK? โ”‚
โ”‚ โœ… Delete message โ”‚
โ”‚ โŒ Timeout โ†’ message reappears โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
caution

If your Lambda/consumer takes longer than visibility timeout, the message becomes visible again and another consumer may process it โ†’ duplicate processing.

Fix: Set visibility timeout > your function's max execution time, or call ChangeMessageVisibility to extend it.


Dead Letter Queue (DLQ)โ€‹

Producer โ†’ [Main Queue]
โ”‚ (fails maxReceiveCount times)
โ–ผ
[Dead Letter Queue]
โ”‚
Monitor via CloudWatch
Alert & debug failed messages
  • DLQ must be the same type as source (Standard DLQ for Standard, FIFO DLQ for FIFO)
  • Use DLQ Redrive to reprocess after fixing bugs

Long Polling vs Short Pollingโ€‹

TypeBehaviorCost
Short Polling (default)Returns immediately (even if empty)More API calls, higher cost
Long PollingWaits up to 20s for a messageFewer API calls, lower latency
// Enable long polling on receive
ReceiveMessageRequest request = ReceiveMessageRequest.builder()
.queueUrl(queueUrl)
.waitTimeSeconds(20) // Long polling
.maxNumberOfMessages(10)
.build();

Lambda Integration (Event Source Mapping)โ€‹

SQS Queue โ†’ Lambda ESM โ†’ Lambda Function
  • Lambda polls SQS (managed by the ESM)
  • Batch size: 1 โ€“ 10,000 messages
  • ReportBatchItemFailures โ€” return failed message IDs to avoid reprocessing successes
public class SqsHandler implements RequestHandler<SQSEvent, SQSBatchResponse> {
public SQSBatchResponse handleRequest(SQSEvent event, Context context) {
List<SQSBatchResponse.BatchItemFailure> failures = new ArrayList<>();

for (SQSEvent.SQSMessage msg : event.getRecords()) {
try {
processMessage(msg.getBody());
} catch (Exception e) {
// Only report this message as failed
failures.add(SQSBatchResponse.BatchItemFailure.builder()
.withItemIdentifier(msg.getMessageId())
.build());
}
}
return SQSBatchResponse.builder().withBatchItemFailures(failures).build();
}
}

๐Ÿงช Practice Questionsโ€‹

Q1. A Lambda function processes SQS messages. The function takes 45 seconds. The visibility timeout is 30 seconds. What will happen?

A) Lambda will timeout and the message will be deleted
B) The message will become visible and may be processed twice
C) Lambda auto-extends the visibility timeout
D) The message goes to the DLQ

โœ… Answer & Explanation

B โ€” When visibility timeout expires before the function finishes, the message becomes visible again and another consumer can pick it up, causing duplicate processing. Set visibility timeout โ‰ฅ Lambda timeout (+ buffer).


Q2. Which SQS feature guarantees that duplicate messages (same content within 5 minutes) are not delivered twice?

A) Standard Queue At-Least-Once delivery
B) FIFO Queue Deduplication
C) Visibility Timeout
D) DLQ

โœ… Answer & Explanation

B โ€” FIFO Queue Deduplication (using MessageDeduplicationId or content-based hashing) prevents duplicate messages within a 5-minute deduplication window.


Q3. A developer wants to reduce API call costs when polling an empty SQS queue. What should they configure?

A) Increase message retention period
B) Enable FIFO
C) Enable Long Polling (set WaitTimeSeconds to up to 20)
D) Reduce batch size

โœ… Answer & Explanation

C โ€” Long polling waits up to 20 seconds for messages, reducing empty-response API calls and cost compared to the default short polling.


๐Ÿ”— Resourcesโ€‹