0
0
Microservicessystem_design~25 mins

Message brokers (Kafka, RabbitMQ) in Microservices - System Design Exercise

Choose your learning style9 modes available
Design: Microservices Communication with Message Brokers
Design focuses on integrating Kafka and RabbitMQ as message brokers for microservices communication. Out of scope are the internal logic of microservices and detailed security implementations.
Functional Requirements
FR1: Enable asynchronous communication between microservices
FR2: Support message durability and reliability
FR3: Handle high throughput of messages (up to 100,000 messages per second)
FR4: Ensure message ordering where required
FR5: Support multiple consumers for the same message stream
FR6: Allow message filtering and routing
FR7: Provide monitoring and alerting for message processing failures
Non-Functional Requirements
NFR1: Latency for message delivery should be under 100ms p99
NFR2: System availability target of 99.9% uptime
NFR3: Support horizontal scaling of message brokers and consumers
NFR4: Ensure at-least-once delivery semantics
NFR5: Handle message retention for up to 7 days
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
❓ Question 6
❓ Question 7
Key Components
Producers (microservices sending messages)
Message broker cluster (Kafka or RabbitMQ)
Consumers (microservices receiving messages)
Message queues or topics
Message serialization format (e.g., JSON, Avro)
Monitoring and alerting system
Load balancers or proxies for broker access
Design Patterns
Publish-Subscribe pattern
Message Queue pattern
Event Sourcing
Dead Letter Queue for failed messages
Consumer Group for load balancing
Message filtering and routing
Reference Architecture
 +-------------+       +----------------+       +-------------+
 |  Producer   | ----> |  Message Broker | ----> |  Consumer   |
 | (Service A) |       | (Kafka/Rabbit) |       | (Service B) |
 +-------------+       +----------------+       +-------------+
         |                     |                      |
         |                     |                      |
         |                     v                      |
         |             +----------------+             |
         |             |  Monitoring &   |             |
         |             |   Alerting     |             |
         |             +----------------+             |
         |                                         
         +-----------------------------------------+
                         Message Flow
Components
Producer
Microservice (any language)
Sends messages/events to the message broker asynchronously
Message Broker Cluster
Apache Kafka or RabbitMQ
Stores, routes, and delivers messages reliably between producers and consumers
Consumer
Microservice (any language)
Receives and processes messages from the broker
Monitoring & Alerting
Prometheus + Grafana or equivalent
Tracks message broker health, message lag, failures, and alerts on issues
Request Flow
1. Producer microservice creates a message and serializes it (e.g., JSON).
2. Producer sends the message to the message broker topic or queue.
3. Message broker stores the message durably and routes it to subscribed consumers.
4. Consumers in a consumer group receive messages for processing.
5. If processing succeeds, consumer commits the message offset (Kafka) or acknowledges (RabbitMQ).
6. If processing fails, message can be retried or sent to a dead letter queue.
7. Monitoring system collects metrics on message throughput, lag, and failures.
8. Alerts are triggered if message processing falls behind or errors increase.
Database Schema
Not applicable as message brokers use internal storage. Key entities: Topics (Kafka) or Queues (RabbitMQ), Messages with metadata (offset, timestamp, key), Consumer Groups for load balancing.
Scaling Discussion
Bottlenecks
Message broker storage and throughput limits under high load
Consumer processing speed causing message backlog
Network bandwidth between producers, brokers, and consumers
Single point of failure if broker cluster is not highly available
Monitoring system overload with large metrics volume
Solutions
Scale broker cluster horizontally by adding more nodes and partitions (Kafka) or nodes and queues (RabbitMQ)
Increase number of consumer instances in consumer groups to parallelize processing
Use compression and efficient serialization to reduce message size
Deploy brokers in a highly available cluster with replication and failover
Implement sampling or aggregation in monitoring to reduce metrics volume
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 20 minutes designing the architecture and data flow, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain why asynchronous messaging is needed for microservices decoupling
Discuss differences between Kafka and RabbitMQ and when to use each
Describe message delivery guarantees and how to handle failures
Show understanding of consumer groups and load balancing
Highlight monitoring importance and how to detect issues early
Address scaling challenges and practical solutions