Message Queues: RabbitMQ & Kafka Intro
Synchronous API calls (HTTP) are risky. If <code>user-service</code> calls <code>payment-service</code> via Feign, and payment takes 10 seconds, the user waits 10 seconds. <strong>Asynchronous Messaging</strong> fixes this using message brokers like <strong>RabbitMQ</strong> and <strong>Kafka</strong>.
The Swiggy Delivery vs. Pre-Ordering Analogy:
If you order Biryani on Swiggy and wait at the door until it arrives, that's Synchronous (HTTP). You can't do anything else.
If you pre-order catering for a wedding, you just drop the request at the restaurant and go to work. The restaurant puts the order in a queue, processes it over 5 hours, and notifies you when done. That's Asynchronous (Message Queue).
In Spring Boot, user-service drops a message in a Queue. It doesn't wait. payment-service picks it up whenever it has time.
- RabbitMQ: A traditional Message Broker. It delivers a message to one consumer, and then deletes it. Great for task queues (e.g., "Send this email").
- Kafka: An Event Streaming Platform. It stores messages like a log. Multiple consumers can read the same event at different times. Great for real-time data pipelines (e.g., "User clicked a button").
- spring-boot-starter-amqp: The dependency for RabbitMQ.
1. pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>2. application.yml
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest3. Producer & Consumer (MessageService.java)
package com.devinhyderabad;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
@Service
public class MessageService {
private final RabbitTemplate rabbitTemplate;
public MessageService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
// 1. PRODUCER: Drops a message into the "payment-queue" and returns instantly
public void sendPaymentRequest(String message) {
System.out.println("Sending to queue: " + message);
rabbitTemplate.convertAndSend("payment-queue", message);
}
// 2. CONSUMER: Listens to the queue. Runs automatically when a message arrives
@RabbitListener(queues = "payment-queue")
public void processPayment(String message) {
System.out.println("Received payment request: " + message);
// Process payment asynchronously...
}
}Interview Question: "What is the difference between RabbitMQ and Kafka?"
Answer: RabbitMQ is a smart broker/dumb consumer. It pushes messages to consumers and deletes them once acknowledged. Kafka is a dumb broker/smart consumer. It appends messages to a permanent log, and consumers keep track of what they have read. Kafka is better for event sourcing and big data, while RabbitMQ is better for traditional task queues.
Enterprise Note: Using message queues decouples your microservices. If the payment-service goes down, user-service can keep accepting requests and dumping them into RabbitMQ. When payment-service comes back online, it processes the backlog. No data is lost.
Key Takeaways
- ✅ Asynchronous messaging via RabbitMQ/Kafka decouples microservices and prevents blocking
- ✅ RabbitMQ is a traditional message broker — delivers messages to one consumer then deletes
- ✅ Kafka is an event streaming platform — stores messages in a log for multiple consumers
- ✅ Spring Boot AMQP provides RabbitTemplate (producer) and @RabbitListener (consumer)
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login