System design concepts · Doing Things Later

Event-Driven Architecture Explained: Events, Pub/Sub, Event Sourcing, CQRS

Instead of services asking each other what happened, they announce what happened and let interested parties react. Done well, it's the most scalable way to connect a large system. Done badly, it's a distributed mystery novel.

What you'll be able to explain
  • Distinguish commands, events and notifications
  • Choose between choreography and orchestration for multi-service flows
  • Design event contracts that evolve safely
  • Explain event sourcing and CQRS, and when to avoid them
  • Debug and operate an event-driven system

Why events?

In a request-driven design, the order service calls inventory, then payments, then notifications, then analytics. Each new consumer means changing the order service, and if any of them is down, ordering may fail.

In an event-driven design, the order service publishes OrderPlaced once. Inventory, payments, notifications and analytics each subscribe and react. Adding a new consumer (a loyalty-points service) needs no change to the producer.

SERVICEOrder serviceQUEUE / LOGEvent busOrderPlacedSERVICEInventorySERVICEPaymentsSERVICENotificationsWORKERAnalyticspublish once
Request-driven vs event-driven: the producer stops knowing about every consumer.

Gains: loose coupling, independent scaling, natural buffering of spikes, an audit trail of what happened, and easy addition of new consumers.

Costs: eventual consistency, harder debugging (no single call stack), duplicate and out-of-order delivery to handle, and the risk of an invisible web of dependencies.

Commands vs events

Command Event
Tense Imperative: "ChargeCard" Past: "PaymentCaptured"
Receivers One handler expected Zero to many subscribers
Coupling Sender knows the receiver Sender doesn't know who listens
Can be rejected? Yes No: it already happened

Event design styles:

  • Notification ("Order 881 changed"): tiny; consumers call back for details. Low coupling on schema, but consumers depend on the producer being up.
  • Event-carried state transfer ("Order 881 changed; here is the full order"): consumers keep local copies and don't need to call back. More data, much more resilience.
  • Delta events ("status changed from PAID to SHIPPED"): compact, but consumers must have seen every previous event to make sense of it.

Choreography vs orchestration

When a business process spans several services (place order → reserve stock → charge → ship), someone has to coordinate it.

Choreography Orchestration
How Each service reacts to events and emits its own A central orchestrator tells each service what to do next
Coupling Low; no central brain Services coupled to the orchestrator's commands
Visibility Hard: the flow is implicit in subscriptions Easy: the flow is explicit in one place
Changing the flow Touch several services Change the orchestrator
Best for Simple flows, broadcast reactions (analytics, notifications) Multi-step business processes with compensation (Background Jobs, Schedulers and Workflow Engines)
OrdersInventoryPaymentsShipping1OrderPlaced (event)2StockReserved (event)3PaymentCaptured (event)4ShipmentCreated (event)5who notices if PaymentCaptured never arrives?
Choreography: each service reacts to the previous event; nobody owns the whole flow.
Tip

A practical mix: orchestrate the core business process (checkout, onboarding) so its state is visible and compensations are explicit, and choreograph the side effects (emails, analytics, search indexing) that simply react to its events.