EDA is an architectural style where components communicate by emitting and responding to events, rather than direct calls.
Instead of “calling” another service, a component publishes an event, and one or more other components react to it asynchronously.
🧱 Core Components
Component | Description |
---|---|
Event | A message that describes something that happened |
Producer | Publishes events (e.g., OrderService emits OrderPlaced ) |
Consumer | Listens for and processes events |
Event Bus / Broker | Middleware (Kafka, RabbitMQ) that delivers events |
🎯 Why Use Event-Driven Architecture?
“We used EDA because we wanted loose coupling, scalability, and asynchronous processing for better performance and resiliency.”
✅ Benefits:
-
Decouples producers and consumers
-
Improves scalability and responsiveness
-
Enables real-time data flow
-
Supports eventual consistency and auditability
❌ Challenges:
-
Debugging is harder
-
Event ordering issues
-
Requires idempotency and reliable messaging
🔁 Common Event Flows (Real-world)
🛒 E-commerce Checkout Example
-
Order Service → emits
OrderPlaced
-
Inventory Service → listens to
OrderPlaced
, updates stock, emitsInventoryReserved
-
Payment Service → listens to
InventoryReserved
, charges payment -
Shipping Service → listens to
PaymentSuccess
, ships item
All without direct service-to-service calls.
⚙️ Communication Models
Model | Description |
---|---|
Asynchronous Messaging | Most common (Kafka, RabbitMQ) |
Synchronous Callbacks | Hybrid (REST + event fallback) |
Event Streaming | Kafka, Pulsar, for real-time |
📦 Event Types
Event Type | Description |
---|---|
Domain Events | High-level business events (UserRegistered ) |
Integration Events | Shared between bounded contexts |
System Events | Lower-level (e.g., file uploaded) |
🛠️ Java & Spring Boot Implementation
Tools:
-
Spring Boot + Spring Cloud Stream or Spring Kafka
-
Kafka, RabbitMQ, ActiveMQ
Example (Spring Kafka):
📐 Architectural Patterns using EDA
-
Event Sourcing: Store event log instead of current state
-
CQRS: Often paired with EDA to separate reads/writes
-
Saga Pattern: Uses events for long-running transaction coordination
-
Choreography: Event-based distributed coordination (no central orchestrator)
🎯 Interview-Pro Answer Format
“We used Event-Driven Architecture to loosely couple our microservices. Each service emitted or consumed domain events using Kafka. This enabled us to decouple the order, payment, and shipping services and scale them independently. We ensured reliability by making event consumers idempotent and using compacted Kafka topics for replayability.”
No comments:
Post a Comment