πŸ“Œ 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
TypeMeaningExample
EventSomething happenedOrderCreated
CommandAsk to do somethingPlaceOrder
QueryFetch dataGetOrderDetails

πŸ“Œ 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

  1. User places order

  2. Order Service:

    • Saves order (PENDING)
    • Publishes event β†’ OrderCreated
  3. Event Router sends event to:

    • Payment Service
    • Inventory Service
  4. These services:

    • Process independently
    • Publish new events (PaymentSuccess, etc.)
  5. 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

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 πŸ‘