Skip to content

Lesson 5 of 9 · 28m

Event-Driven Design That Survives Production

Design events as durable business facts with clear ownership, ordering expectations, schema evolution, and failure semantics.

Event-driven architecture fails quietly before it fails loudly. At first every service is decoupled and beautiful. Six months later nobody knows who consumes UserUpdated, retries are duplicating emails, and the most important business invariant is encoded in consumer lag. Events are not a decoupling spell; they are public contracts about facts.

Events are facts, commands are intent

An event says something happened: InvoiceClosed, PaymentAuthorized, MembershipRevoked. A command asks for work: CloseInvoice, AuthorizePayment. Do not publish commands disguised as events unless you deliberately want other services to act as workers.

{
  "event_id": "evt_01JAH7ZK3J",
  "type": "billing.invoice_closed.v1",
  "occurred_at": "2026-11-30T17:22:10Z",
  "producer": "billing",
  "subject": {"invoice_id": "inv_10492", "organization_id": "org_388"},
  "data": {
    "amount_minor": 12900,
    "currency": "USD",
    "closed_by": "user_991"
  }
}

Use past-tense business language. If the event name cannot be explained to a product manager without mentioning a service, it is probably an implementation leak.

The outbox is not optional in serious systems

Writing state to a database and publishing an event are not atomic. If the process crashes between them, truth and notification diverge. The transactional outbox solves this by writing the event in the same database transaction as the state change, then publishing asynchronously.

BEGIN;
UPDATE invoices
SET status = 'closed', version = version + 1
WHERE invoice_id = 'inv_10492' AND version = 7;

INSERT INTO outbox_events (event_id, type, payload, occurred_at)
VALUES (
  'evt_01JAH7ZK3J',
  'billing.invoice_closed.v1',
  '{"invoice_id":"inv_10492","organization_id":"org_388"}',
  now()
);
COMMIT;

A relay publishes unpublished rows and marks them sent. This gives at-least-once publication. Consumers must therefore be idempotent; at-most-once is a fantasy unless the broker transaction genuinely includes the business write.

Design schema evolution like an API

Events outlive deploys. Consumers upgrade on different schedules. Include a type with a version, keep old fields stable, and document removal policy. Additive changes are safest.

billing.invoice_closed.v1
  invoice_id required string
  organization_id required string
  amount_minor required integer
  currency required ISO-4217 lowercase normalization at producer
  closed_at required RFC3339 timestamp
  reason optional string added 2026-08-14

Do not silently change units, meaning, nullability, or identifier format. amount_minor becoming dollars in the same field is not an evolution; it is an incident.

Ordering is narrower than people pretend

Global ordering is expensive and usually unnecessary. Most systems need ordering per aggregate or per partition. Put the aggregate or tenant key in the partition key, but understand what that does to hot tenants. If ordering matters across entities, ask whether the design hides a consistency boundary that should be synchronous.

Draw the loop before building it

Events create cycles easily: billing emits invoice closed, provisioning reacts, usage reacts, billing reacts again. A sequence diagram with retry paths exposes these loops better than boxes labeled "event bus." Also decide which flows are choreography and which need orchestration. Long-running multi-step business processes with compensation usually benefit from an explicit process manager.

Field notes

  • Do not put large payloads or secrets in events. Include enough context and references, then let consumers fetch authorized detail when necessary.
  • Publish stable event IDs and store consumer checkpoints with them; debugging without both is archaeology.
  • A dead-letter queue is not a trash can. Alert on it, preserve poison messages, and build a replay procedure.
  • Consumer lag is a business metric when it affects user-visible freshness. Measure it in seconds, not only message count.
  • Test schema compatibility in CI with consumer contracts before deploying the producer.

Was this lesson helpful?

Sign in to rate