0
0
Microservicessystem_design~25 mins

Why events decouple services in Microservices - Design It to Understand It

Choose your learning style9 modes available
Design: Event-Driven Microservices Communication
Focus on communication patterns between microservices using events. Out of scope: detailed implementation of each microservice business logic.
Functional Requirements
FR1: Services should communicate without tight dependencies
FR2: Allow independent deployment and scaling of services
FR3: Ensure services can evolve without breaking others
FR4: Support asynchronous communication for better performance
FR5: Handle failures gracefully without cascading errors
Non-Functional Requirements
NFR1: Must support at least 10,000 events per second
NFR2: Event delivery latency should be under 100ms p99
NFR3: System availability target of 99.9%
NFR4: Event ordering is not strictly required but eventual consistency is expected
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Event broker or message queue (e.g., Kafka, RabbitMQ)
Event producers and consumers
Event schema registry
Service registry or discovery
Dead letter queue for failed events
Design Patterns
Publish-Subscribe pattern
Event Sourcing
CQRS (Command Query Responsibility Segregation)
Circuit Breaker for fault tolerance
Idempotent event handling
Reference Architecture
 +----------------+       +----------------+       +----------------+
 |  Service A     |       |  Event Broker  |       |  Service B     |
 | (Producer)     | ----> | (Kafka/Rabbit) | <---- | (Consumer)     |
 +----------------+       +----------------+       +----------------+
          |                        |                        |
          |                        |                        |
          +----> Event published --+                        |
                                   +----> Event consumed --+
Components
Service A (Producer)
Any microservice framework
Generates and publishes events about business actions
Event Broker
Apache Kafka or RabbitMQ
Decouples services by handling event distribution asynchronously
Service B (Consumer)
Any microservice framework
Consumes events to react or update its own state independently
Schema Registry
Confluent Schema Registry or similar
Ensures event data format compatibility between services
Dead Letter Queue
Part of event broker
Stores events that failed processing for later inspection
Request Flow
1. Service A performs a business action and creates an event.
2. Service A publishes the event to the Event Broker asynchronously.
3. Event Broker stores and distributes the event to subscribed consumers.
4. Service B receives the event and processes it independently.
5. If Service B fails to process, the event is sent to Dead Letter Queue.
6. Services operate independently without waiting for each other.
Database Schema
Not applicable as this design focuses on event communication. However, services maintain their own databases and update state based on consumed events.
Scaling Discussion
Bottlenecks
Event Broker throughput limits when event volume grows
Consumer processing speed causing lag in event consumption
Schema evolution causing compatibility issues
Network latency affecting event delivery time
Solutions
Partition event topics and scale Event Broker horizontally
Scale consumers horizontally and implement backpressure handling
Use schema versioning and compatibility checks in Schema Registry
Deploy Event Broker and services in the same region or use CDN for faster delivery
Interview Tips
Time: Spend 10 minutes explaining the problem and why decoupling is important, 15 minutes on architecture and components, 10 minutes on scaling and failure handling, 10 minutes on Q&A.
Explain tight coupling and its drawbacks in microservices
Describe how events enable asynchronous, independent communication
Highlight the role of the event broker as a decoupling layer
Discuss failure isolation and retry mechanisms
Mention schema management to avoid breaking changes
Talk about scaling event brokers and consumers