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โ
| Feature | Standard | FIFO |
|---|---|---|
| Throughput | Unlimited | 300 TPS (3,000 with batching) |
| Ordering | Best-effort | Guaranteed (first-in, first-out) |
| Delivery | At-least-once | Exactly-once processing |
| Deduplication | โ | โ (5-minute dedup window) |
| Message Groups | โ | โ (parallel per group) |
| Naming | Any | Must end in .fifo |
- Financial transactions (order matters)
- Sequential processing (step A must complete before step B)
- Deduplication needed (prevent double-charge)
Key Parametersโ
| Parameter | Default | Max | Description |
|---|---|---|---|
| Message retention | 4 days | 14 days | How long messages stay in queue |
| Visibility timeout | 30 sec | 12 hours | Hides message from other consumers while being processed |
| Max message size | 256 KB | 256 KB | Use S3 Extended Client for larger messages |
| Delivery delay | 0 sec | 15 min | Delay before message becomes visible (Delay Queue) |
| Receive wait time | 0 sec | 20 sec | Long 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 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
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โ
| Type | Behavior | Cost |
|---|---|---|
| Short Polling (default) | Returns immediately (even if empty) | More API calls, higher cost |
| Long Polling | Waits up to 20s for a message | Fewer 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.