LESSON 02 · Event entry

Accept an event without losing it

Authenticate producers, validate the request, persist intent, and queue work.

SYSTEM VIEW

The delivery path

Diagram view
Intents stored—
Publishes—
Unsent rows—

Watch a receipt get stored before it’s acknowledged and a refund’s notice survive two crashes, then see why each channel gets its own queue.

  1. The Product service submits receipt intent evt-4416: recipient, category, template version, parameters and send time. The Notification API authenticates the caller, checks schema and authorization, and assigns a stable notification ID, n-9300. An open endpoint would invite spam and costly provider calls.

    Event ID
    evt-4416
    Notification ID
    n-9300

    What changed

    • Intent evt-4416
    • Notification ID n-9300
  2. The Notification API writes n-9300 to Notification DB before it answers. Only then does the Product service get 202 Accepted, so a crash after the answer can’t lose the intent: a durable copy already exists.

    What changed

    • n-9300 in Notification DB
    • 202 Accepted, after the write
  3. A refund and its notice must not part ways. The Product service writes the refund and an outbox row for evt-4417 to Product DB in one transaction: both commit, or neither does. Product DB now holds 1 unsent row.

    Event ID
    evt-4417
    One commit
    refund + outbox row

    What changed

    • Product DB
    • Refund and outbox row, one commit
    • 1 unsent row
  4. The Product service dies before the notice is published. The outbox row committed with the refund, so Product DB still holds it: 1 unsent row for evt-4417, waiting for something to publish it.

    What changed

    • Product service crashes before publishing
    • Outbox row still in Product DB
  5. With the Product service still down, the Dispatcher finds the unsent row and publishes evt-4417, and the Notification API stores it as n-9301. Then the Dispatcher dies before marking the row sent, so Product DB still counts 1 unsent row.

    Published
    evt-4417, once
    Stored as
    n-9301

    What changed

    • Dispatcher
    • n-9301 in Notification DB
    • Dispatcher crashes before marking sent
  6. Restarted, the Dispatcher still sees the unsent row and publishes evt-4417 again. Notification DB already holds that event ID, so the Notification API drops the copy. The Dispatcher marks the row sent: 2 publishes, still 2 intents.

    Published
    evt-4417, twice
    Stored
    n-9301, once

    What changed

    • Dispatcher restarted
    • Second copy of evt-4417
    • Outbox row marked sent
  7. Sent rows stay in Product DB until something purges them, and the Dispatcher is one more process to run. A disposable notice such as “your friend is online” may not justify that, so it goes straight to the Notification API.

    To purge
    sent outbox rows
    To run
    a dispatcher

    What changed

    • Product service back up
    • Sent rows wait to be purged
    • Dispatcher, one more job to run
    • Friend online, without an outbox
  8. The Notification API queues work per channel, each with its own workers. By channel: the SMS provider is down, so only SMS backs up and email keeps flowing. One shared: email waits behind stuck SMS work. Queues still need monitoring and backpressure; queued isn’t delivered.

    What changed

    • Queues: per channel or one shared
    • Workers: per channel or shared
    Queues

Space plays · ← → step · Home resets

THE IDEA

How it works

A trusted product service submits a notification intent: a record of what should be sent to whom, separate from any delivery attempt. It carries an event ID, recipient, category, template version, parameters, and requested send time. The notification API authenticates that producer, checks schema and authorization, and assigns a stable notification ID. A public unauthenticated send endpoint would invite spam and costly provider calls.

Persist the intent before acknowledging acceptance. When a product change and its notification must move together, use a transactional outbox: a row describing the message, committed in the same database transaction as the change. A dispatcher then publishes outbox rows to durable work queues. Debezium, for example, can read such rows from the Postgres change log and publish them to Kafka.

Follow a refund. The refund and outbox rows commit together. The process dies before publishing. The dispatcher finds the unpublished row and publishes it, then dies before marking it sent, so after restarting it publishes again. The stable event ID lets the notification service discard the second copy. The cost is a table to purge and a dispatcher to run, which a disposable message such as “your friend is online” may not justify.

Queues buffer bursts and let workers scale independently. Split work by channel or priority so an SMS provider outage does not block email, and reserve capacity for urgent transactional events. A queue still needs retention, monitoring, and backpressure, a way to slow producers when workers fall behind; it is not itself proof of delivery.

DECISION POINT

An order commits, but the process crashes before it publishes the receipt event. How can the system avoid losing the receipt?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Acknowledge only after the send intent has a durable recovery path.
  • Separate channel and priority workloads so one slow provider does not stall every notification.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

Why persist a send intent before acknowledging it?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.