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.
More Related questions...