Event-Driven Architecture Pattern
Broker and mediator topologies, event notification vs event-carried state transfer, Java implementation with Kafka, and design rules for events as contracts.
Intent
Components communicate by publishing events (facts) to a broker; others subscribe and react. Producers do not know consumers. See the system design view in Event-Driven Architecture in System Design.
Topologies
| Topology | Description | Use when |
|---|---|---|
| Broker (choreography) | Events flow through a broker; each service reacts independently | Simple flows, high decoupling |
| Mediator (orchestration) | A mediator/orchestrator receives an initiating event and coordinates steps | Complex workflows, error handling, visibility |
flowchart LR
subgraph Choreography
A1[Order] -- OrderPlaced --> B1[(Broker)]
B1 --> P1[Payment] -- PaymentCaptured --> B1
B1 --> S1[Shipping]
end
subgraph Orchestration
O2[Order saga orchestrator] -->|ChargePayment| P2[Payment]
P2 -->|PaymentCaptured| O2
O2 -->|CreateShipment| S2[Shipping]
endJava example: Kafka producer and idempotent consumer
@Component
@RequiredArgsConstructor
class OrderEventsPublisher {
private final KafkaTemplate<String, OrderPlaced> kafka;
void publish(OrderPlaced e) {
kafka.send("orders.order-placed.v1", e.orderId(), e); // key = orderId for per-order ordering
}
}
@Component
@RequiredArgsConstructor
class ReserveStockHandler {
private final ProcessedEvents processed; // table with unique event_id
private final InventoryService inventory;
@KafkaListener(topics = "orders.order-placed.v1", groupId = "inventory")
@Transactional
void on(OrderPlaced e, @Header(KafkaHeaders.RECEIVED_KEY) String key) {
if (!processed.markIfNew(e.eventId())) return; // idempotency
inventory.reserve(e.orderId(), e.lines());
}
}In production, publish via the transactional outbox (Debezium) instead of calling Kafka inside the business transaction.
Event design rules
- Name events in past tense, as business facts:
OrderPlaced, notCreateOrderEvent. - Include event ID, type, version, occurred-at, aggregate ID, correlation ID.
- Version schemas; evolve compatibly; never reuse field meanings.
- Decide between thin events (IDs only) and fat events (state) per consumer needs.
Pros and cons
| Pros | Cons |
|---|---|
| Loose coupling and extensibility | Harder end-to-end visibility |
| Scalability and buffering | Eventual consistency |
| Natural audit/analytics feed | Duplicate and out-of-order handling |
Interview questions
Thin or fat events?
Thin events reduce coupling to data shape but force consumers to call back (load and availability coupling). Fat events (event-carried state transfer) let consumers be autonomous at the cost of larger contracts. Many systems use fat events for core domain facts with stable schemas.
How do you trace a business flow across events?
Propagate correlation/trace IDs in message headers (W3C trace context), instrument producers and consumers with OpenTelemetry, and keep a process view in an orchestrator or a projection of related events.