Microservice Patterns
Event-Driven Architecture
Build systems where services announce facts as events and others react. Learn the difference between event notification and event-carried state transfer, and what each costs.
What you will learn
- Define events as immutable facts and design their names and payloads.
- Compare event notification with event-carried state transfer.
- See how callbacks re-introduce runtime coupling.
- Plan for eventual consistency, ordering and schema evolution.
Before you start
Overview
In a newsroom, reporters do not phone every editor when something happens — they file a story to the wire, and each desk decides what to do with it. Services in an event-driven architecture work the same way: they announce what happened and let others react.
An event is an immutable fact in the past tense: OrderPlaced, PaymentCaptured, ParcelShipped. The service that owns the data publishes it; any number of services subscribe and update their own data or trigger their own work. No service tells another what to do.
Two ways to design the payload
| Event notification | Event-carried state transfer | |
|---|---|---|
| Payload | Just ids: { orderId: 42 } | The data consumers need: items, address, totals |
| Consumers | Call the producer's API back for details | Act on the event alone, often keeping a local copy |
| Coupling | Runtime coupling returns through the callbacks | Consumers keep working when the producer is down |
| Cost | Small events; load on the producer's API | Bigger events; data duplicated; schema is a contract |
Run it
The Orders API is down from t = 10 s to t = 20 s. With event notification (A), Shipping and Invoicing receive the event but cannot handle it — and every event costs two API calls even when everything is healthy. With state transfer (B) the API stays idle and the outage does not matter.
Designing good events
- Name events as business facts in the past tense, owned by one service. Avoid commands disguised as events (
SendEmail). - Include a unique event id, the entity id (as the message key, for ordering), a timestamp and a schema version.
- Carry the data most consumers need, but not the entire aggregate; private fields stay private.
- Evolve schemas additively; never reuse a field with a new meaning.
{
"specversion": "1.0",
"id": "6f1d9c2e-8b0a-4b8e-9a51-1f2f7f4b3c11",
"source": "/orders",
"type": "com.shop.order.placed.v1",
"subject": "order-42",
"time": "2026-11-18T10:15:00Z",
"data": {
"orderId": "order-42",
"customerId": "c-7",
"items": [{ "sku": "A-100", "qty": 2, "price": 12.50 }],
"shippingAddress": { "city": "Hanoi", "line1": "12 Tran Hung Dao" },
"total": 25.00
}
}Advanced Event-driven systems are eventually consistent: a customer may see their order before Shipping knows about it. Design UIs for that ("processing"), use the entity id as the message key so per-entity order holds, and publish events through a Transactional Outbox so they are never lost. For flows that must complete or roll back, see Saga.
Trade-offs
| Benefits | Costs | |
|---|---|---|
| Autonomy | Services evolve, deploy and fail independently. | No single place shows the whole business flow. |
| Scalability | New features plug in as subscribers. | Event volume and storage grow with every subscriber and retention period. |
| Consistency | Each service owns its data. | Eventual consistency, duplicates and reordering must be designed for. |
Production considerations
- Trace events end to end with correlation ids; draw the flows in an event catalogue.
- Alert on consumer lag and dead-letter queues per subscriber.
- Prefer state transfer for high-volume or critical consumers; use notification when payloads would be huge or confidential.