Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

An order service publishes OrderPlaced events that billing, shipping and analytics must each receive, and billing must process events for the same order in order. Design this with SNS and SQS from Java, covering FIFO topics and queues, deduplication, filtering and idempotency.

We publish OrderPlaced to an SNS topic and subscribe one SQS queue per consumer, so billing, shipping and analytics each get their own copy and fail independently. Because billing needs per-order ordering, the topic is an SNS FIFO topic and billing's queue is an SQS FIFO queue, with MessageGroupId set to the order id: events of one order stay in order while different orders flow in parallel. FIFO deduplication drops repeats with the same MessageDeduplicationId within five minutes, but consumers still need idempotency, filter policies decide which events reach each queue, and consumers that do not need order can use cheaper, faster standard queues.

Why this question is asked

Fan-out with ordering is one of the most common event designs on AWS, and it is easy to get subtly wrong: a single queue shared by three services, ordering promised across all orders when only per-order ordering is needed, or deduplication trusted to remove every duplicate. Interviewers use this question to see whether we understand where SNS and SQS guarantee order and uniqueness, and where our Java code must take over. In this answer, we will build the fan-out, add ordering with message groups, look at deduplication and its limits, add filtering, and finish with throughput and idempotency.

1A Quick Recap of SNS and SQS Together

SNS is a publish-subscribe service: a producer publishes a message to a topic, and SNS pushes a copy to every subscription. SQS is a queue: messages wait until a consumer receives and deletes them. Together they give fan-out with buffering. The order service publishes once; each consumer service owns a queue subscribed to the topic, so it gets its own copy, reads at its own pace, and can be down for an hour without losing anything or slowing the others.

The alternative, all three services reading from one queue, does not work, because a queue gives each message to only one consumer. And having the order service call three services directly couples it to their availability.

Step throughOrderPlaced fan-out
  1. Order servicepublishes OrderPlaced with group id = orderId
  2. SNS FIFO topic orders.fifocopies the event to each subscription
  3. Subscriptions with filter policiesbilling: all; shipping: physical goods; analytics: all
  4. Queuesbilling.fifo, shipping.fifo, analytics.fifo
  5. Consumerseach with its own DLQ and idempotent handler

5 layers, from the top down. Step through them one at a time.

2Ordering Per Order With Message Groups

Billing must process OrderPlaced, OrderAmended and OrderCancelled for the same order in the order they happened, otherwise it might charge for an order already cancelled. It does not need global order across all orders, and asking for that would serialize the whole system.

FIFO topics and queues give exactly the order we need. Every message carries a MessageGroupId, and ordering is guaranteed within a group. We use the order id as the group id. SQS FIFO delivers the next message of a group only after the previous one has been deleted, while messages of different groups are delivered in parallel to different consumers. So order 42's events are strictly ordered, and order 43's events are processed at the same time on another thread.

Java
SnsClient sns = SnsClient.create();
sns.publish(PublishRequest.builder()
.topicArn("arn:aws:sns:eu-west-1:123456789012:orders.fifo") // FIFO topic names end in .fifo
.message(json) // {"type":"OrderPlaced","orderId":"42",...}
.messageGroupId("order-42") // ordering holds within this group
.messageDeduplicationId(event.eventId()) // same id within 5 minutes -> dropped
.messageAttributes(Map.of(
"eventType", MessageAttributeValue.builder().dataType("String").stringValue("OrderPlaced").build(),
"fulfilment", MessageAttributeValue.builder().dataType("String").stringValue("physical").build()))
.build());

Notice that the producer must also publish in order. If two threads of the order service publish events of the same order concurrently, SNS orders them by arrival, which may not be the business order. So the order service publishes an order's events from one place, often an outbox table read in sequence.

3Deduplication and Its Limits

FIFO topics and queues drop a message whose MessageDeduplicationId was already seen in the last five minutes. We set it to the event's own unique id, so a producer retry after a timeout does not create a second event. Alternatively, content-based deduplication uses a hash of the body, which only works if retries send byte-for-byte identical bodies.

But deduplication only covers the publish side and only for five minutes. A consumer can still get the same message twice, for example when its processing outlasts the visibility timeout or it crashes before deleting, and an outbox relay that republishes after an hour is outside the window. So every consumer remains idempotent: billing records processed event ids in its own database, in the same transaction as the charge, and skips any it has seen.

4Filtering So Each Queue Gets Only What It Needs

