Careeroza
Kafka vs RabbitMQ
Two Different Models
Kafka is a distributed log: records stay for days, many groups read the same data, and you can replay. RabbitMQ is a message broker with queues: a message is routed through an exchange into queues and removed once a consumer acknowledges it.
| Apache Kafka | RabbitMQ | |
|---|---|---|
| Model | Partitioned log, consumers pull | Exchanges + queues, broker pushes |
| After reading | Record kept (retention); replay possible | Message deleted after ack |
| Ordering | Per partition (per key) | Per queue (single consumer) |
| Throughput | Very high (millions/s per cluster) | High (tens of thousands/s per node) |
| Routing | By topic only | Rich: direct, topic patterns, fanout, headers |
| Per-message features | Few | TTL, priorities, delayed messages, per-message ack |
| Spring support | spring-kafka: KafkaTemplate, @KafkaListener | spring-boot-starter-amqp: RabbitTemplate, @RabbitListener |
| Choose for | Event streaming, many consumers, analytics, audit log, high volume | Task queues, work distribution, complex routing, RPC-style jobs |
The Same Event with RabbitMQ
@Configuration
class RabbitConfig {
static final String EXCHANGE = "shopease.orders";
static final String PAYMENT_QUEUE = "payment.order-placed";
@Bean TopicExchange ordersExchange() { return new TopicExchange(EXCHANGE); }
@Bean Queue paymentQueue() {
return QueueBuilder.durable(PAYMENT_QUEUE).deadLetterExchange("shopease.dlx").build();
}
@Bean Binding paymentBinding(Queue paymentQueue, TopicExchange ordersExchange) {
return BindingBuilder.bind(paymentQueue).to(ordersExchange).with("order.placed");
}
@Bean MessageConverter jsonMessageConverter() { // send records as JSON
return new Jackson2JsonMessageConverter();
}
}
// order-service: publish
rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE, "order.placed", event);
// payment-service: consume
@RabbitListener(queues = RabbitConfig.PAYMENT_QUEUE)
void onOrderPlaced(OrderPlacedEvent event) {
paymentService.startPayment(event);
}Example
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.13-managementExample output
# management UI at http://localhost:15672 (guest / guest) Queues Name Messages Ready Unacked Incoming/s Deliver/s payment.order-placed 0 0 12.0 12.0
Tip: Both are fine for ShopEase. A common split in Indian product companies: Kafka for business events and analytics streams, RabbitMQ (or SQS) for background jobs like sending emails and generating invoices.