Event-Driven Architecture Principles
On this page
What is event-driven architecture?
Day 45's pub/sub pattern solved one handler. Event-driven architecture (EDA) applies the idea across a whole system: services communicate mainly by publishing events about changes in their own domain, and other services react. The order service doesn't call shipping; it announces OrderPaid, and shipping decides what that means for it.
The result is services that can be built, deployed and scaled independently, and a system that's easy to extend with new reactions. The price is that the overall flow is no longer written down in one place.
Event notification vs event-carried state transfer
Two styles, and the choice matters:
- Event notification: a thin message,
OrderPaid { order_id: 991 }. Subscribers call back to the order service for details. Small and stable, but every subscriber adds load to the publisher, and the data may have changed by the time they ask. - Event-carried state transfer: the event includes what subscribers need,
OrderPaid { order_id, customer_id, items, total, address }. Subscribers don't call back and can keep their own copy. Larger messages, and the event's shape becomes a contract that's harder to change.
Most teams use notification events for simple triggers and state-carrying events where subscribers would otherwise all call back.
State-carrying events have a trap: they can arrive out of order. If CustomerUpdated version 8 is redelivered after version 9, a subscriber that blindly copies the payload overwrites the newer address with the older one. Put a version or sequence number in every event, and have subscribers ignore any event older than what they've already stored:
UPDATE customers_copy SET address = @address, version = @version
WHERE id = @id AND version < @version; -- an older event changes nothingChoreography vs orchestration
When a business process spans several services, someone has to keep it moving.
Choreography: no coordinator. Each service listens for events and emits its own. OrderPlaced → inventory reserves stock and emits StockReserved → payment charges and emits PaymentCaptured → shipping creates a shipment. Loosely coupled and easy to extend, but no single place shows the whole process, and cycles or gaps are easy to create.
Orchestration: a coordinator, often a workflow or saga orchestrator, tells each service what to do and waits for the result. The process is explicit in one place and easy to monitor ("order 991 is waiting on payment"). The orchestrator knows about every participant, so it's more coupled.
| Choreography | Orchestration | |
|---|---|---|
| Where the flow lives | Spread across services | In the orchestrator |
| Adding a step | Add a subscriber | Change the orchestrator |
| Seeing a stuck process | Requires tracing across services | Visible in the orchestrator's state |
| Good for | Simple, short chains; broadcasting facts | Long, multi-step business processes with compensation |
A practical rule: choreography for "these things should happen when X occurs", orchestration for "this process has steps, order and failure handling". Tools like Temporal, AWS Step Functions and Azure Durable Functions make orchestration durable across crashes. Day 49 applies both to sagas.
Keeping an event-driven system understandable
- Name events as business facts in the past tense:
OrderPaid, notUpdateOrderTable. - Publish a catalog of events, their owners, schemas and subscribers.
- Version schemas and use a schema registry for important streams, so producers can't break consumers silently. A standard envelope, such as the CNCF CloudEvents spec, gives every event the same id, type, source and time fields.
- Watch for loops. Service A reacts to B's event by emitting one B reacts to, and the two ping-pong forever. Each service should react only to facts it doesn't produce itself, and traces make loops visible quickly.
- Trace with correlation ids carried on every event (Day 88).
- Start small: a few well-named events for the most important processes, not every table change.
Accept eventual consistency
Different services react at different times. For a moment, the order is paid but shipping hasn't heard yet. Design screens and APIs around that: "Payment received, preparing your shipment" is honest and usually fine.
The Event-Driven Architecture course goes deeper into event design, event sourcing and CQRS.
Check yourself
An older event was delivered after a newer one, and the subscriber applied it anyway. Carry a version in each event and ignore events older than the stored version.
For a multi-step process with ordering and failure handling, such as fulfillment with compensations, where seeing and controlling the whole flow in one place matters.
Visibility into the choreography: an event catalog that lists every event with its publishers and subscribers, and distributed tracing that follows one order across services. In event-driven systems the flow lives in subscriptions rather than in one place in code, so it must be documented and observable.
An event notification says something happened, like OrderPaid { orderId }, and consumers call back for details. Event-carried state transfer includes the data consumers need, so they can keep their own copy without calling back. The first is simpler; the second reduces runtime coupling.
Show the user's own change immediately, for example with an optimistic update or a "processing" state, and update when the backend confirms. Don't promise the change is visible everywhere instantly; design screens around the delay instead of hiding it.