Skip to Content

SQS FIFO and patterns

SQS FIFO queues with ordering and deduplication, dead-letter queues, delay queues, and SQS as a buffer in front of a database.

FIFO queues

An SQS FIFO queue: messages sent in the order 1, 2, 3, 4 are received in the same order within a message group ID, duplicates sent within 5 minutes are removed, and throughput is 300 messages a second, or 3,000 with batching An SQS FIFO queue: messages sent in the order 1, 2, 3, 4 are received in the same order within a message group ID, duplicates sent within 5 minutes are removed, and throughput is 300 messages a second, or 3,000 with batching

First in, first out, and no duplicates.

A FIFO queue (First In, First Out) keeps messages in order. Its name must end in .fifo.

  • Ordering by message group ID: messages with the same message group ID (a mandatory parameter) are received in the order they were sent. Different groups can be processed in parallel, for example one group per customer.
  • Exactly-once processing: FIFO removes duplicates sent within a 5-minute deduplication interval, using a deduplication ID sent with the message or content-based deduplication (a hash of the message body).
  • Limited throughput: 300 messages per second without batching, 3,000 per second with batching (10 messages per call). High throughput mode raises this a lot (up to 70,000 transactions per second per API action in the largest Regions), spread across message groups.

Use FIFO when order matters (bank transactions, an order's status changes) or when duplicates would be harmful.

Dead-letter queues

A dead-letter queue: when a message has been received from the source queue more times than maxReceiveCount without being deleted, for example because the consumer keeps failing on it, SQS moves it to the dead-letter queue for analysis A dead-letter queue: when a message has been received from the source queue more times than maxReceiveCount without being deleted, for example because the consumer keeps failing on it, SQS moves it to the dead-letter queue for analysis

Messages that keep failing are set aside.

Some messages can never be processed: a malformed order, a bug triggered by one value. Without care, such a "poison" message is received, fails, reappears and fails again, forever.

  • A dead-letter queue (DLQ) is a normal queue that receives messages a source queue gave up on.
  • The source queue's redrive policy sets maxReceiveCount: after a message has been received that many times without being deleted, SQS moves it to the DLQ.
  • The DLQ of a standard queue must be a standard queue, and the DLQ of a FIFO queue a FIFO queue.
  • After fixing the bug, redrive the messages back to the source queue.
  • A message's expiry is based on when it was first sent, so give the DLQ a long retention period (up to 14 days).

Not in the course slides

Dead-letter queues and delay queues are only mentioned in passing in this section of the slides, but they're common exam topics. This section is written from the AWS documentation.

Delay queues

A delay queue hides new messages for a set time, 0 to 15 minutes, before consumers can see them. A delay can also be set on single messages with a message timer. Use it when consumers need time before acting, for example waiting for a related record to be written first.

SQS as a buffer to database writes

SQS as a buffer: during a traffic spike the front-end tier sends work to an SQS queue instead of writing to the database directly; the back-end tier reads the queue at a steady pace and writes to the database, which is never overloaded SQS as a buffer: during a traffic spike the front-end tier sends work to an SQS queue instead of writing to the database directly; the back-end tier reads the queue at a steady pace and writes to the database, which is never overloaded

The queue absorbs the spike; the database never sees it.

If an application writes transactions straight to a database (RDS, Aurora, DynamoDB) and the load gets too big, some transactions may be lost: the database throttles or times out, and the request fails.

Instead:

  1. The front end enqueues each transaction in SQS (SendMessage), which never runs out of capacity.
  2. A back-end tier dequeues messages (ReceiveMessage) and inserts them into the database at a pace it can handle, deleting each message only once it's written.

Nothing is lost: if the database slows down, messages wait in the queue. This works when the client doesn't need an immediate answer from the database.

Real-world examples

  • A stock trading app puts each account's orders in a FIFO queue with the account ID as message group ID: orders for one account stay in order, different accounts are processed in parallel.
  • A payment processor uses FIFO deduplication so a retried request doesn't charge a customer twice.
  • An image pipeline sends files that crash the resizer to a DLQ after 5 attempts; engineers inspect them the next morning.
  • A ticket sale absorbs 50,000 orders a minute in SQS while the database writes them at 2,000 a minute.

Exam tips

  • "Order must be preserved", "no duplicates" → SQS FIFO (message group ID, deduplication ID).
  • "FIFO throughput" → 300 msg/s, or 3,000 with batching (higher with high throughput mode).
  • "Messages that fail repeatedly block the queue" → dead-letter queue with maxReceiveCount.
  • "Delay processing of new messages for a few minutes" → delay queue (up to 15 minutes).
  • "Database can't keep up with writes during spikes, don't lose any" → SQS as a write buffer.

Common traps

  • "Standard queues with a sequence number give the same result as FIFO." Only FIFO guarantees order and deduplication.
  • "Convert a standard queue to FIFO." That isn't possible; create a new FIFO queue.
  • "The DLQ keeps messages for the DLQ's full retention from when they arrive there." Expiry counts from the original send time.

Hands-on lab: a FIFO queue and a dead-letter queue

Goal: see deduplication in a FIFO queue, and a message move to a DLQ.

Costs money

A few dozen requests cost far less than a cent. Any Region works.

Create the FIFO queue

SQS → Create queue, type FIFO, name saa-orders.fifo, tick Content-based deduplication. Create.

Send a duplicate

Send and receive messages: body order 1, message group ID customer-a. Send it twice. Then send order 2. Poll: you receive order 1 and order 2 once each, in order. The duplicate was dropped.

Add a dead-letter queue

Create a standard queue saa-dlq. Edit saa-orders (the standard queue from the SQS lab): Dead-letter queue → Enabled, choose saa-dlq, Maximum receives 2. Set its visibility timeout to 5 seconds.

Fail a message

Send a message to saa-orders. Poll it, don't delete it, wait 5 seconds; poll again, wait again. On the third poll it's gone from saa-orders; poll saa-dlq and it's there.

Expected result: duplicates dropped by the FIFO queue, and a repeatedly failed message moved to the DLQ.

Clean up: delete saa-orders.fifo, saa-orders and saa-dlq.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt