Event-Driven Architecture

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2023-12-30

Problem Statement

Design a highly scalable, loosely coupled, and asynchronous system using Event-Driven Architecture (EDA) principles.


The goal is to handle communication between multiple microservices or modules through events rather than direct synchronous API calls.


You need to demonstrate how your system:


•Publishes and consumes events efficiently,
•Ensures reliability, ordering, and fault tolerance, and
•Can scale to millions of events per day.


🧾 Context


Modern distributed systems — like e-commerce platforms, logistics tracking, or streaming services — rely on event-driven designs to ensure decoupling, real-time updates, and high throughput.


In this problem, you will design an event-driven architecture for a system where multiple services communicate via event buses/message brokers (e.g., Kafka, RabbitMQ, or AWS SNS/SQS).


🧱 Problem Statement

Design a scalable Event-Driven Architecture that enables multiple independent services to communicate asynchronously through events, ensuring reliability, high throughput, and fault tolerance.

Your design should cover:


•Event generation (producers/publishers)
•Event distribution (message brokers or queues)
•Event consumption (subscribers/consumers)
•Event persistence and replay mechanisms
•Error handling and retries


📋 Functional Requirements


1.Event Publishing: Services should be able to publish events to a centralized event bus (e.g., “OrderCreated”, “PaymentCompleted”).
2.Event Subscription: Services should subscribe to relevant events and react accordingly (e.g., “SendInvoiceService” listens to “PaymentCompleted”).
3.Asynchronous Communication: No direct coupling between services; all communication should occur via events.
4.Event Persistence: Events must be stored durably to prevent loss and support replay.
5.Scalability: The system must handle millions of events per day.
6.Monitoring & Observability: The system should provide visibility into event flow and failures.


⚙️ Non-Functional Requirements


Attribute and Description


Scalability


Must handle high event throughput with low latency.


Reliability


No event loss even during broker failures.


Durability


Persist events for replay and auditing.


Fault Tolerance


Consumers should recover from crashes gracefully.



Decoupling


Producers and consumers should evolve independently.


Consistency


Ensure eventual consistency across services.



🧩 High-Level Architecture


Core Components

1.Event Producers:
•Services generating events (e.g., “OrderService” publishes “OrderPlaced”).
2.Event Broker:
•A distributed message bus (e.g., Apache Kafka, RabbitMQ, AWS SNS/SQS) that delivers messages asynchronously.
3.Event Consumers:
•Services that subscribe and react to events (e.g., “NotificationService”, “BillingService”).
4.Event Store (optional):
•Stores all published events for auditing, debugging, and replay.
5.Monitoring & Alerting Layer:
•Tracks lag, failed events, and consumer offsets.


Example Architecture Diagram (Textual Representation)

1 2[Order Service] ───► [Event Broker: Kafka Topic "OrderPlaced"] ───► [Payment Service] 3 └──► [Inventory Service] 4 └──► [Notification Service]

Each microservice publishes and consumes events asynchronously through Kafka topics or message queues.


🧠 Example Scenario — E-commerce Order Workflow

Flow:


1.Order Service publishes OrderCreated event.
2.Payment Service consumes OrderCreated, processes payment, and emits PaymentCompleted.
3.Inventory Service consumes OrderCreated, reserves stock.
4.Notification Service consumes both events and sends confirmation emails.
5.Analytics Service listens to all events for reporting.


🧮 Scalability Considerations


•Partition events by key (e.g., OrderID) for parallel processing.
•Use consumer groups for load balancing.
•Use schema registry for versioning event formats.
•Enable event retention policies in the broker for replay and debugging.



🧰 Tech Stack (Example Choices)


Component and Technology


Message Broker

Apache Kafka / RabbitMQ / AWS SNS + SQS


Producers/Consumers

Spring Boot, Node.js, or Go microservices


Schema Registry

Confluent Schema Registry (for Avro/JSON schema evolution)


Monitoring

Prometheus + Grafana or ELK stack


Event Store (Optional)

PostgreSQL / Cassandra / S3 bucket


⚠️ Challenges and Trade-offs


Design AspectTrade-off


At-least-once vs Exactly-once Delivery

Exactly-once adds complexity and cost; at-least-once often acceptable.


Ordering Guarantees

Kafka ensures ordering per partition, not globally.


Event Schema Evolution

Requires versioning to avoid consumer failures.


Error Handling

Failed messages go to Dead Letter Queues (DLQs).


System Observability

Harder to trace asynchronous event chains.


📊 Key Discussion Topics in Interview


•How would you handle idempotency in consumers?
•How do you ensure event ordering?
•What happens if a consumer crashes mid-processing?
•How do you handle schema changes in events?
•How would you scale consumers for millions of messages per second?
•How do you design for monitoring and alerting on event failures or lags?


🚀 Expected Deliverables


DeliverableDescription


System Overview

Description of services, message flow, and event lifecycle.


Architecture Diagram

Depict event producers, broker, consumers, and flow.


Data Flow Diagram

Sequence of event publication and consumption.


Scaling & Recovery Strategy

Partitioning, retries, DLQs, replay mechanisms.


Challenges & Solutions

Explain trade-offs and design decisions.


✅ Example Problem Statement Summary

Design a scalable Event-Driven Architecture to connect multiple microservices in an e-commerce system, where services publish and consume events asynchronously using a message broker (like Kafka or RabbitMQ). The architecture should ensure reliability, ordering, fault tolerance, and support for event replay and monitoring.


Solution

Comments