Decoupling with events
Moving from request-response to events without losing data or ordering.
When you finish, you should be able to say
Explain how a write and its event stay consistent, and what ordering a consumer can rely on.
The route
6 blocks (2 fundamental, 3 senior, 1 staff), then the practice problem.
- Decouple producer from consumerFundamental
- Fan one event out to many consumersFundamental
- Keep events as a replayable logSenior
- Publish events reliably with the writeSenior
- React to database changesSenior
- Coordinate a multi-service transactionStaff
- Notification System
Next, step 1 adds Intake queue, Channel queues (outlined). A box fills in when you tick “I can explain this” on the block that introduces it; plain boxes come from the problem itself.
The simplest decoupling: a producer that does not wait for the consumer.
The request path waits on slow work (email, resize, analytics) or on a service that may be down.
Say it like this: The request only needs the write to be durable, not the email to be sent, so I enqueue a job and return 202; workers drain the queue at their own pace and a slow provider never blocks the API.
Adds to the design: Intake queue, Channel queues
Check yourself: How does the client find out the work is done?
When several consumers need the same event, and the producer should not know about any of them.
Several systems need to react to the same event, and the producer should not know about them.
Say it like this: The post service should not know who cares about a new post, so it publishes one event to a topic and the fan-out, search and analytics consumers each subscribe independently.
Adds to the design: Status topic
Check yourself: When would you use a queue instead of a topic?
When there is exactly one kind of consumer and the point is to share work across workers, such as a transcoding job queue. A topic is for broadcast to independent consumers. In practice you often have a topic feeding several queues, one per consumer type.
A queue you can rewind; the model behind Kafka and the ordering guarantee it gives per partition.
Consumers need to re-read history, or new consumers need to catch up from the start.
Say it like this: I need consumers to replay history and new consumers to catch up, so instead of a queue that deletes on ack I keep an append-only log partitioned by key, and each consumer owns its offset.
Adds to the design: Status topic
Check yourself: How do you pick the partition key?
By the entity whose events must stay ordered and co-located: conversation id for chat, author id for posts, region for locations. Check its cardinality and skew first; a key with a few giant values gives hot partitions no matter how many you create.
The write and the event must both happen or neither; this is how you get that without a distributed transaction.
Writing to the database and publishing to Kafka are two steps; one can succeed without the other.
Say it like this: A database commit and a Kafka publish cannot share a transaction, so I write the event into an outbox table in the same transaction as the row and let a relay publish it; the event is then exactly as durable as the data.
Adds to the design: Notification DB, Outbox relay
Check yourself: Why not use a distributed transaction across the database and Kafka?
Two-phase commit across heterogeneous systems is slow, blocks while the coordinator is down, and Kafka transactions do not participate in XA anyway. The outbox gets the same guarantee with one local transaction and an idempotent relay.
Or let the database’s own log produce the events, and know when that is the better choice.
Other systems (search, cache, analytics) must stay in sync with the database without the app publishing for each one.
Say it like this: The search index and the cache must follow the database without every service remembering to publish, so I tail the write-ahead log with change data capture and emit each committed row change as an event.
Adds to the design: Preference DB
Check yourself: When would you use CDC instead of the outbox pattern?
When one business action spans several services, the sequence-with-compensation that replaces a transaction.
An order touches payment, inventory and shipping services; no single transaction spans them.
Say it like this: Payment, inventory and shipping each own their data, so instead of a distributed transaction I run a saga: local transactions in sequence, each with a compensating step, so a failure anywhere unwinds what already happened.
Adds to the design: Order saga
Check yourself: How is a saga different from a distributed transaction?
A distributed transaction holds locks across all participants until a coordinator commits, giving atomicity and isolation at the cost of availability. A saga commits each step locally and relies on compensation for failure, so intermediate states are visible but nothing blocks.
Putting it together
How the blocks above combine into one design for Notification System. The numbers on the boxes are the steps that introduce each part.
The notification pipeline, assembled from the path Each number is a step of this path; a box with two numbers is shaped by both blocks. The ring colour is the block's category, as on the route map. Boxes without a number come from the problem itself. Point at a step below, or at a bubble, to see where it appears.
A producer submits a notification; the API commits the row and an outbox row together, and a relay feeds the intake queue that decouples the router and the per-channel senders. Every delivery state change is published once to a status topic that several consumers read.
How each block shows up in the design
- 1. Decouple producer from consumer: the intake queue lets the API return 202 in milliseconds and absorbs a 33K/s campaign burst, and six channel-by-priority queues mean a Twilio outage fills the SMS queues while push and email keep flowing.
- 2. Fan one event out to many consumers: every delivery state change is published once to a status topic, and dashboards, the status API and producer services each subscribe to it without the senders knowing who listens.
- 3. Keep events as a replayable log: the status topic is kept as a retained log, so a new dashboard or a consumer that shipped a bug can replay past state changes instead of querying the notification database.
- 4. Publish events reliably with the write: the API writes the notification row and an outbox row in one transaction, and a relay publishes committed rows to the intake queue, so a crash between the insert and the publish can never strand a queued notification.
- 5. React to database changes: changes to the preference table stream to the router through change data capture, so an opt-out, or an unsubscribe recorded by the webhook handler, reaches the router's preference cache within seconds without every writer publishing an event.
- 6. Coordinate a multi-service transaction: an order saga is a typical producer, and each step and each compensation, such as a refund after a failed shipment, submits its own notification with an idempotency key naming that step, so a retried step never notifies twice.
- ★
Notification System
Now run the timed practice. The blocks above should appear in your answer without prompting; if one does not, that is the one to reread.
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon Aurora | Preference DB, Notification DB, Postgres | AlloyDB for PostgreSQL, Cloud SQL | Azure Database for PostgreSQL or MySQL | PostgreSQL, MySQL, CockroachDB |
| AWS Step Functions | Order saga, Saga orchestrator | Workflows | Logic Apps, Durable Functions | Temporal, Apache Airflow |
| Amazon EC2 Auto Scaling | Notification API, Router workers, Outbox relay, Channel senders, API, Workers, Post service, Fan-out workers, Search indexer, Notification service, Driver gateway, Location service, Relay, Debezium, Cache invalidator, Analytics loader, Driver lock service, Trip service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon SQS | Intake queue, Channel queues, Queue | Pub/Sub, Cloud Tasks | Service Bus queues, Queue Storage | RabbitMQ, ActiveMQ |
| Amazon Kinesis Data Streams | Status topic | Pub/Sub | Event Hubs | Apache Kafka, Redpanda, Apache Pulsar |
| Amazon DynamoDB | Job status, Saga log | Firestore, Bigtable | Cosmos DB | ScyllaDB, Apache Cassandra |
| Amazon MSK (Kafka) | Topic: posts, Topic: driver-locations, Topic: notifications, Topic: posts-changes | Managed Service for Apache Kafka | Event Hubs (Kafka API) | Confluent Cloud, Redpanda |
| Amazon Redshift | Analytics sink | BigQuery | Synapse Analytics, Microsoft Fabric | ClickHouse, Snowflake, Apache Druid |
| Amazon S3 | Archive sink | Cloud Storage | Blob Storage | Cloudflare R2, MinIO, Ceph |
| Amazon SageMaker | ETA trainer | Vertex AI | Azure Machine Learning | KServe, Ray Serve, NVIDIA Triton |