RabbitMQ vs. Apache Kafka: A Comparison

Event-Driven Messaging System

·

3 min read

RabbitMQ vs. Apache Kafka: A Comparison

Great choice! RabbitMQ and Apache Kafka are popular message brokers used for message queuing and event processing in distributed systems. Here's a comparison of RabbitMQ and Kafka along with a small example for each.

RabbitMQ vs. Apache Kafka

RabbitMQ

  • RabbitMQ is a traditional message broker that follows the Advanced Message Queuing Protocol (AMQP).

  • It's designed for message queuing, and it's well-suited for scenarios where you need reliable message delivery and acknowledgment.

  • RabbitMQ is great for applications that require complex routing and rules-based message processing.

Apache Kafka

  • Apache Kafka is a distributed event streaming platform that is highly scalable and fault-tolerant.

  • It's designed for real-time data streaming and processing of high-throughput, fault-tolerant, and real-time applications.

  • Kafka is excellent for use cases like real-time analytics, monitoring, and log aggregation.

Let's create examples using Java for both RabbitMQ and Apache Kafka to send and receive messages.

Example using RabbitMQ (Java)

  1. Producer (Sender):
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQProducer {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        try (Connection connection = factory.newConnection(); 
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello, RabbitMQ!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}
  1. Consumer (Receiver):
import com.rabbitmq.client.*;

public class RabbitMQConsumer {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        try (Connection connection = factory.newConnection(); 
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);

            DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                String message = new String(delivery.getBody(), "UTF-8");
                System.out.println(" [x] Received '" + message + "'");
            };

            channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
        }
    }
}

Example using Apache Kafka (Java)

  1. Producer (Sender):
import org.apache.kafka.clients.producer.*;

import java.util.Properties;

public class KafkaProducerExample {
    private static final String TOPIC = "my_topic";

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);

        String message = "Hello, Kafka!";

        ProducerRecord<String, String> record = new ProducerRecord<>(TOPIC, message);

        producer.send(record);
        producer.close();

        System.out.println("Message sent: " + message);
    }
}
  1. Consumer (Receiver):
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.Collections;
import java.util.Properties;

public class KafkaConsumerExample {
    private static final String TOPIC = "my_topic";
    private static final String GROUP_ID = "my_consumer_group";

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("key.deserializer", StringDeserializer.class.getName());
        props.put("value.deserializer", StringDeserializer.class.getName());
        props.put("group.id", GROUP_ID);

        Consumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Collections.singletonList(TOPIC));

        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records) {
                System.out.println("Message received: " + record.value());
            }
        }
    }
}

These Java examples demonstrate sending and receiving messages using RabbitMQ and Apache Kafka. Feel free to adjust the code according to your needs and explore further features and configurations for both RabbitMQ and Apache Kafka.

More in-depth blog on each Messaging system and their differtent types of implementation based on the use cases with gitHub link will be shared on further post.

Happy coding :)