Message Queues: RabbitMQ vs Kafka
Message queues decouple services and enable async processing. Choosing between them is a common system design question.
import pika, json
# RabbitMQ — Publisher
conn = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = conn.channel()
channel.exchange_declare(exchange='app', exchange_type='topic')
channel.queue_declare(queue='email_queue', durable=True)
channel.queue_bind(exchange='app', queue='email_queue', routing_key='user.*')
channel.basic_publish(
exchange='app', routing_key='user.registered',
body=json.dumps({'user_id': 42, 'email': 'r@e.com'}),
properties=pika.BasicProperties(delivery_mode=2) # persistent
)
# Consumer
def callback(ch, method, props, body):
data = json.loads(body)
send_welcome_email(data['email'])
ch.basic_ack(delivery_tag=method.delivery_tag)
# Kafka — Producer and Consumer
from kafka import KafkaProducer, KafkaConsumer
producer = KafkaProducer(bootstrap_servers='localhost:9092',
value_serializer=lambda v: json.dumps(v).encode())
producer.send('user-events', {'event': 'registered', 'user_id': 42})
consumer = KafkaConsumer('user-events', bootstrap_servers='localhost:9092',
group_id='email-service')
for message in consumer:
process_event(json.loads(message.value))
| Feature | RabbitMQ | Kafka |
|---|---|---|
| Delivery | Push (broker pushes) | Pull (consumer fetches) |
| Message replay | No (deleted after ACK) | Yes (configurable) |
| Throughput | 50K msg/s | 1M+ msg/s |
| Best for | Task queues, routing | Event streaming, logs |
Comments (0)
No comments yet. Be the first!
Leave a Comment