Shipping only cares about orders with physical goods. Instead of receiving everything and discarding most of it, its subscription gets a filter policy, a small JSON rule on message attributes, or on the message body when the policy scope is set to the body. SNS evaluates it and delivers only matching messages. Analytics has no filter and receives all events. This keeps each consumer simple and saves queue and processing cost.

Java
// filter policy on the shipping subscription (scope: MessageAttributes)
{ "eventType": ["OrderPlaced", "OrderCancelled"],
"fulfilment": ["physical"] } // digital orders never reach shipping.fifo

5Throughput, and When to Use Standard Queues

FIFO comes with lower throughput limits than standard queues, measured in messages per second per topic or queue and per message group. High throughput mode for FIFO queues raises them considerably, and the quotas have grown over the years, so we check the current numbers for our region rather than designing from an old figure. A single hot group, such as one enormous order, is processed one message at a time no matter what the queue allows.

Analytics does not need order: it aggregates. Where the service allows mixing, we can give analytics a standard queue, which is cheaper and nearly unlimited in throughput, and have it tolerate out-of-order and duplicate events. Whether an SNS FIFO topic can deliver to a standard queue depends on current SNS support, so we check; if it cannot, analytics subscribes through a separate standard topic or reads a copy from its FIFO queue. The general rule is to pay for ordering only where a consumer needs it.

Per-consumer choices
ConsumerNeeds order?QueueFilter
BillingYes, per orderSQS FIFO, group = orderIdall order events
ShippingYes, per orderSQS FIFOphysical goods only
AnalyticsNoStandard if supported, else FIFOnone
Every consumer is idempotent, whatever queue it uses.

6Follow-up questions

How do you replay events to a new consumer with SNS and SQS?

SNS and SQS are not an event log: once a queue deletes a message it is gone, and a new subscription only receives messages published after it was created. For FIFO topics, SNS offers message archiving with a retention period and replay to a subscription, which covers this case within the archive window; check its current limits. Otherwise we replay from our own source of truth, such as the outbox table or an event store in S3 written by an archiving subscriber, publishing the history into the new consumer's queue directly so other consumers do not see it again. If replay is a frequent need, that is a sign the use case wants a log such as Kafka.

What happens to ordering if billing fails on one event?

In an SQS FIFO queue, the failed message is not deleted, so it becomes visible again after the visibility timeout and is retried, and later messages of the same group wait behind it. Other groups continue. After maxReceiveCount it moves to the FIFO dead-letter queue, which unblocks the group, so we must decide whether later events of that order are still safe to process; billing usually parks the whole order for manual review.

Why not use one SQS queue that all three services poll?

A queue delivers each message to one consumer, so the three services would steal messages from each other. Each service needs its own copy, which is exactly what one queue per subscriber gives.

7Conclusion

In this answer, we fanned OrderPlaced out from one SNS topic to one SQS queue per consumer, used a FIFO topic and FIFO queues with the order id as MessageGroupId so billing sees each order's events in order while orders run in parallel, set MessageDeduplicationId to the event id while keeping consumers idempotent beyond the five-minute window, filtered events per subscription, and saved FIFO's limits for consumers that need ordering.

What to say in the interviewAbout 100 seconds aloud

Open with

I would publish OrderPlaced to an SNS topic and give each consumer its own SQS queue subscribed to it, so billing, shipping and analytics each get a copy, read at their own pace and can fail without affecting the others.

Then, if they want more

Billing needs the events of one order in order, so the topic is an SNS FIFO topic and billing's queue is an SQS FIFO queue, and the Java producer sets MessageGroupId to the order id. Ordering then holds within each order, and different orders are processed in parallel, which keeps throughput reasonable.

The producer itself must publish an order's events in sequence, for example from an outbox. For duplicates, I set MessageDeduplicationId to the event's id, so a retried publish within five minutes is dropped, or use content-based deduplication.

But that does not cover consumer redeliveries or retries after the window, so each consumer is idempotent, for example billing stores processed event ids in the same transaction as the charge. Shipping only needs physical goods, so its subscription has a filter policy on message attributes, and analytics takes everything.

FIFO has lower throughput limits, higher with high throughput mode, so I would check current quotas, and any consumer that does not need ordering, like analytics, can use a standard queue if the topic supports it. Each queue has its own dead-letter queue with alerts.

And if teams start asking to replay history, I would look at FIFO topic archiving or a log like Kafka.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
AWS documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "An order service publishes OrderPlaced events that billing, shipping and analytics must...": https://scrivio-interview-library.netlify.app/spring-boot/aws/a5c6959dc4/