RabbitMQ is message-queuing software for indirect, asynchronous communication between microservices. It lets senders publish messages without knowing which receivers, if any, will handle them, reducing direct coupling. section 5.1 section 5.8
Use RabbitMQ when a microservice should communicate without directly orchestrating another service:
- Publish events to a queue so another service can process them in its own time. section 5.8
- Broadcast application-wide events to multiple recipients when several services may react independently. section 5.8.9
- Prefer direct HTTP messaging when the sender must sequence behavior or immediately know whether handling succeeded. section 5.11
A Docker Compose deployment can run RabbitMQ as a rabbitmq:3.12.4-management container, exposing ports 5672 and 15672. A Node.js service connects through amqplib, using an environment-configured AMQP URI, then creates a messaging channel. section 5.8.3 section 5.8.5
Viewed event
Instead of having the video-streaming microservice call the history microservice directly, publish a “Viewed” message. The history service pulls it from the queue in its own time, while other services can receive the event independently. section 5.8 section 5.8.9
Listing 5.8 Adding a RabbitMQ server to the Docker Compose file
version: '3'
services:
--snip--
rabbit: #1
image: rabbitmq:3.12.4-management #2
container_name: rabbit #3
ports: #4
- "5672:5672" #4
- "15672:15672" #4
restart: always #5
--snip--
Inspect the Docker Compose service definition for the RabbitMQ image, management variant, exposed ports, and restart policy. section 5.8.3
Listing 5.9 Connecting to the RabbitMQ server
--snip-- const amqp = require("amqplib"); #1 const RABBIT = process.env.RABBIT; #2 async function main() { --snip-- const messagingConnection = await amqp.connect(RABBIT); #3 const messageChannel = await messagingConnection.createChannel(); #4 --snip-- app.listen(PORT); #5 } #1 Imports the amqplib library. This is the API for talking to the RabbitMQ server. #2 Gets the URI for connecting to RabbitMQ #3 Connects to the RabbitMQ server #4 Creates a RabbitMQ messaging channel #5 Starts the HTTP server
Inspect the Node.js connection sequence: import amqplib, read the RABBIT URI, connect, and create a channel. section 5.8.5




