Picture a bike rental system where a bike is reported as "returned" before its "rented" event has been processed. The dashboard now shows the bike out on the street, even though it is parked at the station. Switching to an SQS FIFO queue looks like the obvious fix. In my experience, it is only half of one.

I recently wrote a detailed walkthrough on SQS FIFO with Lambda, including a complete CDK project. Here is the condensed version of what I learned while building it.

Order belongs to the message group, not the queue

FIFO in Amazon SQS does not mean one global line. Order is only guaranteed among messages that share the same message group ID. Messages from different groups may overtake each other and are processed in parallel.

That makes the group ID the most important design decision. A constant ID such as "default" gives you strict global ordering, but also only one batch in flight at a time, so a single slow message blocks everything. A random ID per message leaves you with an expensive standard queue. The sweet spot is the smallest business unit where order actually matters: one bike, one order, one customer account.

The group ID also decides how far you scale. For FIFO queues, Lambda runs at most as many concurrent invocations as there are active message groups, or your configured maximum concurrency if that is lower. Six active bikes means six parallel functions at most.

The catch block that quietly reorders events

With standard queues, the usual partial batch response pattern is simple: loop over the records, catch errors, add failures to batchItemFailures, carry on. With FIFO, exactly this pattern breaks the order. If events 1, 2 and 3 of the same bike arrive in one batch and event 2 fails while event 3 succeeds, event 3 has now overtaken event 2.

The fix is to stop at the first failure and report the failed message plus everything after it. A softer variant only stops the affected group and lets other groups continue. That is still correct, because SQS only guarantees order per group anyway. A simplified sketch:

const stopped = new Set<string>(); const batchItemFailures: { itemIdentifier: string }[] = []; for (const record of event.Records) { const group = record.attributes.MessageGroupId ?? ''; try { if (stopped.has(group)) throw new Error('group stopped'); await handle(record); } catch { stopped.add(group); batchItemFailures.push({ itemIdentifier: record.messageId }); } } return { batchItemFailures }; 

Powertools for AWS Lambda offers the same behaviour as SqsFifoPartialProcessor with the skipGroupOnError option. Whichever you choose, I would protect it with a unit test so nobody "simplifies" it later.

Deduplication is not idempotency

FIFO queues drop duplicate sends with the same deduplication ID within five minutes. That protects you from producers that retry after a network error. It does not help when your function writes to DynamoDB and then times out before the message is deleted. The message becomes visible again and is processed a second time, because it was never sent twice.

So the consumer still needs an idempotent write. In the example, every event carries a running number per bike, and the status table remembers the last one processed. A lower or equal number is a duplicate. A gap means something went wrong, and the function would rather write nothing than store a wrong state.

The dead-letter queue is where order really gets lost

This was the part that surprised me most. Once a message exceeds maxReceiveCount and moves to the DLQ, SQS treats it as done and releases the next message of the group. Event 5 is gone, event 6 is processed, and nobody notices.

The sequence check catches exactly this. Event 6 sees the gap, fails and eventually ends up in the DLQ as well. The bike is effectively parked without a wrong state, while all other bikes keep running. That is why I now add a business-level sequence check and a DLQ alarm to every FIFO consumer. With FIFO, a message in the DLQ almost always means a whole group is stuck. The redrive has its own subtle trap, which the full article walks through.

What the full article covers

  • A runnable AWS CDK project in TypeScript with FIFO queue, FIFO DLQ, DynamoDB status table and DLQ alarm
  • The complete batch helper with strict and per-group mode, plus tests
  • Why redriven messages can block a group again, and how to redrive safely
  • High throughput mode, real FIFO limits, common mistakes and costs

Read it here: SQS FIFO with Lambda: ordering per message group

📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.

👉 Next step: Need a schedule in EventBridge? My EventBridge cron helper builds and explains the expression for you.

📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).

Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en