Amazon Simple Queue Service (SQS) is a fully managed message queuing service by AWS that allows distributed applications to communicate asynchronously, making it a critical tool for decoupling microservices and improving system resilience. SQS offers two types of queues tailored for different use cases:
-
Standard Queues: These queues are designed for high throughput, allowing an unlimited number of transactions per second while supporting at-least-once message delivery. Although standard queues don’t guarantee strict message ordering, they excel in scenarios where speed and scalability are prioritized over sequence, such as large-scale data processing or background tasks.
-
FIFO (First-In-First-Out) Queues: FIFO queues ensure that messages are delivered in the exact order they’re sent, offering exactly once processing. This strict ordering makes FIFO queues essential for workflows where order accuracy is critical, like financial transactions, order management systems, or coordinated workflow tasks. By preventing duplicates and ensuring order consistency, FIFO queues provide dependable, sequence-driven processing.
In this tutorial, we’ll use an AWS Lambda function to send messages to a standard and a FIFO queue to demonstrate their unique handling of message orders. By manually polling these queues in the SQS dashboard, we’ll observe how they differ in preserving message sequence—information that is crucial when deciding which queue type best suits specific application needs.
Setting Up SQS Queues: FIFO and Standard.
-
Create a FIFO Queue:
- Go to the SQS dashboard in the AWS Console.
- Click on “Create Queue” and select FIFO Queue.
- Name your queue, ensuring the name ends with
.fifo
. - Configure other settings as needed (e.g., enabling content-based deduplication).
- Note the Queue URL as it will be used later in the Lambda function.
- Create a Standard Queue:
-
- In the SQS dashboard, click on “Create Queue” and select Standard Queue.
- Name the queue (no specific naming convention is required).
- Note the Queue URL for this queue as well.
Creating the AWS Lambda Function to Send Messages
Now, we’ll create an AWS Lambda function that:
- Generates a unique message.
- Sends the message to both the FIFO queue (with a
MessageGroupId
to enforce ordering) and the standard queue. - Prints the message details to the Lambda logs for verification.
This setup will allow us to observe message order differences between the two queue types in the SQS dashboard.
Lambda Function Code
1. Set Up a Lambda Function in AWS Console:
-
- Go to the AWS Lambda Console.
- Click on Create Function and choose “Author from scratch.”
- Name the function (e.g.,
SendMessagesToSQS
), and select Python 3.8 or later as the runtime. - Set up permissions allowing Lambda to send messages to SQS queues.
2. Add the Code:Use the following code for the Lambda function, replacing accound_id
, fifo_queue_name
, and standard_queue_name
with the actual values. This code uses boto3
, the AWS SDK for Python, to send messages to both SQS queues.
import boto3 import json # Initialize the SQS client sqs = boto3.client('sqs') # Define queue URLs FIFO_QUEUE_URL = 'https://sqs.<region>.amazonaws.com/<account_id>/<fifo_queue_name>.fifo' STANDARD_QUEUE_URL = 'https://sqs.<region>.amazonaws.com/<account_id>/<standard_queue_name>' def lambda_handler(event, context): # Number of messages to send num_messages = event.get('num_messages', 5) # Default to 5 if not provided in the event message_group_id = "TestGroup" # Same group ID for FIFO to maintain order fifo_responses = [] standard_responses = [] for i in range(1, num_messages + 1): # Sequential message body message_body = f"Test message {i}" # Send message to FIFO queue fifo_response = sqs.send_message( QueueUrl=FIFO_QUEUE_URL, MessageBody=message_body, MessageGroupId=message_group_id ) fifo_responses.append({ 'MessageId': fifo_response['MessageId'], 'MessageBody': message_body, 'MessageGroupId': message_group_id }) # Send message to Standard queue standard_response = sqs.send_message( QueueUrl=STANDARD_QUEUE_URL, MessageBody=message_body ) standard_responses.append({ 'MessageId': standard_response['MessageId'], 'MessageBody': message_body }) # Print all message details print("Messages sent to FIFO Queue:", json.dumps(fifo_responses, indent=2)) print("Messages sent to Standard Queue:", json.dumps(standard_responses, indent=2)) return { 'statusCode': 200, 'body': json.dumps({ 'message': 'Messages sent to FIFO and Standard Queues.', 'fifo_queue_messages': fifo_responses, 'standard_queue_messages': standard_responses }) }
3. Function Explanation:
-
- Initialize SQS Client: The script uses
boto3
to initialize an Amazon SQS client and define URLs for a FIFO queue and a Standard queue. - Send Messages: In the
lambda_handler
function, it iterates to send a specified number of messages (default is 5) to both the FIFO and Standard queues, using aMessageGroupId
for FIFO to ensure message order. - Return Results: After sending, it prints message details for both queues and returns a JSON response with message information, confirming that messages were sent to both queues.
- Initialize SQS Client: The script uses
4. Deploy the Function: Save and deploy the Lambda function.
Triggering the Lambda Function to Send Messages
-
Invoke the Lambda Function:
- You can manually trigger the function from the AWS Lambda Console using the Test Event.
-
Verify Message Details in the SQS Queue.
- After each invocation, go to the SQS Queue Dashboard. Confirm that messages are received in both queues.
Manually Polling Messages in the SQS Dashboard
With several messages in each queue, it’s time to manually poll messages in the SQS dashboard to see how FIFO and standard queues handle orders.
-
Polling the FIFO Queue and Standard Queue:
- Go to the SQS dashboard, select your FIFO queue, and click Send and receive messages.
- Click on Poll for messages to view the messages in the queue.
- Note that the messages appear in the exact order they were sent, thanks to the FIFO queue’s strict ordering.
- Similarly, return to the SQS dashboard, select your standard queue, and follow the same steps to poll messages.
- Unlike the FIFO queue, the standard queue does not guarantee message order. You may notice that messages appear in a different sequence, demonstrating the standard queue’s flexibility in prioritizing high throughput over message order.
Comparison:
-
- First, message:
-
- Second Message:
-
- Third Message:
-
- Fourth Message:
-
- Fifth Message:
Observing Results and Understanding the Differences
After polling messages in each queue, you’ll notice the following key behaviors:
- FIFO Queue: Messages are processed in the exact order they were sent. This order preservation is due to the
MessageGroupId
, which ensures that messages in the same group are processed sequentially. - Standard Queue: Messages may appear out of order because standard queues are optimized for scalability. This behavior is acceptable in cases where strict message ordering is not required.
Conclusion: Choosing Between FIFO and Standard Queues
By using this simple Lambda function and manually polling messages in the SQS dashboard, we’ve illustrated the core differences between FIFO and standard SQS queues:
- FIFO Queues: Ideal for scenarios where maintaining the exact order of messages is essential, such as transaction processing or order management systems.
- Standard Queues: Best suited for applications where high throughput is more important than preserving message order, like log processing or large-scale data ingestion.
Both queue types provide powerful, reliable message-handling capabilities but cater to different application requirements. Understanding these distinctions will help you select the right queue type for your specific use case, whether order-sensitive workflows or high-throughput systems.
This hands-on example demonstrates how FIFO queues provide a reliable order-preserving option in SQS, while standard queues optimize for scalability over sequence. You should now understand when to choose each type of queue based on your application’s requirements.