Event-Driven
Architecture
Stop asking "What should I do next?" and start shouting "I just did something!". Inverting control flow to decouple services.
Why Events?
Request-Driven (Command)
OrderService → PaymentService
"Hey Payment Service, charge this card."
(Coupling: OrderService MUST know PaymentService exists.)
Event-Driven (Notification)
OrderService → Broker
"I just created an order."
(Decoupling: OrderService doesn't care who listens.)
The Benefit
You can add new functionality without changing existing code.
- Want to send an email? Add an Email Listener.
- Want to update analytics? Add an Analytics Listener.
- Want to trigger shipping? Add a Shipping Listener.
The Order Service changes exactly zero lines of code.
Producer-Consumer Pattern
Choreography vs Orchestration
How do services coordinate complex workflows?
Choreography (Dance)
Decentralized
Each service knows what to do when an event happens. No central manager.
Con: Hard to visualize the whole process.
Orchestration (Conductor)
Centralized
A central "Orchestrator" service tells everyone what to do.
Con: Tight coupling. Order service becomes a "God Service".
Event Sourcing
Store Events, Not State
Traditional (CRUD)
We overwrote the old balance. History is lost.
Event Sourcing
We store the sequence of changes. We can replay history to debug.
CQRS
Command Query Responsibility Segregation. Separating the model for updating information (Command) from the model for reading information (Query).
Command Model
Optimized for Writes / Logic.
Query Model
Optimized for Reads / Views.
The Command side publishes events. The Query side listens to events and updates its read-optimized views. (Eventual Consistency).