0
0
RabbitMQdevops~5 mins

What is a message queue in RabbitMQ - CLI Guide

Choose your learning style9 modes available
Introduction
A message queue helps different parts of a system talk to each other by sending messages in order. It solves the problem of making sure messages are delivered safely and in the right sequence, even if parts of the system work at different speeds.
When you want to send tasks from a web app to a background worker without slowing down the user.
When you need to connect different services that run on separate servers and must exchange data reliably.
When you want to handle spikes in traffic by storing messages temporarily until workers can process them.
When you want to make sure messages are not lost if a service crashes or restarts.
When you want to decouple parts of your system so they can be updated independently.
Commands
This command starts a RabbitMQ server in a Docker container with the management interface enabled. It maps ports so you can connect to RabbitMQ and access the web dashboard.
Terminal
docker run -d --hostname my-rabbit --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
Expected OutputExpected
a long container ID string like "e3f1c2d4b5a6..."
-d - Run container in background
--hostname - Set container hostname
-p - Map container ports to host
This command lists all queues currently on the RabbitMQ server to check if any message queues exist.
Terminal
docker exec -it rabbitmq rabbitmqctl list_queues
Expected OutputExpected
Listing queues ...
-it - Run interactive terminal inside container
This command creates a new durable queue named 'my-queue' on the RabbitMQ server. Durable means the queue will survive server restarts.
Terminal
docker exec -it rabbitmq rabbitmqadmin declare queue name=my-queue durable=true
Expected OutputExpected
Success
This command sends a message with the text 'Hello, world!' to the 'my-queue' queue. This simulates a producer sending a message.
Terminal
docker exec -it rabbitmq rabbitmqadmin publish routing_key=my-queue payload='Hello, world!'
Expected OutputExpected
Message published
This command retrieves a message from 'my-queue' without putting it back. This simulates a consumer receiving and processing the message.
Terminal
docker exec -it rabbitmq rabbitmqadmin get queue=my-queue requeue=false
Expected OutputExpected
Message 1 received { "payload": "Hello, world!", "payload_bytes": 13, "redelivered": false, "exchange": "", "routing_key": "my-queue", "message_count": 0 }
Key Concept

If you remember nothing else from this pattern, remember: a message queue safely stores messages so different parts of a system can communicate smoothly and reliably at their own pace.

Common Mistakes
Not creating a queue before sending messages
Messages have nowhere to go and will be lost or rejected.
Always declare the queue first before publishing messages to it.
Using non-durable queues for important messages
Messages and queues will be lost if RabbitMQ restarts.
Set queues as durable to keep messages safe across restarts.
Not consuming messages, causing queues to fill up
Queues can grow indefinitely, using too much memory and slowing down the system.
Make sure consumers are running to process and remove messages from queues.
Summary
Start RabbitMQ server with management interface using Docker.
Create a durable queue to hold messages safely.
Publish messages to the queue as a producer.
Consume messages from the queue as a consumer.