Intermediate LevelSoftware Engineering Principles
What is Event-Driven Architecture (EDA) and how do Event Handlers communicate asynchronously?
❓ Interview Prompt: Explain Event-Driven Architecture (EDA), Event Producers/Consumers, Message Brokers, and Saga Pattern (Choreography vs Orchestration).
Event-Driven Architecture (EDA) connects decoupled microservices by publishing and consuming asynchronous event messages:
Saga Pattern for Distributed Transactions:
When a transaction spans multiple microservices, Sagas coordinate execution via a sequence of local transactions:
•
Choreography: Each service listens to events and publishes new events independently.
•
Orchestration: A central Saga Orchestrator tells participating services which local transaction to execute and handles compensating rollback actions if a step fails.
Code Implementation Example
typescript
// Saga Compensating Action Example
class OrderSagaOrchestrator {
async executeOrder(orderId: string) {
try {
await inventoryService.reserveStock(orderId);
await paymentService.chargeCard(orderId); // Fails!
} catch (err) {
// Compensating Action: Rollback previous local transaction!
await inventoryService.releaseStock(orderId);
}
}
}💡Executing compensating rollback actions in Saga Orchestration.
🎯 Interview Key Takeaway
Use Event-Driven Architecture and Saga patterns to manage distributed microservice workflows asynchronously.
#Event Driven#Saga Pattern#Microservices#Intermediate
Back to all Software Engineering Principles Questions→