0
0
RabbitMQdevops~7 mins

Why producer-consumer is the basic messaging pattern in RabbitMQ - Why It Works

Choose your learning style9 modes available
Introduction
When different parts of a system need to talk without waiting for each other, they use messaging. The producer-consumer pattern helps one part send messages and another part receive them smoothly, even if they work at different speeds.
When a web app sends tasks to a background worker without making users wait
When a sensor device sends data to a server that processes it later
When an order system sends order details to a shipping system asynchronously
When you want to balance work between multiple workers without losing messages
When you need to make sure messages are not lost even if the receiver is busy or down
Config File - producer_consumer_example.py
producer_consumer_example.py
import pika

# Connect to RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# Declare a queue named 'task_queue'
channel.queue_declare(queue='task_queue', durable=True)

# Producer sends a message
message = 'Hello, Consumer!'
channel.basic_publish(
    exchange='',
    routing_key='task_queue',
    body=message,
    properties=pika.BasicProperties(delivery_mode=2)  # make message persistent
)
print("[Producer] Sent 'Hello, Consumer!'")

# Consumer callback function
def callback(ch, method, properties, body):
    print(f"[Consumer] Received {body.decode()}")
    ch.basic_ack(delivery_tag=method.delivery_tag)

# Consumer setup
channel.basic_qos(prefetch_count=1)  # fair dispatch
channel.basic_consume(queue='task_queue', on_message_callback=callback)

print('[Consumer] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

This Python script shows both producer and consumer using RabbitMQ.

Connection: Connects to RabbitMQ server on localhost.

Queue Declaration: Creates a durable queue named 'task_queue' to hold messages safely.

Producer: Sends a persistent message to the queue so it won't be lost if RabbitMQ restarts.

Consumer: Listens to the queue, processes messages one by one, and acknowledges each to tell RabbitMQ it was handled.

Commands
Runs the Python script that sends a message as a producer and starts a consumer to receive messages from the queue.
Terminal
python3 producer_consumer_example.py
Expected OutputExpected
[Producer] Sent 'Hello, Consumer!' [Consumer] Waiting for messages. To exit press CTRL+C [Consumer] Received Hello, Consumer!
Lists all queues on the RabbitMQ server to verify the 'task_queue' exists and check message counts.
Terminal
rabbitmqctl list_queues
Expected OutputExpected
Listing queues ... task_queue 0
Key Concept

If you remember nothing else from this pattern, remember: the producer-consumer pattern lets parts of a system send and receive messages independently, so work can be balanced and no messages get lost.

Common Mistakes
Not declaring the queue as durable
Messages can be lost if RabbitMQ restarts because the queue and messages are not saved.
Declare the queue with durable=True and send messages with delivery_mode=2 to make them persistent.
Not acknowledging messages in the consumer
RabbitMQ thinks messages are not processed and may resend them, causing duplicates or blocking the queue.
Call basic_ack after processing each message to tell RabbitMQ it can remove the message from the queue.
Producer and consumer running in the same script without separation
It mixes roles and can confuse the flow; in real use, they run separately to allow independent scaling.
Run producer and consumer as separate processes or scripts to keep roles clear and scalable.
Summary
Declare a durable queue to hold messages safely.
Producer sends persistent messages to the queue.
Consumer listens to the queue, processes messages, and acknowledges them to avoid duplicates.