Exchanges Route
Exchanges route messages to queues based on routing keys. Direct, topic, fanout, headers.
RabbitMQ is a traditional message broker that implements the AMQP (Advanced Message Queuing Protocol) standard. Unlike Kafka’s log-based approach, RabbitMQ uses a more traditional broker model where messages are delivered to consumers and then removed from queues (unless configured otherwise).
RabbitMQ’s architecture revolves around four key components:
This design provides flexible routing capabilities that Kafka doesn’t offer natively. RabbitMQ excels at scenarios requiring complex routing logic, task distribution, and point-to-point messaging.
The Flow: Producer → Exchange (with routing key) → Binding (matches routing key) → Queue → Consumer
This design allows for flexible routing - you can route the same message to multiple queues, filter messages based on patterns, or broadcast to all queues, depending on the exchange type.
Routes to queue with matching routing key.
Use case: Point-to-point messaging, task queues
Routes based on pattern matching (wildcards).
Patterns:
* - Matches one word# - Matches zero or more wordsUse case: Categorized messages, event routing
Broadcasts to all bound queues (ignores routing key).
Use case: Pub-sub, notifications, cache invalidation
Routes based on message headers (ignores routing key).
Use case: Complex routing logic
Critical for reliable message processing.
# Message removed immediately when deliveredchannel.basic_consume(queue='orders', on_message_callback=callback, auto_ack=True)Problem: If consumer crashes, message lost!
# Message removed only after ackdef callback(ch, method, properties, body): process_message(body) ch.basic_ack(delivery_tag=method.delivery_tag) # Acknowledge
channel.basic_consume(queue='orders', on_message_callback=callback, auto_ack=False)Benefits:
| Feature | RabbitMQ | Kafka |
|---|---|---|
| Model | Traditional broker | Streaming platform |
| Message Retention | Removed after consumption | Retained (configurable) |
| Routing | Flexible (exchanges) | Simple (topics/partitions) |
| Ordering | Per queue | Per partition |
| Throughput | Good | Excellent |
| Use Case | Task queues, RPC | Event streaming, logs |
Choose RabbitMQ when:
Choose Kafka when:
Exchanges Route
Exchanges route messages to queues based on routing keys. Direct, topic, fanout, headers.
Manual Ack
Use manual acknowledgment for reliability. Auto-ack removes messages immediately (risky).
Durability
Make queues/exchanges/messages durable to survive broker restart. Critical for production.
Flexible Routing
RabbitMQ’s flexible routing (exchanges) makes it great for complex routing scenarios.