SQS Standard queues don’t guarantee order, but FIFO queues can guarantee order, but only if you use them correctly.
Let’s see what that actually looks like. Imagine you have a system that processes customer orders. You’ve got a web server that receives orders and drops them into an SQS queue for a worker process to pick up and fulfill.
Standard Queue in Action
// Message 1: Order { "order_id": "123", "item": "widget", "quantity": 1 }
// Message 2: Order { "order_id": "456", "item": "gadget", "quantity": 2 }
// Message 3: Order { "order_id": "123", "item": "widget", "quantity": 2 } // Update for order 123
If these messages go into a Standard queue, the worker might receive them in this order: Message 1, Message 3, Message 2. The update to order 123 (Message 3) arrived after the initial order (Message 1) in the queue, but the worker processed Message 2 first. You’d have to build logic into your worker to handle out-of-order processing, perhaps by checking if an order is already in progress or has been updated.
FIFO Queue in Action
Now, if these same messages go into a FIFO queue, and you set the MessageGroupId to 123 for Message 1 and Message 3, and 456 for Message 2, the worker will receive them in order within their groups:
// Worker receives:
Message 1 (GroupId: 123)
Message 3 (GroupId: 123)
Message 2 (GroupId: 456)
The worker gets Message 1, then Message 3 (the update for order 123), and then Message 2. This is because FIFO queues process messages in the order they are sent, per Message Group ID. All messages with the same MessageGroupId are delivered in strict order. Messages with different MessageGroupIds can be delivered in any order relative to each other.
The core problem FIFO queues solve is ensuring that related messages are processed in the sequence they were sent, preventing race conditions and ensuring data integrity. Standard queues are designed for high throughput and at-least-once delivery, sacrificing strict ordering.
Here’s how you’d create them:
Standard Queue:
aws sqs create-queue --queue-name my-standard-queue
This creates a queue with the default settings, which are Standard.
FIFO Queue:
aws sqs create-queue --queue-name my-fifo-queue.fifo --attributes '{"FifoQueue": "true", "ContentBasedDeduplication": "true"}'
Notice the .fifo suffix on the queue name, which is mandatory for FIFO queues. FifoQueue: "true" enables FIFO behavior. ContentBasedDeduplication: "true" is optional but highly recommended for FIFO; it automatically deduplicates messages based on their content, preventing duplicate processing if you send the same message multiple times within the 5-minute deduplication interval.
When sending messages to a FIFO queue, you must specify a MessageGroupId.
aws sqs send-message --queue-url <your-fifo-queue-url> --message-body '{"order_id": "123", "item": "widget", "quantity": 1}' --message-group-id "123"
aws sqs send-message --queue-url <your-fifo-queue-url> --message-body '{"order_id": "456", "item": "gadget", "quantity": 2}' --message-group-id "456"
aws sqs send-message --queue-url <your-fifo-queue-url> --message-body '{"order_id": "123", "item": "widget", "quantity": 2}' --message-group-id "123"
The MessageGroupId is the key to ordering in FIFO queues. All messages sent with the same MessageGroupId are processed in strict order. If you omit MessageGroupId when sending to a FIFO queue, SQS will reject the message.
The mental model to hold onto is that Standard queues are like a busy highway with many lanes – cars (messages) arrive and depart quickly, but their relative order isn’t guaranteed. FIFO queues are like a single-lane road for each destination (Message Group ID) – traffic flows strictly in order to that specific destination.
A common misunderstanding is that FIFO queues guarantee global ordering across all messages. This isn’t true. They guarantee ordering per Message Group ID. If you send messages for order_id: 123 and order_id: 456, messages for 123 will be ordered amongst themselves, and messages for 456 will be ordered amongst themselves, but a message for 456 might be processed before a message for 123, or vice-versa, depending on how they are received by the worker.
The next concept to grapple with is duplicate detection in FIFO queues.