Sub topic 7
Standard and FIFO Queues Compared
This chapter assumes you can create an SQS queue and send, receive, and delete messages, as covered in the earlier chapters. It compares the two queue types and gives criteria for choosing one.
The two queue types
A standard queue is the default. It favors throughput and availability over strict guarantees. A FIFO (first in, first out) queue favors ordering and duplicate protection, and it accepts lower throughput in return.
| Property | Standard | FIFO |
|---|---|---|
| Ordering | Best effort, so messages can arrive out of order | Strict order within a message group |
| Delivery | At least once, so duplicates are possible | Duplicates within the deduplication window are dropped |
| Throughput | Nearly unlimited | Limited per API action, with a high throughput mode for more |
| Queue name | Any valid name | Must end in `.fifo` |
| Per-message timers | Supported | Not supported (queue-level delay still works) |
Choose the type when you create the queue. You cannot convert an existing queue from one type to the other.
Creating and using a FIFO queue
A FIFO queue needs the FifoQueue attribute and a name ending in .fifo. Each message carries a group ID and a deduplication ID.
import boto3
sqs = boto3.client("sqs")
# FIFO queue names must end in .fifo
queue_url = sqs.create_queue(
QueueName="payments.fifo",
Attributes={"FifoQueue": "true"},
)["QueueUrl"]
sqs.send_message(
QueueUrl=queue_url,
MessageBody='{"paymentId": "p-1", "amount": 25}',
MessageGroupId="customer-42", # ordering is kept within a group
MessageDeduplicationId="p-1-attempt-1" # repeats within the dedup window are dropped
)The group ID defines the unit of ordering. Messages in the same group are delivered in order, and one consumer at a time works on a group. Different groups are processed in parallel. The deduplication ID makes a retried send safe: if you send the same ID again within the five-minute deduplication window, SQS accepts the call but does not enqueue a second copy. You can enable content-based deduplication on the queue instead, which derives the ID from a hash of the message body.
Common errors
- Sending to a FIFO queue without a
MessageGroupId. The call fails. - Using one group ID for every message. Ordering is preserved, but you lose parallelism because one group is processed serially.
- Using a random deduplication ID on each retry. The retry is then treated as a new message, so deduplication never triggers.
- Attaching a standard dead-letter queue to a FIFO queue. The dead-letter queue must be the same type as its source.
- Assuming FIFO removes the need for idempotent consumers. Deduplication covers the send side only. A consumer that crashes before deleting a message still sees it again.
Choosing
Start with a standard queue. Move to FIFO when processing order matters or a duplicate causes real harm, such as account balance updates for a single customer. Use the entity that needs ordering, such as a customer or an order, as the group ID.
Exercise
A service sends "create order", "add item", and "close order" messages for each order, and they must be handled in sequence per order. Many orders are processed at once. Which queue type and group ID fit?
Show answer
Use a FIFO queue with the order ID as the MessageGroupId. Each order's messages stay in sequence, and different orders are consumed in parallel.