← Study paths

Decoupling with events

Moving from request-response to events without losing data or ordering.

6 blocks · about 31 min of reading · ends with Notification System

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.

  1. Decouple producer from consumerFundamental
  2. Fan one event out to many consumersFundamental
  3. Keep events as a replayable logSenior
  4. Publish events reliably with the writeSenior
  5. React to database changesSenior
  6. Coordinate a multi-service transactionStaff
  7. Notification System
Decoupling & async workFundamentalSeniorStaff
Read each block, then tick “I can explain this”: the path remembers where you are.Start with step 1
Your Notification System so far0 of 6 blocks in
Preference DBAmazon AuroraCDC streams everychangeOrder sagaAWS Step Functionseach step orcompensation notifiesNotification APIAmazon EC2 Auto Scalingvalidate, SET NXidempotency keyIntake queueAmazon SQSabsorbs the 33K/scampaign peakRouter workersAmazon EC2 Auto Scalingpreferences, quiethours, renderNotification DBAmazon Auroranotification andoutbox rows, one txnOutbox relayAmazon EC2 Auto Scalingpublishes committedrowsChannel queuesAmazon SQSpush, SMS, email bypriorityDashboards and callersSLO metrics, status updatesStatus topicAmazon Kinesis Data Streamsevery state change,retainedChannel sendersAmazon EC2 Auto ScalingAPNs and FCM, Twilio,SESStep 5: React to database changes5Step 6: Coordinate a multi-service transaction6Step 1: Decouple producer from consumer1Step 4: Publish events reliably with the write4Step 4: Publish events reliably with the write4Step 1: Decouple producer from consumer1Step 2: Fan one event out to many consumers2Step 3: Keep events as a replayable log3submitrow + outboxoutbox rowspublishnotification idper channelstate changessubscribeCDC changes

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.

Each block has a diagram. Open one beside its step, or all of them at once.
  1. Decouple producer from consumer

    Decoupling & async workfundamental~6 min

    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?

    Return 202 with a job id and a status endpoint the client can poll with backoff. For interactive flows push the result over WebSocket or a notification instead. Either way the job record is the source of truth, written by the worker when it finishes.

    Read this block →

  2. Fan one event out to many consumers

    Decoupling & async workfundamental~5 min

    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.

    Read this block →

  3. Keep events as a replayable log

    Decoupling & async worksenior~5 min

    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.

    Read this block →

  4. Publish events reliably with the write

    Decoupling & async worksenior~5 min

    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.

    Read this block →

  5. React to database changes

    Decoupling & async worksenior~5 min

    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 many consumers need to follow the tables as they are and the application should not know about them: search, cache, analytics, replicas. The outbox is better when consumers need a stable domain event and you want to control what is published; CDC can be the relay for the outbox table.

    Read this block →

  6. Coordinate a multi-service transaction

    Decoupling & async workstaff~5 min

    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.

    Read this block →

  7. 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
    Preference DBAmazon AuroraCDC streams everychangeOrder sagaAWS Step Functionseach step orcompensation notifiesNotification APIAmazon EC2 Auto Scalingvalidate, SET NXidempotency keyIntake queueAmazon SQSabsorbs the 33K/scampaign peakRouter workersAmazon EC2 Auto Scalingpreferences, quiethours, renderNotification DBAmazon Auroranotification andoutbox rows, one txnOutbox relayAmazon EC2 Auto Scalingpublishes committedrowsChannel queuesAmazon SQSpush, SMS, email bypriorityDashboards and callersSLO metrics, status updatesStatus topicAmazon Kinesis Data Streamsevery state change,retainedChannel sendersAmazon EC2 Auto ScalingAPNs and FCM, Twilio,SESStep 5: React to database changes5Step 6: Coordinate a multi-service transaction6Step 1: Decouple producer from consumer1Step 4: Publish events reliably with the write4Step 4: Publish events reliably with the write4Step 1: Decouple producer from consumer1Step 2: Fan one event out to many consumers2Step 3: Keep events as a replayable log3submitrow + outboxoutbox rowspublishnotification idper channelstate changessubscribeCDC changes

    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.

    1. Decouple producer from consumer
    2. Fan one event out to many consumers
    3. Keep events as a replayable log
    4. Publish events reliably with the write
    5. React to database changes
    6. Coordinate a multi-service transaction

    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.
  8. ★

    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.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon AuroraPreference DB, Notification DB, PostgresAlloyDB for PostgreSQL, Cloud SQLAzure Database for PostgreSQL or MySQLPostgreSQL, MySQL, CockroachDB
AWS Step FunctionsOrder saga, Saga orchestratorWorkflowsLogic Apps, Durable FunctionsTemporal, Apache Airflow
Amazon EC2 Auto ScalingNotification 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 serviceCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon SQSIntake queue, Channel queues, QueuePub/Sub, Cloud TasksService Bus queues, Queue StorageRabbitMQ, ActiveMQ
Amazon Kinesis Data StreamsStatus topicPub/SubEvent HubsApache Kafka, Redpanda, Apache Pulsar
Amazon DynamoDBJob status, Saga logFirestore, BigtableCosmos DBScyllaDB, Apache Cassandra
Amazon MSK (Kafka)Topic: posts, Topic: driver-locations, Topic: notifications, Topic: posts-changesManaged Service for Apache KafkaEvent Hubs (Kafka API)Confluent Cloud, Redpanda
Amazon RedshiftAnalytics sinkBigQuerySynapse Analytics, Microsoft FabricClickHouse, Snowflake, Apache Druid
Amazon S3Archive sinkCloud StorageBlob StorageCloudflare R2, MinIO, Ceph
Amazon SageMakerETA trainerVertex AIAzure Machine LearningKServe, Ray Serve, NVIDIA Triton