Wednesday, July 23, 2025

What is Event-Driven Architecture (EDA)?

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

ComponentDescription
EventA message that describes something that happened
ProducerPublishes events (e.g., OrderService emits OrderPlaced)
ConsumerListens for and processes events
Event Bus / BrokerMiddleware (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

  1. Order Service → emits OrderPlaced

  2. Inventory Service → listens to OrderPlaced, updates stock, emits InventoryReserved

  3. Payment Service → listens to InventoryReserved, charges payment

  4. Shipping Service → listens to PaymentSuccess, ships item

All without direct service-to-service calls.


⚙️ Communication Models

ModelDescription
Asynchronous MessagingMost common (Kafka, RabbitMQ)
Synchronous CallbacksHybrid (REST + event fallback)
Event StreamingKafka, Pulsar, for real-time

📦 Event Types

Event TypeDescription
Domain EventsHigh-level business events (UserRegistered)
Integration EventsShared between bounded contexts
System EventsLower-level (e.g., file uploaded)

🛠️ Java & Spring Boot Implementation

Tools:

  • Spring Boot + Spring Cloud Stream or Spring Kafka

  • Kafka, RabbitMQ, ActiveMQ

Example (Spring Kafka):

// Producer kafkaTemplate.send("order-events", new OrderPlaced(orderId)); // Consumer @KafkaListener(topics = "order-events") public void handleOrderEvent(OrderPlaced event) { inventoryService.reserve(event.getOrderId()); }

📐 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