Sub topic 9
FIFO message groups and deduplication IDs
This chapter assumes you know how to create an SQS queue, send and receive messages, and how the visibility timeout works. It also assumes you have read the comparison of standard and FIFO queues. Examples use Python with boto3.
What a FIFO queue guarantees
A FIFO queue keeps messages in order and drops duplicate sends. Two message properties control this:
- The message group ID defines which messages must stay in order relative to each other.
- The message deduplication ID defines which sends count as the same message.
Both are set by the producer when it calls SendMessage. The group ID is required on every send to a FIFO queue. The deduplication ID is required unless the queue has content-based deduplication turned on.
Message groups
A message group is a set of messages that share a MessageGroupId. SQS delivers the messages of one group in the order they were sent. Messages in different groups have no ordering relationship, so SQS can deliver them in parallel to different consumers.
A group acts like a lane. Within a lane, order is strict. Across lanes, there is no order and no limit on how many lanes you use.
How a group is locked while in flight
When a consumer receives messages from a group, SQS stops returning further messages from that group until the received messages are deleted or become visible again. This is what preserves order. If message 1 of a group is in flight, message 2 of the same group is held back, even if other consumers are polling.
This has two consequences:
- Parallelism is limited by the number of distinct groups that have messages waiting. One group means one effective consumer at a time.
- A message that keeps failing blocks the rest of its group. Messages behind it wait until it is deleted, or until a redrive policy moves it to a dead-letter queue. Other groups keep flowing.
A single ReceiveMessage call can return several messages from the same group, in order. Process them in the order returned.
Choosing a group ID
Pick the entity whose events must stay in order. Common choices are a customer ID, an order ID, or an account ID. Consider these trade-offs:
| Choice | Ordering | Parallelism |
|---|---|---|
| One constant group ID | Total order across the queue | One consumer at a time |
| One group per order | Order kept per order | High, as many orders are active |
| A random value per message | No useful order | Maximum, but FIFO gives you nothing a standard queue would not |
Avoid a "hot" group that receives most of the traffic. It becomes the bottleneck for the whole queue.
Deduplication IDs
A producer may retry a send because a network call timed out and it does not know whether the first attempt succeeded. Without protection, the queue could hold two copies. The deduplication ID prevents this.
SQS remembers each deduplication ID for a deduplication interval of 5 minutes. If a second send arrives with the same ID within that interval, SQS accepts the call and returns success, but it does not enqueue a second message. After the interval, the same ID is treated as new.
You can supply the ID in two ways:
- Explicitly, with the
MessageDeduplicationIdparameter. Use a value that identifies the business event, such as a payment ID plus an attempt number. - Content-based, by enabling
ContentBasedDeduplicationon the queue. SQS then computes a SHA-256 hash of the message body (not the attributes) and uses it as the ID. If you also pass an explicit ID, the explicit value wins.
Sending with a group 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
)Running the same send_message call again within five minutes returns a normal response with the same message ID as the first send. The queue still holds one message.
Content-based deduplication
Content-based deduplication is convenient, but it can drop messages you meant to keep. Two different events with an identical body, such as {"action": "refresh"} sent a minute apart, collapse into one. Include a unique field, such as an event ID or timestamp, in the body if every send must count.
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.create_queue(
QueueName="events.fifo",
Attributes={
"FifoQueue": "true",
"ContentBasedDeduplication": "true",
},
)["QueueUrl"]
# No MessageDeduplicationId needed: SQS hashes the body.
sqs.send_message(
QueueUrl=queue_url,
MessageBody='{"eventId": "e-100", "action": "refresh"}',
MessageGroupId="device-7",
)Edge cases and gotchas
- Deduplication scope. Deduplication is applied per queue by default. High-throughput FIFO mode can scope it per message group instead. In that mode the same ID in two groups is two distinct messages.
- Sends dropped as duplicates still succeed. You cannot tell from the response that a send was suppressed. Design producers so that a repeated ID always means the same event.
- Retry changes ordering. If a producer sends message A, times out, and sends message B before retrying A, order can be lost. Retry the same message with the same deduplication ID before sending the next one in that group. The SQS API also accepts a
SequenceNumberin the response, which increases within a group and can help you verify order. - Receive request attempt ID.
ReceiveMessageon FIFO queues accepts aReceiveRequestAttemptId. If a receive call fails on the network, repeat it with the same ID within five minutes to get the same batch back, which avoids waiting for the visibility timeout. - Dead-letter queues must match. A FIFO queue can only use a FIFO queue as its dead-letter queue.
- Consumers still need to be idempotent. FIFO prevents duplicate sends within the interval. It does not remove the case where your consumer processes a message, crashes before deleting it, and sees it again after the visibility timeout.
- SNS to FIFO. An SNS FIFO topic can publish to SQS FIFO queues. Set the message group ID when publishing so ordering is carried through.
Throughput
Standard FIFO queues have a lower throughput ceiling than standard queues. High-throughput mode raises it by partitioning the queue by message group ID. Many distinct groups let it scale. Enabling it requires setting the deduplication scope to message group and the throughput limit to per message group ID. Check the current SQS quotas for the exact limits in your Region, because they change over time.
Putting it together
A good FIFO design answers three questions:
- What entity needs ordered processing? That is your group ID.
- What identifies a single business event? That is your deduplication ID.
- What happens when a message fails repeatedly? Set a FIFO dead-letter queue and a
maxReceiveCountso one bad message does not block its group forever.
Exercise
A FIFO queue uses a single constant MessageGroupId for all messages. You add ten consumers, but throughput does not improve. Why, and what change would help while keeping order where it matters?
Show answer
SQS holds back later messages of a group while earlier ones are in flight, so a single group lets only one consumer work at a time. Use a group ID per entity that needs ordering, such as one per customer or per order. Messages for different entities then flow in parallel and order is kept within each entity.
Exercise
A producer sends the body {"action": "refresh"} to a queue with content-based deduplication enabled, then sends the identical body again two minutes later. How many messages are enqueued, and how do you make both count?
Show answer
One message is enqueued, because both bodies hash to the same value within the five-minute deduplication interval and the second send is treated as a duplicate. To make both count, add a unique field such as an event ID to the body, or pass a distinct MessageDeduplicationId on each send.