System Design blueprint · E-Commerce
Event-driven order processing
A message broker decouples services so each one scales, fails and retries on its own.
- Steps
- 5
- Subjective questions
- 32
- Objective questions
- 32
The problem and the outcome
Problem: A slow email or payment call should never block the checkout.
Outcome: Checkout replies at once while each team's service scales and retries alone.
Architecture
How it works, step by step
- The order service saves the order, publishes an OrderPlaced event and replies at once.
- The broker stores the event durably; producers and consumers never call each other directly.
- Each consumer group reads the event at its own pace and scales on its own.
- Consumers are idempotent, so an event delivered twice (at-least-once delivery) does no harm.
- A message that keeps failing moves to a dead-letter queue to inspect and replay.
Tech stack
- Apache Kafka, Amazon SQS/SNS or RabbitMQ
- Outbox table + CDC (Debezium)
- Schema registry (Avro / Protobuf)
- Dead-letter queues
Key design decisions
- Publish an OrderPlaced event instead of calling payment, inventory and email directly, so each consumer scales and fails on its own.
- The transactional outbox: write the order and the event in one database transaction, then relay it, so an event is never lost or sent for a rolled-back order.
- Partition by order ID, so all events for one order are processed in order.
Common mistakes
- Consumers that are not idempotent, so a redelivered message charges or ships twice.
- Dual writes (save to the database, then publish) that drift apart when the second step fails.
- No dead-letter queue, so one bad message blocks a partition forever.
What to measure
- Consumer lag per partition
- End-to-end event latency
- Dead-letter queue depth
Most-asked interview questions
Subjective questions 32
1How do you get exactly-once processing with a message broker?Asked almost every time
In practice you build effectively-once: the broker delivers at least once, and consumers are idempotent. Store processed message IDs (or use a natural idempotency key) and skip duplicates, ideally in the same transaction as the side effect. Kafka's transactions give exactly-once within Kafka, but external side effects still need idempotency.
2What is the transactional outbox pattern and why is it needed?Asked almost every time
Writing to the database and publishing to a broker are two systems, so one can succeed while the other fails. The outbox writes the event into a table in the same transaction as the business change; a relay (polling or CDC) then publishes it. Events are never lost and never sent for a change that did not commit.
3What are the benefits and costs of an event-driven architecture?Asked almost every time
Benefits: services are loosely coupled, can scale and fail independently, new consumers can be added without changing producers, and work is absorbed by queues during spikes. Costs: eventual consistency, harder debugging across services, duplicate and out-of-order messages, and the need for good observability and schema management.
4Events vs. commands: what is the difference?Asked almost every time
A command asks a specific service to do something (ChargePayment) and expects it to be handled once. An event announces something that already happened (OrderPlaced) and any number of consumers may react. Naming and ownership follow from that: commands go to one owner, events are broadcast.
5How do you make consumers idempotent?Asked almost every time
Give every event a unique ID, record processed IDs (in the same transaction as the side effect when possible), and skip duplicates. Design updates to be naturally idempotent (set status to PAID rather than add amount) and use idempotency keys for external calls.
6Choreography vs. orchestration in a saga?Asked almost every time
In choreography, each service listens for events and emits the next one; there is no central controller, which is decoupled but hard to follow. In orchestration, a coordinator (like a workflow engine) tells each service what to do and handles compensation, which is easier to reason about and monitor.
7How would you run a multi-step order flow without distributed transactions?Very often asked
Use a saga: each step is a local transaction that emits an event, and each has a compensating action (refund the payment, release the stock) if a later step fails. Choreography lets services react to each other's events; orchestration uses one coordinator that tracks the steps.
8How do you evolve event schemas without breaking consumers?Very often asked
Use a schema registry with Avro, Protobuf or JSON Schema, make backward-compatible changes (add optional fields, never rename or change types), version event types for breaking changes, and let consumers ignore unknown fields.
9How do you handle out-of-order events?Very often asked
Partition by entity so one entity's events stay ordered, include a sequence number or version in events, and have consumers ignore events older than the state they already applied, or buffer briefly to reorder.
10What is event sourcing?Very often asked
Storing the sequence of events as the source of truth instead of current state. State is rebuilt by replaying events, giving a complete audit history and time travel, at the cost of complexity, snapshotting for performance and careful schema evolution.
11What is CQRS and how does it relate to events?Very often asked
Command Query Responsibility Segregation separates the write model from read models. Events from the write side update one or more read models (projections) optimised for queries, which are eventually consistent.
12How do you debug a flow that spans many services?Very often asked
Propagate a correlation or trace ID in every event's headers, use distributed tracing (OpenTelemetry), log structured events with IDs, and build a timeline view per order from the event log.
13Kafka vs. RabbitMQ vs. SQS: how do you choose?Very often asked
Kafka is a durable, partitioned log with replay and high throughput, suited to event streaming and many consumer groups. RabbitMQ is a flexible message broker with routing and per-message acknowledgements. SQS is a fully managed queue with minimal operations. Choose by replay needs, ordering, throughput and operational capacity.
14How do you handle a poison message?Very often asked
After a limited number of retries with backoff, move it to a dead-letter queue with the error details, alert, and let the consumer continue. Fix the bug or data, then replay the DLQ messages.
15How do you handle a consumer that needs data from an event it never received?Very often asked
Either make events carry enough state (event-carried state transfer) so consumers do not need to call back, or let the consumer query the owning service's API on demand. For new consumers, backfill by replaying the log or loading a snapshot before consuming live events.
16How do you design events so other teams can discover and trust them?Very often asked
Publish schemas in a registry with owners, versions and examples, use a consistent envelope (event ID, type, source, time, correlation ID), document guarantees such as ordering keys and retention, and treat event contracts like public APIs with deprecation periods.
17How do consumer groups scale processing in Kafka?Often asked
Each partition is consumed by one member of a group, so adding consumers up to the number of partitions increases parallelism. More partitions allow more parallelism but add overhead, and ordering holds only within a partition.
18What is at-least-once vs. at-most-once delivery?Often asked
At-least-once acknowledges after processing, so crashes cause redelivery and duplicates. At-most-once acknowledges before processing, so crashes lose messages. Most systems choose at-least-once with idempotent consumers.
19How do you replay events to rebuild a read model?Often asked
Reset the consumer group's offset (or create a new group) and reprocess from the beginning of the retained log into a fresh store, then switch reads to it. This requires long enough retention or a compacted topic.
20What is a compacted topic?Often asked
A Kafka topic that keeps at least the latest value for each key, removing older values over time. It works like a changelog table, useful for state that consumers need in full, such as current prices.
21How do you prevent a slow consumer from affecting others?Often asked
Give each consumer its own queue or consumer group, size it independently, and monitor lag per consumer. A slow consumer only builds up its own backlog.
22How do you guarantee an order is not shipped before payment?Often asked
Make the shipping service react to a PaymentCaptured event rather than OrderPlaced, or have an orchestrator call shipping only after the payment step succeeds; enforce state transitions in the order service.
23What is the inbox pattern?Often asked
Consumers store incoming message IDs in an inbox table in the same transaction as their state changes, so redelivered messages are detected and skipped, complementing the outbox pattern on the producer side.
24What is the difference between a domain event and an integration event?Often asked
A domain event records something meaningful inside one service's model and may carry internal detail. An integration event is the stable, public version published for other services, with only the fields they need, so internal refactors do not break consumers.
25How do you handle a downstream service that is down for an hour?Often asked
Messages wait in the broker, so the consumer catches up when it returns. Size retention to cover the outage, make catch-up idempotent, watch lag, and consider extra consumers to drain the backlog faster once it recovers.
26How do you avoid an event loop between two services?Often asked
Never react to your own published events, mark events with their source and a hop count or correlation ID, make handlers idempotent, and design flows as a directed graph so an update in service A does not trigger an update back in A through B.
27When would you choose a stream processor such as Kafka Streams or Flink?Often asked
When you need stateful work over events, such as joins, windows, aggregates or deduplication, with exactly-once state updates and fault-tolerant checkpoints, rather than handling each message independently in a simple consumer.
28How do you monitor an event-driven system?Sometimes asked
Track consumer lag, processing latency, error and retry rates, DLQ size, throughput per topic, and end-to-end time from event to effect for key flows, with alerts on growing lag.
29When is event-driven design overkill?Sometimes asked
For simple CRUD apps with one team, low scale and strong consistency needs. A modular monolith with a database transaction is simpler until there is a real need to decouple.
30How do you test event-driven services?Sometimes asked
Unit-test handlers with sample events, use contract tests for event schemas between producers and consumers, run integration tests with a real broker in containers, and test duplicate and out-of-order delivery explicitly.
31What belongs in an event payload?Sometimes asked
The event type, a unique ID, the entity ID, a timestamp, a version, and the data consumers need (or a reference to fetch it). Avoid huge payloads and sensitive data that consumers do not need.
32How do you keep personal data out of long-lived event logs?Sometimes asked
Publish references instead of personal fields, encrypt sensitive fields with per-user keys so deleting the key erases them (crypto-shredding), and set retention or compaction so data does not live longer than policy allows.
Objective questions 32
1Which delivery guarantee do most brokers give by default?Asked almost every time
Why: Messages are redelivered until acknowledged, so duplicates are possible and consumers must be idempotent.
2An event describes…Asked almost every time
Why: Commands ask; events announce.
3How should duplicate events be handled?Asked almost every time
Why: At-least-once delivery creates duplicates.
4Which saga style uses a central coordinator?Asked almost every time
Why: The coordinator handles compensation.
5In Kafka, what preserves ordering for one order's events?Very often asked
Why: Ordering is guaranteed only within a partition, and the key decides the partition.
6Where does a message go after repeatedly failing processing?Very often asked
Why: A dead-letter queue parks poison messages so the rest of the stream keeps flowing and someone can inspect them.
7Which change to an event schema is backward compatible?Very often asked
Why: Old consumers ignore new optional fields.
8Event sourcing stores…Very often asked
Why: State is rebuilt by replay.
9What does CQRS separate?Very often asked
Why: Reads use projections.
10What carries one flow's identity across services?Very often asked
Why: Enables distributed tracing.
11Which broker keeps a replayable log for many consumer groups?Very often asked
Why: Retention allows replay.
12What limits parallelism in a Kafka consumer group?Very often asked
Why: One consumer per partition per group.
13Event-carried state transfer means…Very often asked
Why: Consumers can act without a synchronous call to the owner.
14An integration event differs from a domain event because it is…Very often asked
Why: Public events shield consumers from internal model changes.
15At-most-once delivery risks…Often asked
Why: Acknowledged before processing.
16A compacted topic keeps…Often asked
Why: Works like a changelog table.
17Which pattern detects redelivered messages on the consumer side?Often asked
Why: Stores processed IDs.
18How do you rebuild a read model?Often asked
Why: Needs enough retention.
19When should shipping start in an order flow?Often asked
Why: React to the right event.
20What should a poison message eventually go to?Often asked
Why: So other messages keep flowing.
21Ordering in Kafka is guaranteed…Often asked
Why: Partition by entity key.
22Which metric shows a consumer falling behind?Often asked
Why: Alert when lag keeps growing.
23Where should event schemas be published?Often asked
Why: Shared schemas let teams discover and validate events.
24What happens to events while a consumer is down, if retention is long enough?Often asked
Why: Durable brokers decouple producer and consumer uptime.
25Which tool fits windowed joins and aggregates over events?Often asked
Why: Stream processors manage state and checkpoints for you.
26Crypto-shredding removes personal data by…Often asked
Why: Encrypted fields become unreadable once the key is gone.
27Event-driven design is usually overkill for…Sometimes asked
Why: A transaction is simpler.
28Which test checks producers and consumers agree on schemas?Sometimes asked
Why: Catch breaking changes early.
29Which managed AWS queue needs minimal operations?Sometimes asked
Why: Fully managed by AWS.
30Which pattern publishes events reliably from a DB transaction?Sometimes asked
Why: Events are stored with the change.
31Events should avoid carrying…Sometimes asked
Why: Share only what is needed.
32What helps prevent infinite event loops between services?Sometimes asked
Why: Knowing an event's origin stops it bouncing back.
Track your progress through every blueprint in your dashboard.
Start learning