Event-driven architecture
In most systems, one component tells another what to do. In an event-driven system, components announce what happened ("order placed", "payment taken") and anyone interested reacts. That one change gives extraordinary scalability, responsiveness and ease of adding new behaviour, and makes the overall workflow harder to see, test and recover when something fails. This module covers both topologies, how events differ from messages, and the event design mistakes that hurt most.
1. The idea
Events vs messages
| Event | Message (command) | |
|---|---|---|
| Meaning | Something happened: OrderPlaced | Please do something: ChargeCard |
| Tense | Past | Imperative |
| Receivers | Any number; the sender doesn't know them | One intended receiver |
| Usual channel | A topic (publish/subscribe) | A queue (point to point) |
Two topologies
Broker topology (choreography)
No coordinator. An initiating event goes to a topic; each event processor reacts, does its part and publishes a derived event ("payment taken"), which others react to in turn. Highly decoupled, fast and extensible; nobody owns the whole workflow, so errors and overall state are hard to manage.
Mediator topology (orchestration)
An event mediator receives the initiating event and directs the steps, sending commands to processors in order and handling their replies and failures. Better control, error handling and visibility; more coupling to the mediator, and it can become a bottleneck.
How it rates
| Characteristic | Event-driven | Service-based |
|---|---|---|
| Partitioning | technical | domain |
| Quanta | 1 to many | 1 to many |
| Cost | ★★★★★ | ★★★★★ |
| Simplicity | ★★★★★ | ★★★★★ |
| Scalability | ★★★★★ | ★★★★★ |
| Elasticity | ★★★★★ | ★★★★★ |
| Fault tolerance | ★★★★★ | ★★★★★ |
| Evolvability | ★★★★★ | ★★★★★ |
| Deployability | ★★★★★ | ★★★★★ |
| Testability | ★★★★★ | ★★★★★ |
| Performance | ★★★★★ | ★★★★★ |
Our qualitative ratings, 1 to 5 stars, more is better (for cost, more stars = cheaper). They compare styles in general; your context can move any of them.
2. How it works
Broker topology: everyone reacts
Mediator topology: someone in charge
The same order, with an event mediator. Watch the same payment failure handled.
3. Try it yourself
Anemic or full events?
An anemic event carries only an ID (OrderPlaced {id: 81}), so every consumer calls back to fetch the details. A full event carries the data consumers need. Set the load and payload and see the cost.
Assumes an ID-only event is about 0.1 KB, key fields (customer, items, total, address) about 1 KB, and the full order with history about 8 KB; with key fields, a quarter of consumers still need one call back for something else.
4. Trade-offs
Broker vs mediator
Broker topology wins on scalability, responsiveness and extensibility; mediator topology wins on workflow control, error handling and seeing where a request is. Many systems use a broker for simple fan-out and a mediator for complex, failure-sensitive workflows.
Thin vs fat events
Thin events keep contracts small but cause callbacks and couple consumers to the producer's API. Fat events avoid callbacks but couple consumers to a larger schema and cost bandwidth. Carry what most consumers need.
Responsiveness vs consistency
Replying as soon as the event is published is fast, but the rest of the system catches up later. Users may briefly see an order without its payment status.
Extensibility vs testability
Adding a consumer needs no producer change, which also means no single place shows the whole flow. Testing needs contract tests per event and end-to-end tests of the main paths.
5. In real projects
The swarm of gnats. Teams new to events often publish one event for every field change: OrderAddressChanged, OrderPhoneChanged, OrderNoteChanged. Consumers subscribe to dozens of tiny events, ordering between them gets hard, and the meaning of a business change is lost. Publish events at the level of business facts (OrderAmended with what changed).
Plan for errors from day one. In broker topology, a failed processor can't ask a coordinator what to do. A common answer is a workflow event pattern: the failing processor publishes the event to an error channel, a separate workflow processor (or a person) repairs it, and resubmits it, while the original processor carries on with other events.
Don't lose events. Use persistent topics, acknowledge only after processing, make consumers idempotent (they will see duplicates), and publish events together with the database change (the transactional outbox, Module 20).
Request/reply still exists. When a caller truly needs an answer, event-driven systems use a reply queue and a correlation ID: the caller waits on its own queue for the reply with its ID.
Say it in the interview
- "Event vs message?" An event says something happened and goes to anyone interested; a message is a command to one receiver. Events decouple the sender from who reacts.
- "Broker or mediator topology?" Broker for simple fan-out where extensibility and speed matter; mediator when the workflow has ordered steps and failures that need coordinated handling.
- "What's an anemic event?" One that carries only an ID, so every consumer calls back for the data. It multiplies load on the producer and couples consumers to its API.
- No answer to "what happens when a consumer fails?"
- Events named as commands (
SendEmailon a topic). - Assuming events arrive exactly once and in order.
6. Remember it
How this hook works: what "shout what happened" means
What the slogan means, phrase by phrase
- "Shout"
- Publish to a topic, not to a named receiver.
- "what happened"
- A past-tense business fact: OrderPlaced, PaymentTaken.
- "don't say what to do"
- The producer doesn't decide who reacts or how; that's what keeps it decoupled.
Why this picture fits: the newsroom works because the reporter announces news instead of assigning tasks; adding a new team never changes the reporter.
The story → the concept
| Newsroom | Event-driven system |
|---|---|
| "Fire at the docks!" | Initiating event |
| Photographer, editor, web team | Event processors |
| "Photos are in" | Derived event |
| The news editor directing coverage | Mediator topology |
The trap it saves you from
Building a relay race on a message broker: commands passed along a chain, with all of the coupling and none of the decoupling.
7. Common mistakes
8. Design exercise (30 minutes)
- Draw it both ways (15 min). Take an order workflow: place, pay, reserve, ship, notify. Draw it as broker topology with derived events, then as mediator topology.
- Break it (10 min). In each drawing, make payment fail. Write exactly what happens to the reserved stock and the customer.
- Design one event (5 min). Write the fields of
OrderPlacedso that most consumers never need to call back.
9. Self-check quiz
In broker topology, how do you add a new loyalty-points feature that reacts to orders?
A workflow has strict ordering and needs coordinated undo on failure. Which topology?
What's the main cost of ID-only (anemic) events?
Which name is a well-formed event?
Brokers can deliver an event twice. What must consumers do?
10. Go deeper
| Resource | Time | Why |
|---|---|---|
| What do you mean by "Event-Driven"? (Martin Fowler) | 20 min | Four different things people mean by event-driven, from notification to event sourcing. |
| Fundamentals of Software Architecture, 2nd edition (Richards and Ford) | book | The event-driven chapter: broker and mediator topologies, error handling, data loss. |
| Enterprise Integration Patterns (Hohpe and Woolf) | browse | The classic catalogue of messaging patterns behind event-driven systems. |