Sub topic 2
Standard and FIFO Queues Compared
Assume you know basic AWS, IAM, JSON, and Python. The examples use boto3.
| Queue type | Ordering | Delivery | Deduplication |
|---|---|---|---|
| Standard | Best effort | At least once | Consumer responsibility |
| FIFO | Ordered within each message group | Exactly-once processing intent | Queue-supported |
A standard queue provides high throughput and may deliver duplicates or messages out of order. A FIFO queue preserves order within each MessageGroupId, but usually has stricter throughput and configuration requirements.
Duplicates and Idempotent Consumers
At-least-once delivery means a consumer can receive the same message more than once. An idempotent consumer produces the same final result when it processes a message again.
Use a stable event ID or business key. Store that key with the result in a database. Before applying the operation, check whether the key was already processed. Delete the SQS message only after the operation succeeds.
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.get_queue_url(QueueName="orders")["QueueUrl"]
response = sqs.receive_message(
QueueUrl=queue_url,
MaxNumberOfMessages=10,
WaitTimeSeconds=20,
)
for message in response.get("Messages", []):
# Apply the operation with an idempotency key here.
print(message["Body"])
sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=message["ReceiptHandle"],
)FIFO Groups and Deduplication IDs
FIFO ordering applies separately to each message group. Messages for customer-42 stay ordered, while another group can proceed independently. A deduplication ID lets SQS discard repeated sends within its deduplication window.
sqs.send_message(
QueueUrl="https://sqs.us-east-1.amazonaws.com/123456789012/payments.fifo",
MessageBody='{"paymentId":"p-1"}',
MessageGroupId="customer-42",
MessageDeduplicationId="payment-p-1",
)Dead-Letter Queues and Redrive
A dead-letter queue (DLQ) stores messages that exceed a configured maxReceiveCount. A redrive policy connects the main queue to the DLQ. Inspect or repair failed messages instead of retrying them forever. Set the visibility timeout long enough for normal processing, but keep failures recoverable.
SNS Topics and Subscriptions
Amazon SNS is a publish-subscribe service. A publisher sends one message to a topic. SNS delivers it to subscribed endpoints, including SQS queues, Lambda functions, HTTP or HTTPS endpoints, email, and SMS. This supports fan-out, where multiple consumers receive the same event.
Letting SNS Write to SQS
An SQS queue policy must allow the SNS service principal to call sqs:SendMessage. Restrict the permission with the topic ARN, so another topic cannot write to the queue.
Exercise
Where should a consumer handle duplicate standard-queue messages?
Show answer
At the application boundary, using an idempotency key and durable record of processed operations.