How-To Guide

How to Implement Event-Driven Architecture

Design an event-driven system: publish facts, keep events immutable, decouple producers from consumers, and handle the eventual consistency.

On this page

  1. Introduction
  2. Publish facts, not commands
  3. Keep events immutable and versioned
  4. Decouple the producer from the consumers
  5. Design for the eventual consistency
  6. Handle the failures and the ordering
  7. Test the decoupling and the consistency
  8. Common pitfalls
  9. Conclusion

Introduction

An event-driven architecture is a system where services communicate by publishing and consuming events rather than by calling each other. A service that does something publishes a fact about what happened, and the services that care react to it. The producer does not know the consumers, so a new consumer is added without changing the producer, and a slow consumer does not block the producer. The cost is that the state is consistent only after the consumers process, which is the eventual consistency to design for.

This tutorial covers publishing facts rather than commands, keeping events immutable and versioned, decoupling the producer from the consumers, and handling the eventual consistency. The examples use a pub/sub channel, and the design applies to any broker.

Publish facts, not commands

An event describes what already happened, named in the past tense: OrderPlaced, PaymentCaptured, UserRegistered. It is a fact, and any service may react to it. A command, in contrast, tells a specific service to do something: PlaceOrder, CapturePayment. Publish events to decouple, because a fact can have many reactions the publisher did not anticipate, and send commands when a specific action is required.

The past-tense naming makes the distinction concrete and forces the design to be event-shaped. An event carries the data of what happened, enough for a consumer to react without calling back. A command carries the instruction and an expectation of one handler. A system that publishes commands and calls them events often still couples the producer to the consumer, which is the coupling events remove.

// an event: a fact about the past, any consumer may react
await broker.publish('orders', {
  id: 'evt-1', type: 'OrderPlaced', occurredAt: new Date().toISOString(),
  payload: { orderId: 42, customerId: 7, total: 100 },
});
// the publisher does not know which services react

Keep events immutable and versioned

An event is a fact, and a fact does not change after it happened, so an event is immutable once published. Do not edit or delete a published event; publish a new one that reflects the new fact. This immutability lets a consumer replay the events to rebuild its state, and it makes the event log an audit trail of what happened.

An event’s schema evolves, so version it. Adding an optional field is compatible, because the consumers that do not read it ignore it. Removing or changing a field is not, because a consumer that reads it breaks. Carry a version and let a consumer subscribe to the versions it understands, so a new version is published alongside the old during a migration rather than replacing it.

// version the event schema: a new field is compatible, a removed one is not
{ id: 'evt-1', type: 'OrderPlaced', version: 2,
  payload: { orderId: 42, customerId: 7, total: 100, currency: 'USD' } }

Decouple the producer from the consumers

The producer publishes the event without knowing the consumers, and a consumer subscribes to the types it cares about. This is the decoupling: a new consumer is added by subscribing, without a change to the producer, and a consumer’s failure does not block the producer, because the broker holds the events until the consumer processes them.

The producer must not encode the consumers’ needs into the event, or it becomes coupled to them. Publish the facts about what happened, and let each consumer derive what it needs, calling back to the producer’s API if it needs more. A producer that includes downstream-specific fields is coupled to those downstreams, which defeats the point.

Design for the eventual consistency

In an event-driven system, the states are consistent only after the consumers process the events, so a read may see a view that lags the write. The user places an order and the order appears immediately, but the inventory and the analytics update a moment later. Design the UX and the reads for that lag: show the confirmed state of the user’s own action, and let the downstream views catch up.

For a read that must be current, read the write model that the producer owns, not a downstream projection that lags. For a read that tolerates the lag, a projection is fine and often faster. The choice is per read, and stating which reads tolerate the lag is part of the design. A system that assumes immediate consistency everywhere will show stale data and confuse the user.

# the write is committed and the event published; the projections catch up
# a read of the user's own order: read the write model (current)
# a read of the analytics or the inventory: read the projection (eventually consistent)

Handle the failures and the ordering

A consumer can fail, so the broker must hold the event until it is processed, and the consumer must dedupe because the delivery is at least once. A consumer that fails repeatedly goes to a dead-letter area after the retries, so the event is not lost and the failure is visible. This is the same reliable consumption as in any message system.

Ordering matters per entity, not globally. The events for one order must be processed in order, so a partition keyed by the order id keeps them ordered, while different orders process in parallel. A global order is not achievable in a distributed system and is not needed; a per-key order is what the consumers rely on.

Test the decoupling and the consistency

Test that a consumer added for a new purpose reacts to the events without a change to the producer, which proves the decoupling. Test that a consumer failure does not block the producer, by failing the consumer and asserting the producer still commits and publishes. Test that the state is consistent after the consumers process, by asserting the projection matches the write model after the events are handled.

# publish an event and assert two independent consumers both react
# fail one consumer and assert the producer still commits and publishes
# then let the consumer recover and assert the projection catches up

Common pitfalls

Publishing commands and calling them events so the producer stays coupled, an event with downstream-specific fields so the producer is coupled to the consumers, editing a published event instead of publishing a new fact, assuming immediate consistency so a stale projection is served, and a global ordering expectation that is not achievable are the recurring mistakes. Past-tense facts, immutable versioned events, a producer that does not know the consumers, a designed-for lag, and a per-key ordering address them.

Conclusion

An event-driven architecture decouples services with facts. Publish past-tense events that any consumer may react to, keep them immutable and versioned, keep the producer unaware of the consumers, design the reads for the eventual consistency, and handle the failures and the per-key ordering. Self-check by adding a consumer without changing the producer and by asserting the projection catches up after a consumer failure. Then a new reaction is added without touching the publisher.

Sources: Martin Fowler: Event-driven architecture, Microsoft: Event-driven architecture style, Confluent: Event-driven microservices.

FAQ

Event versus command?
An event is a fact about the past, such as OrderPlaced, and any consumer may react; a command is a request to do something, such as PlaceOrder, and is sent to one handler. Publish events to decouple; send commands when a specific action is required.
Why is the consistency eventual?
The producer commits its change and publishes the event, and the consumers update their own state as they process it. Until every consumer has processed, the states differ, so a read may see an older view. The system is consistent after the lag, not at the moment of the write.

Related Examples