Sub topic 7
Standard and FIFO queues compared
This chapter assumes you can create a queue and send, receive and delete messages, and that you know what a visibility timeout is. The examples use Python with boto3.
SQS offers two queue types. You choose the type when you create the queue, and you can't change it afterwards. If you pick the wrong one, you create a new queue and migrate your producers and consumers.
What each type guarantees
A standard queue gives you almost unlimited throughput. It delivers each message at least once, so you may occasionally receive a duplicate. It also orders messages on a best-effort basis, so messages can arrive out of order.
A FIFO queue (first in, first out) keeps messages in order within a message group. It also removes duplicate sends within a deduplication window of five minutes. The price is lower default throughput and a few feature restrictions.
| Property | Standard | FIFO |
|---|---|---|
| Ordering | Best effort | Strict within a message group |
| Delivery | At least once | Duplicate sends dropped within 5 minutes |
| Throughput | Nearly unlimited | 300 API calls per second per action by default, or 3,000 messages per second with batches of 10 |
| Queue name | Any valid name | Must end in `.fifo` |
| Per-message delay timer | Supported | Not supported (queue-level delay works) |
| In-flight message limit | About 120,000 | About 20,000 |
| Dead-letter queue type | Standard | Must be FIFO |
FIFO queues also offer a high throughput mode, which raises the limits considerably. The exact ceiling depends on the Region, so check the current SQS quotas before you rely on a number.
Choosing a type
Use a standard queue when each message is independent: resizing images, sending emails, or recording clickstream events. Use a FIFO queue when the sequence changes the result. Examples are applying account updates to the same customer, or processing a create, update and delete for the same record.
Creating both types
The calls differ in only a few attributes. The following script creates one of each.
import boto3
sqs = boto3.client("sqs")
standard_url = sqs.create_queue(QueueName="thumbnails")["QueueUrl"]
fifo_url = sqs.create_queue(
QueueName="account-updates.fifo",
Attributes={
"FifoQueue": "true",
"ContentBasedDeduplication": "true",
},
)["QueueUrl"]
print(standard_url)
print(fifo_url)With ContentBasedDeduplication enabled, SQS computes a SHA-256 hash of the message body and uses it as the deduplication ID. You can then omit MessageDeduplicationId when you send. Two messages with identical bodies inside five minutes count as one, even if you meant them as separate events. Include a unique field, such as an event ID, in the body if that matters.
Message groups and ordering
Every FIFO message needs a MessageGroupId. SQS preserves order within one group. Different groups have no ordering relationship, and consumers can process them in parallel.
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.get_queue_url(QueueName="account-updates.fifo")["QueueUrl"]
updates = [
("customer-42", "address changed"),
("customer-42", "email changed"),
("customer-7", "plan upgraded"),
]
for i, (customer, change) in enumerate(updates):
sqs.send_message(
QueueUrl=queue_url,
MessageBody=change,
MessageGroupId=customer,
MessageDeduplicationId=f"update-{i}",
)SQS holds back later messages in a group while an earlier message from that group is in flight, which means received but not yet deleted. A consumer that is slow or failing on one message therefore blocks the rest of its group until the message is deleted or its visibility timeout expires. Other groups are not affected.
For this reason, choose the group key carefully. A key such as customer-42 gives you per-customer ordering and lets many customers proceed in parallel. A single constant group ID gives you total ordering but only one active consumer at a time.
Duplicates and retries
In a standard queue, duplicates come from two places. SQS stores messages on several servers, and in rare cases a message is delivered twice. Your own retries can also cause them. The consumer must be idempotent, meaning that processing the same message twice has the same effect as processing it once.
FIFO deduplication covers only the send side. If a producer retries a send with the same deduplication ID within five minutes, SQS accepts the call but stores a single message. It doesn't stop a consumer from receiving a message twice after a visibility timeout expires. A consumer that takes longer than the timeout still sees the message again, so idempotent consumers are good practice on FIFO queues too.
On the receive side, FIFO supports a ReceiveRequestAttemptId. If a receive call times out and you retry it with the same ID within five minutes, you get the same batch back rather than a new one.
Common errors
- Creating a queue named
paymentswithFifoQueueset totruefails. The name must end in.fifo. - Sending to a FIFO queue without
MessageGroupIdfails. Without a deduplication ID, it also fails unless content-based deduplication is on. - Setting
DelaySecondson an individual FIFO message is rejected. Set the delay on the queue instead. - Attaching a standard queue as the dead-letter queue of a FIFO queue fails. Both must be FIFO.
- Trying to convert an existing queue by changing an attribute is not possible. Create a new queue.
Exercise
An order system must apply "created", "paid" and "shipped" events to each order in sequence, but different orders are independent and the system handles thousands of orders concurrently. Which queue type and message group ID would you choose, and why?
Show answer
Use a FIFO queue with the order ID as the MessageGroupId. Events for one order stay in sequence, and different orders sit in different groups, so consumers process them in parallel. A single shared group ID would give correct ordering but allow only one consumer at a time. A standard queue could deliver "shipped" before "paid".
Exercise
A FIFO producer retries a send after a network timeout, using the same MessageDeduplicationId 90 seconds later. How many copies of the message end up in the queue, and what changes if the retry happens 10 minutes later?
Show answer
One copy, because the retry is inside the five-minute deduplication window and SQS accepts the call without storing a second message. After 10 minutes the window has passed, so SQS treats the retry as a new message and the queue holds two copies.