API / Microservices Design Patterns Interview Questions
What is the Message Broker pattern and how does it enable asynchronous microservice communication?
The Message Broker pattern introduces a durable intermediary — the broker (Apache Kafka, RabbitMQ, AWS SQS/SNS) — between a producer service and one or more consumer services. The producer publishes a message to the broker and returns immediately without waiting for consumers to process it. Consumers pull messages when ready. The broker stores messages until they are acknowledged, providing durability and decoupling.
// Producer (Java/Kafka)
ProducerRecord<String, String> record =
new ProducerRecord<>("order-events", orderId, orderEventJson);
kafkaProducer.send(record); // non-blocking; returns immediately
// No dependency on whether any consumer is alive
// Consumer (Java/Kafka)
ConsumerRecords<String, String> records =
kafkaConsumer.poll(Duration.ofMillis(500));
for (ConsumerRecord r : records) {
orderEventHandler.handle(r.value());
kafkaConsumer.commitSync(); // acknowledge offset
}
Types of messaging models:
- Publish/Subscribe (topic) — one message is delivered to all subscribed consumer groups. Used for domain events (e.g., Kafka topics). Multiple independent consumers receive the same event.
- Point-to-point (queue) — each message is delivered to exactly one consumer. Used for task distribution (e.g., SQS, RabbitMQ queues). Competing consumers load-balance across queue messages.
The pattern solves temporal coupling: with direct HTTP calls, the caller must wait for the receiver to be available. With a broker, the producer can publish even if all consumers are down — messages accumulate and are processed when consumers recover. It also provides rate smoothing: if a producer bursts messages faster than consumers can process, the broker absorbs the burst and consumers drain at their own pace.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
