π Event Driven Architecture (EDA)
1οΈβ£ What is EDA? β‘
Definition:
- A system design style where services do NOT call each other directly
- Instead:
- One service publishes an event β Other services react to that event
π Key Idea
βServices communicate via events, not direct callsβ
π What is an Event?
- An event = fact that already happened (past)
- Always in past tense
| Type | Meaning | Example |
|---|---|---|
| Event | Something happened | OrderCreated |
| Command | Ask to do something | PlaceOrder |
| Query | Fetch data | GetOrderDetails |
π Event Properties
- Immutable β cannot change once created
- Past fact β describes what happened
- Self-contained β contains all required data
2οΈβ£ Traditional (Synchronous) vs EDA π
β Synchronous Flow (REST)
Order Service β Inventory β Payment β Notification
π¨ Problems
- Availability issue
- All services must be UP
- Latency accumulation
- Total latency = sum of all services
- Cascading failure
- One slow service affects entire flow
- Tight coupling
- Services depend on each other
- Scaling issue
- Cannot scale services independently
3οΈβ£ EDA Flow (Asynchronous) π
π High-Level Flow
-
User places order
-
Order Service:
- Saves order (
PENDING) - Publishes event β
OrderCreated
- Saves order (
-
Event Router sends event to:
- Payment Service
- Inventory Service
-
These services:
- Process independently
- Publish new events (
PaymentSuccess, etc.)
-
Notification Service reacts
π Key Concept
βEmit β Route β Consumeβ
4οΈβ£ Core Components π§±
1. Producer π€
- Publishes events
- Example: Order Service
2. Broker (Event Router) π
- Routes events to consumers
- Acts as mediator
Examples:
- Kafka
- RabbitMQ
3. Consumer π₯
- Listens and reacts to events
5οΈβ£ Advantages of EDA π
- Loose coupling
- No direct dependency
- Independent scalability
- Scale services individually
- Better resilience
- One failure β system failure
- Improved latency
- Faster user response
- Replay capability
- Reprocess old events
6οΈβ£ Event Flow Models π
πΉ Push Model
- Broker β pushes events to consumers
- Consumer must handle incoming rate
πΉ Pull Model
- Consumer β requests events from broker
- Processes at its own pace
7οΈβ£ EDA Models π§
1. Pub-Sub Model π‘
- Events sent to active consumers only
- No storage
- No replay
Example:
- RabbitMQ
2. Streaming Model π
- Events stored in logs
- Supports replay
- Consumers read using offset
Example:
- Kafka
8οΈβ£ Challenges in EDA β οΈ
πΈ Eventual Consistency
- Data may be temporarily stale
πΈ Duplicate Events
- At-least-once delivery
- Must handle duplicates
πΈ Ordering Issues
- Events may arrive out of order
πΈ Schema Evolution
- Changing event structure can break consumers
πΈ Debugging Complexity
- Hard to trace async flows
πΈ Poison Message
- Bad event blocks processing
πΈ Operational Overhead
- Monitor:
- Consumer lag
- Queue size
- Throughput
9οΈβ£ When to Use EDA? π€
β Use EDA when:
1. One Event β Multiple Consumers
- Example:
- OrderCreated β
- Payment
- Inventory
- Notification
- Analytics
- OrderCreated β
2. Long-running workflows
- Order β Payment β Shipping β Delivery
3. Eventual Consistency is acceptable
- Slight delay is okay
4. Real-time data processing
- Streaming / analytics systems
π Key Takeaway π―
EDA = Decoupled + Scalable + Asynchronous system
π Mental Model
Producer β Broker β Consumer
If you want next level notes, I can break this into:
- Kafka deep dive (interview level)
- Real-world design (Flipkart / Amazon order system)
- EDA vs REST decision framework
Just tell π