SNS FIFO Topics

Difficulty: Intermediate

Question

What is an SNS FIFO topic, how does it guarantee ordering and deduplication, and when would you use it?

Answer

An SNS FIFO topic is a first-in-first-out version of an Amazon SNS topic that delivers messages to subscribers in the exact order they were published within a message group, and drops duplicates published within a five-minute window. It is designed for fan-out to SQS FIFO queues, so several downstream services can each receive the same ordered, deduplicated stream of events.

How ordering works

  • Every publish must include a MessageGroupId. Ordering is guaranteed within a group, not across the whole topic.
  • Pick the group so it matches the thing that must stay ordered: an order id, an account id, a wallet id.
  • Different groups are processed in parallel, which is how FIFO topics scale. One group per customer gives strict per-customer ordering while thousands of customers progress independently.

How deduplication works

  • Each message has a MessageDeduplicationId, either set explicitly or generated from a SHA-256 hash of the body when content-based deduplication is enabled.
  • If the same deduplication id is published again within 5 minutes, SNS accepts the call but does not deliver it again.
  • This protects against producer retries, for example when a network timeout makes your service publish the same "payment captured" event twice.

Combined with SQS FIFO queues and consumers that delete messages after processing, this gives effectively exactly-once processing end to end.

Standard vs FIFO topics

Standard topicFIFO topic
OrderingBest effortStrict within a message group
DuplicatesPossibleDeduplicated within 5 minutes
ThroughputVery highLower; scales with number of message groups
SubscribersSQS, Lambda, HTTP, email, SMS, mobile push, FirehoseSQS queues (FIFO for ordering)
Topic nameAnyMust end in .fifo

FIFO topics support message filtering, encryption with KMS, and message archiving and replay, which lets a new subscriber catch up from an archived history.

When to use it

  • Bank or wallet ledgers: a debit must be applied before a later credit for the same account. Publish txn-events.fifo with MessageGroupId = accountId, fan out to a ledger service queue, a notifications queue and an analytics queue.
  • Order lifecycle events: CREATED -> PAID -> SHIPPED -> DELIVERED must arrive in order to every subscriber for the same order.
  • Price or inventory updates where applying an older update after a newer one would corrupt state.

Gotchas

  • Using a single MessageGroupId for everything forces fully serial processing and kills throughput.
  • SNS FIFO does not deliver to Lambda, HTTP, email or SMS directly. Subscribe an SQS FIFO queue and let Lambda consume from it.
  • Subscribing a standard SQS queue to a FIFO topic is possible but that queue loses the ordering and deduplication guarantees.
  • Deduplication only covers 5 minutes; long-delayed retries still need idempotent consumers.
Ordered fan-out with SNS FIFO. One ordered, deduplicated stream reaches several FIFO queues, each consumed independently.
  1. Producer publishes: Includes MessageGroupId and deduplication id.
  2. SNS FIFO topic: Orders per group, drops duplicates within 5 minutes.
  3. SQS FIFO queues: Ledger, notifications and analytics queues.
  4. Consumers per queue: Process each group in order.

Code examples

Create a FIFO topic and publish ordered events

import boto3, json

sns = boto3.client("sns", region_name="ap-south-1")

topic = sns.create_topic(
    Name="wallet-txn.fifo",
    Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "false"},
)["TopicArn"]

def publish_txn(account_id, txn_id, kind, amount_paise):
    sns.publish(
        TopicArn=topic,
        Message=json.dumps({"txnId": txn_id, "type": kind, "amount": amount_paise}),
        MessageGroupId=account_id,          # order is kept per account
        MessageDeduplicationId=txn_id,      # retries of the same txn are dropped
    )

publish_txn("acct-42", "txn-1001", "DEBIT", 50000)
publish_txn("acct-42", "txn-1002", "CREDIT", 20000)
publish_txn("acct-42", "txn-1001", "DEBIT", 50000)  # duplicate, not delivered again

Using the account id as the group keeps each account's transactions in order while different accounts flow in parallel; the transaction id as deduplication id makes producer retries safe.

Key points

  • SNS FIFO topics order messages per MessageGroupId and deduplicate within 5 minutes.
  • Topic names must end in .fifo.
  • Designed for fan-out to SQS FIFO queues; no direct Lambda, HTTP, email or SMS delivery.
  • Throughput scales with the number of distinct message groups.
  • Pair with idempotent consumers for full exactly-once processing.

Common follow-up questions

  • How would you choose a MessageGroupId for a payments system?
  • What is the difference between content-based and explicit deduplication?
  • Why can't a Lambda function subscribe directly to an SNS FIFO topic?
  • How would you scale a FIFO pipeline that is too slow?

Interview tips

  • Stress that ordering is per message group, not global; it is the most common misunderstanding.
  • Give a concrete grouping key like account id or order id.
  • Mention the SNS FIFO plus SQS FIFO fan-out pattern as the standard design.

Concepts covered

SNS FIFO Topics, Message Group ID, Deduplication, SQS FIFO, Fan-out, Strict Ordering

More AWS & Cloud Fundamentals questions