Interview question
What is RabbitMQ and how does it differ from Kafka? RabbitMQ क्या है और Kafka से कैसे अलग है?
Answer
RabbitMQ is a traditional message broker using AMQP protocol for queuing. Unlike Kafka (event streaming), RabbitMQ is optimized for point-to-point messaging, request-reply patterns, and guaranteed delivery with less throughput but simpler semantics.
| Feature | RabbitMQ | Kafka |
|---|---|---|
| Model | Message Queue | Event Stream |
| Throughput | Moderate (50K/sec) | Very High (1M+/sec) |
| Use Case | Task queue, RPC | Real-time analytics, streaming |
| Message retention | Until consumed | Configurable duration |
| Complexity | Simple | Complex |
// RabbitMQ Configuration
@Configuration
public class RabbitMQConfig {
public static final String QUEUE = 'orders.queue';
public static final String EXCHANGE = 'orders.exchange';
public static final String ROUTING_KEY = 'order.#';
@Bean
public Queue queue() {
return new Queue(QUEUE);
}
@Bean
public TopicExchange exchange() {
return new TopicExchange(EXCHANGE);
}
@Bean
public Binding binding() {
return BindingBuilder.bind(queue())
.to(exchange())
.with(ROUTING_KEY);
}
}
// RabbitMQ Producer
@Service
public class RabbitProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMessage(String message) {
rabbitTemplate.convertAndSend(
'orders.exchange', 'order.created', message);
}
}
// RabbitMQ Consumer
@Service
public class RabbitConsumer {
@RabbitListener(queues = 'orders.queue')
public void consumeMessage(String message) {
System.out.println('Received: ' + message);
}
}
// RabbitMQ patterns:
// 1. Work queues - load balancing
// 2. Pub/Sub - multiple consumers
// 3. Routing - topic-based
// 4. RPC - request-reply
// When to use RabbitMQ:
// - Task queues (email, notifications)
// - Request-reply patterns
// - Complex routing
// - Guaranteed delivery
// - Lower throughput acceptableRabbitMQ vs Kafka:
RabbitMQ:
- Message Queue model
- Moderate throughput (50K/sec)
- Point-to-point messaging
- Immediate consumption
- Simple setup
Kafka:
- Event Streaming
- Very high throughput (1M+/sec)
- Distributed streaming
- Message retention (replay)
- Complex distributed system
Configuration:
Queue, Exchange, Binding
RabbitTemplate - send करना
@RabbitListener - receive करना
When RabbitMQ:
- Task processing
- Simple messaging
- RPC callsWas this answer clear?