Stop 15 · Message Queues & Event-Driven ArchitectureTopics · 2/8

RabbitMQ

Part of the Backend Developer Roadmap

In short

RabbitMQ is the classic message broker. Its trick is routing: producers send to an exchange, and rules decide which queues get each message.

A mature, widely used open-source message broker implementing the AMQP protocol, known for flexible routing (exchanges, queues, bindings) and reliability.

The blob sorts colour-coded envelopes into a wall of wooden pigeonholes.

Think of it like this

The post office sorting room

You drop a letter in the post box; you don't walk it to the right town. In the sorting room, a clerk reads the PIN code and slips it into the right pigeonhole, and each delivery person empties their own hole. Exchanges are the clerk, queues are the pigeonholes.

Watch it happen

One message through RabbitMQ

Producer
Exchange
Queue
Consumer

key: order.created

1/5The order service publishes a message to the orders exchange with a routing key.

The big ideas

What you need to know

Exchanges route

Direct matches the key exactly, topic matches patterns like order.*, and fanout copies to every bound queue.

Durable and persistent

Mark queues durable and messages persistent so they survive a broker restart. Both flags are needed.

Prefetch controls load

prefetch(5) means a consumer holds at most five unacked messages, so work spreads fairly across consumers.

Dead lettering

Rejected or expired messages can be routed to a separate queue for inspection instead of vanishing.

See it in code

Walk through the code

ts
1import amqp from "amqplib";2 3const conn = await amqp.connect("amqp://localhost");4const ch = await conn.createChannel();5await ch.assertQueue("emails", { durable: true });6 7// Producer8ch.sendToQueue("emails", Buffer.from(JSON.stringify({ to: "riya@example.com" })), { persistent: true });9 10// Consumer11await ch.prefetch(5);12await ch.consume("emails", async (msg) => {13  if (!msg) return;14  await sendEmail(JSON.parse(msg.content.toString()));15  ch.ack(msg);16});

1/4Connect, open a channel, and make sure a durable emails queue exists.

sendToQueue uses the default exchange, which routes by queue name. For real routing, assertExchange and bindQueue, then publish to the exchange.

Save yourself the pain

Common mistakes

Oops

Using noAck: true for important jobs to make the code shorter.

Do this instead

With auto-ack, a crash loses the message. Ack manually after the work succeeds.

Oops

Opening a new connection for every message.

Do this instead

Connections are heavy. Open one per process and reuse channels.

Your mission

RabbitMQ in Docker

~30 min

Quick check

You want every bound queue to get a copy of each message, whatever the routing key. Which exchange type fits?

Go deeper

  1. 1Run RabbitMQ locally and send/receive a message between a producer and consumer script.
  2. 2Learn the difference between a direct, fanout and topic exchange.
  3. 3Explore the RabbitMQ management UI to visualize queues and message flow.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge