Messaging and streaming

RabbitMQ

RabbitMQ is a mature message broker with flexible routing through exchanges, acknowledgements and AMQP support.

What it is

RabbitMQ appeared in 2007 at Rabbit Technologies, then moved to VMware and is now developed by Broadcom. It is a broker written in Erlang that implements AMQP 0-9-1 and also supports MQTT, STOMP and AMQP 1.0.

A producer sends a message to an exchange, which routes it to queues by rules (direct, topic, fanout, headers). Consumers take messages from queues and acknowledge processing (ack), otherwise the message returns.

RabbitMQ fits background jobs, RPC and service integration. Newer quorum queues and streams add replication and log-like behavior.

Use it when

  • You need background job queues with acknowledgements and retries.
  • You need flexible routing by keys or patterns.
  • You integrate systems through AMQP or MQTT.
  • You need priorities, message TTL and dead-letter queues.

Avoid it when

  • You need a long-retention event log with replay at millions of messages per second: Kafka fits better.
  • You need strict global ordering of all messages.

Key concepts

Exchange
The entry point for messages that routes them to queues by type and key.
Queue / binding
A queue stores messages, a binding links it to an exchange by a key.
Acknowledgement
The consumer confirms processing, otherwise the broker delivers the message again.
Dead-letter queue
A queue for messages that were rejected, expired or exhausted their retries.
Quorum queue
Replicated Raft-based queues that survive a node failure.

Example

Publishing a persistent message to the emails queue with the pika library.

Cheat sheet

RabbitMQ cheat sheet
Command or codeWhat it does
docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:managementRun with the management UI on port 15672
rabbitmqctl list_queuesList queues and message counts
rabbitmqctl list_exchangesList exchanges
rabbitmqctl list_bindingsBindings between exchanges and queues
rabbitmqctl list_consumersConnected consumers
rabbitmqctl add_user app secretCreate a user
rabbitmqctl set_permissions -p / app ".*" ".*" ".*"Grant permissions on a vhost
rabbitmqctl purge_queue jobsPurge a queue
rabbitmq-plugins enable rabbitmq_managementEnable the management UI plugin
rabbitmqctl statusNode status

How to learn it

  1. Understand the AMQP model: producer, exchange, binding, queue, consumer, and why a producer writes to an exchange, not a queue.
  2. Run RabbitMQ with the management UI in Docker and look at the queues in the browser.
  3. Build the simplest example: a producer puts a message in a queue, a consumer reads it.
  4. Learn exchange types: direct, fanout, topic, headers and pick the right one for each case.
  5. Master acknowledgements, prefetch, durable queues and persistent messages so you do not lose data.
  6. Add delayed retries and a dead letter exchange for messages that fail processing.
  7. Build work queue and pub/sub patterns in your app through an official client (amqplib, Pika).
  8. Read about clusters, quorum queues and streams before running RabbitMQ in production.

Interview questions

  1. 1. What exchange types does RabbitMQ have?

    Direct routes by exact key, topic by a dotted pattern with wildcards, fanout broadcasts to all bound queues, headers routes by message headers.

  2. 2. What is an ack and why is it needed?

    It is the consumer's confirmation of processing. If a worker dies without an ack, the broker puts the message back, so the task is not lost. The cost: possible duplicates.

  3. 3. How does RabbitMQ differ from Kafka?

    RabbitMQ is a smart broker with routing where a message is removed after ack. Kafka is a log where the consumer manages its position and data stays per retention.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).