AWS SQS FIFO Queue Example: A Step-by-Step Guide
Amazon Simple Queue Service (SQS) is a fully managed message queuing service that allows you to decouple and scale microservices, distributed systems, and serverless applications. The FIFO (First-In-First-Out) queue type ensures messages are processed in the exact order they are sent and eliminates duplicate messages. In this article, we’ll explore an example of setting up and using an AWS SQS FIFO queue for reliable message processing.
What Is an SQS FIFO Queue?
An SQS FIFO (First-In-First-Out) queue provides:
- Message Ordering: Ensures messages are processed in the exact order they are sent.
- Message Deduplication: Prevents duplicate message delivery.
- High Throughput: Supports up to 300 messages per second with batching or 3,000 with batching and deduplication.
Common Use Cases
- Processing financial transactions.
- Logging event sequences.
- Workflow orchestration.
AWS SQS FIFO Queue Example
Objective
We’ll create an SQS FIFO queue and demonstrate:
- Sending messages to the queue.
- Receiving and processing messages in order.
Step 1: Create an SQS FIFO Queue
- Navigate to the SQS Console:
- Go to the AWS Management Console → SQS → Create Queue.
- Choose Queue Type:
- Select FIFO.
- Configure Queue Settings:
- Name: Provide a name ending with
.fifo
(e.g.,example-queue.fifo
). - Content-Based Deduplication: Enable to automatically deduplicate messages with the same content.
- Leave other settings as default or configure as needed.
- Name: Provide a name ending with
- Click Create Queue.
Step 2: Send Messages to the Queue
Using AWS Management Console
- Select the queue from the SQS console.
- Click Send and Receive Messages → Send Message.
- Enter the Message Body and Message Group ID (e.g.,
group1
). - Click Send Message.
Using AWS CLI
Send a message via CLI:
aws sqs send-message \
--queue-url https://sqs.<region>.amazonaws.com/<account-id>/example-queue.fifo \
--message-body "Hello from SQS FIFO" \
--message-group-id "group1"
Using Python (Boto3)
Install Boto3 if not already installed:
pip install boto3
Python script to send a message:
import boto3
sqs = boto3.client('sqs', region_name='your-region')
queue_url = 'https://sqs.<region>.amazonaws.com/<account-id>/example-queue.fifo'
response = sqs.send_message(
QueueUrl=queue_url,
MessageBody='Hello from SQS FIFO!',
MessageGroupId='group1'
)
print(f"Message sent! ID: {response['MessageId']}")
Step 3: Receive Messages from the Queue
Using AWS CLI
Receive a message:
aws sqs receive-message \
--queue-url https://sqs.<region>.amazonaws.com/<account-id>/example-queue.fifo \
--max-number-of-messages 1
Using Python (Boto3)
Python script to receive messages:
response = sqs.receive_message(
QueueUrl=queue_url,
MaxNumberOfMessages=1
)
if 'Messages' in response:
for message in response['Messages']:
print(f"Received message: {message['Body']}")
# Delete the message after processing
sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=message['ReceiptHandle']
)
print("Message deleted.")
else:
print("No messages available.")
Step 4: Process Messages
- Use Message Group ID to group related messages and ensure strict ordering within the group.
- Handle duplicate messages by enabling Content-Based Deduplication or using a deduplication ID.
Best Practices for SQS FIFO Queues
- Use Message Group IDs: Organize messages into logical groups to maintain order within each group.
- Enable Deduplication: Prevent duplicate message delivery by enabling content-based deduplication.
- Scale Throughput: Use multiple message groups to increase throughput (up to 3,000 transactions/second with batching).
- Monitor Metrics: Use CloudWatch to monitor queue metrics like message latency and approximate number of messages.
Common Errors and Solutions
- Message Deduplication Disabled:
- Ensure Content-Based Deduplication is enabled if duplicate messages are being processed.
- Throughput Limits:
- If you hit throughput limits, distribute messages across multiple Message Group IDs.
- Message Visibility Timeout:
- Adjust the visibility timeout to ensure messages are processed before they become visible again.
Conclusion
AWS SQS FIFO queues are ideal for use cases requiring strict message ordering and deduplication. By following this example, you can create, send, and process messages efficiently using the FIFO queue type. With its reliability and scalability, SQS FIFO queues are a powerful tool for building robust, decoupled systems.