Updated Nov 23, 20266 min read

SQS FIFO Message Group ID: Ordering and Parallelism

How the message group ID decides what an SQS FIFO queue keeps in order and what it processes in parallel, how to pick a key, what a stuck message does to its group, and how an ordering key in code maps onto it.

Illustration for SQS FIFO Message Group ID: Ordering and Parallelism

An SQS FIFO queue does not order everything. It orders messages within a message group, and the group is whatever string you put in MessageGroupId when you send. Everything else about how a FIFO queue behaves, including how much it can process in parallel, follows from that one choice.

This guide covers what the group ID guarantees, how to choose it, and the failure modes that surprise people in production.

What does the message group ID do?

Per the SQS developer guide, messages within the same group are processed one at a time, in strict order, and no two messages from the same group are processed simultaneously. Messages from different groups can be processed at the same time, which is how a FIFO queue serves multiple consumers while keeping order where it matters. On a standard queue the same attribute enables fair queues instead, and does not give ordering.

Choice of group IDOrderingParallelism
One value for every messageWhole queue in orderNone, one message at a time
Account or order IDPer account or orderAs many consumers as there are active groups
Unique value per messageNoneMaximum

The interleaving rule is in the interleaving guide: while a message from a group is in flight and invisible, no other consumer can receive a message from that group until the visibility timeout expires or the message is deleted.

How do you choose the group ID?

Pick the identifier of the entity whose events have to be applied in order. For a ledger that is the account, for an order workflow it is the order, for telemetry it is the device. The test is to ask which pairs of messages would produce a wrong result if swapped: a credit and a debit on the same account would, and a credit on one account and a debit on another would not, so the account ID is the group.

Avoid two mistakes. A constant group ID turns a FIFO queue into a serial queue, which caps throughput at the speed of one consumer, and a group ID with a few huge groups, such as a tenant ID where one tenant sends 90% of the traffic, leaves most consumers idle while one works through a backlog. If the large tenant's events are independent of each other, use a finer key such as the order ID within the tenant.

What happens when one message fails?

The message becomes visible again after the visibility timeout and is delivered again before any later message from its group, so one message that always fails blocks everything behind it in that group, and only that group. A dead-letter queue with a maxReceiveCount lets the queue give up on the message and release the group, and the dead-letter queue then holds it for inspection, as covered in SQS dead-letter queues. Set the visibility timeout above the longest a handler can run, since a message that is still being processed and becomes visible again is delivered twice.

Deduplication is a separate setting. FIFO queues use a MessageDeduplicationId to drop duplicates sent within a 5-minute window, per the deduplication documentation, and handlers still need to be safe to run twice for the redelivery cases above.

What are the throughput limits?

Each partition of a FIFO queue is limited to 300 transactions per second, or 3,000 messages per second with batching, in the default mode, per the SQS quotas. High throughput mode raises that, and spreads load across partitions by message group ID, which is one more reason to use many groups.

How does Encore map an ordering key to the group ID?

Encore brings infrastructure from code, and the ordering key is a field on the topic: orderingAttribute names a field of the message, and messages with the same value are delivered in publish order. The AWS runtime source sets that value as the message group ID when it publishes, per the Pub/Sub runtime code, and the deploy provisions the SNS and SQS resources with dead-letter queues, per the AWS infrastructure docs.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.
Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

A balance updater where credits and debits for one account must apply in order, and a duplicate delivery must not apply twice:

// ledger/ledger.ts import { api, APIError } from "encore.dev/api"; import { Topic, Subscription, Attribute } from "encore.dev/pubsub"; import { SQLDatabase } from "encore.dev/storage/sqldb"; const db = new SQLDatabase("ledger", { migrations: "./migrations" }); interface AccountEvent { accountId: Attribute<string>; // the ordering key, the SQS message group ID on AWS eventId: string; amountCents: number; // positive credit, negative debit } const accountEvents = new Topic<AccountEvent>("account-events", { deliveryGuarantee: "at-least-once", orderingAttribute: "accountId", }); // Callers must be signed in; check that the caller owns accountId before publishing. export const post = api( { expose: true, auth: true, method: "POST", path: "/accounts/:accountId/events" }, async (req: { accountId: string; eventId: string; amountCents: number }): Promise<{ queued: boolean }> => { if (!Number.isInteger(req.amountCents) || req.amountCents === 0) { throw APIError.invalidArgument("amountCents must be a non-zero integer"); } await accountEvents.publish(req); return { queued: true }; }, ); new Subscription(accountEvents, "apply-event", { retryPolicy: { minBackoff: "5s", maxBackoff: "1m", maxRetries: 5 }, handler: async (e) => { const accountId = String(e.accountId); const tx = await db.begin(); try { // A redelivered event is already recorded, so it changes nothing. const fresh = await tx.queryRow<{ event_id: string }>` INSERT INTO applied_events (event_id) VALUES (${e.eventId}) ON CONFLICT DO NOTHING RETURNING event_id`; if (fresh) { await tx.exec` INSERT INTO balances (account_id, cents) VALUES (${accountId}, ${e.amountCents}) ON CONFLICT (account_id) DO UPDATE SET cents = balances.cents + EXCLUDED.cents`; } await tx.commit(); } catch (err) { await tx.rollback(); throw err; } }, }); export const balance = api( { expose: true, auth: true, method: "GET", path: "/accounts/:accountId/balance" }, async ({ accountId }: { accountId: string }): Promise<{ cents: number }> => { const row = await db.queryRow<{ cents: number }>`SELECT cents FROM balances WHERE account_id = ${accountId}`; return { cents: row?.cents ?? 0 }; }, );
-- ledger/migrations/1_init.up.sql CREATE TABLE balances ( account_id TEXT PRIMARY KEY, cents BIGINT NOT NULL ); CREATE TABLE applied_events ( event_id TEXT PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT now() );

The ordering ceiling on AWS is 300 messages per second for the topic, per the Pub/Sub docs, and the same page warns that a failing message blocks later messages with the same key until it is dead-lettered. The orderingAttribute has no effect in local development, so ordering behavior has to be tested in a deployed environment, while encore run still runs the handler and the database locally.

To deploy, connect your AWS account as described in Connect your cloud account and push with git push encore.

A coding agent that adds an event type picks the ordering field in the same change as the handler, and the FIFO setup comes from the declaration, with no MessageGroupId argument to forget on a send call.

Best fit: Services that need per-account or per-order ordering on AWS and want the key declared on the topic, with dead-lettering handled by the platform.

What should you check before choosing FIFO?

Confirm that ordering matters between the messages you would send, since most queues do not need it and a standard queue scales further. When it does, pick the narrowest key that still protects correctness, set a dead-letter queue from the first day and write the handler so a second delivery changes nothing.

Frequently asked questions

What is the message group ID in SQS FIFO?

It is the attribute that tags messages as belonging to one ordered group. Messages in the same group are processed one at a time in strict order, while different groups can be processed in parallel by multiple consumers.

What should I use as the message group ID?

Use the identifier of the thing whose events must stay in order, such as an account ID, order ID or device ID. A single constant value makes the whole queue one group, which processes everything serially, and a random value gives no ordering at all.

What happens when a message in a group fails?

Later messages in the same group wait, since the failed message becomes visible again after the visibility timeout and is retried first. After the queue's maximum receive count it moves to the dead-letter queue and the group continues.

How do I get ordered processing per key without configuring FIFO queues?

With Encore you set orderingAttribute on a Topic to a field of the message, and messages with the same value are delivered in publish order. On AWS the value is used as the message group ID on an SNS and SQS FIFO setup, and ordering has no effect in local development.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders