Introduction
Configuration
Language Support
AMQP 0-9-1 Overview
More Exchange Types
More Consumer Features
Queue Deep-dive
Other Features
Reliable Message Delivery
High Availability
Monitoring
Management HTTP API
Tutorials
Networking
LavinMQ CLI
Single Active Consumer
What is the Single Active Consumer?
A Single Active Consumer permits only one consumer to process messages from a specific queue. If the active consumer becomes unavailable, the queue automatically fails to the next available consumer.
What benefits does the Single Active Consumer have?
- Guarantees that messages are processed in the exact order they arrive in the queue.
- Ensures a single consumer executes a task at a time.
- Eliminates conflicts from multiple consumers processing the same message.
When to use a Single Active Consumer?
- Maintaining event order is critical (e.g., when processing ticket requests).
- Preventing conflicts from parallel processing is necessary.
- Tasks require exclusive, sequential execution.
Single Active Consumer can be enabled when declaring a queue, with the x-single-active-consumer
argument set to True
.
Example: Declaring a queue with a single active consumer
import pika
connection = pika.BlockingConnection(pika.URLParameters('host-url'))
channel = connection.channel()
channel.queue_declare(
queue="sac",
durable=True,
arguments={"x-single-active-consumer":True}
)
connection.close()
For a queue with single active consumer enabled, registering consumers results in the following:
- The first consumer registered with the queue becomes the single active consumer.
- If the active consumer becomes unavailable, the queue selects the next consumer from the pool in connection order.
Consumer Exclusivity vs Single Active Consumer
A Single Active Consumer is not the only way to ensure that a single consumer processes messages from a queue. Consumer Exclusivity offers an alternative. It assigns one consumer as the sole processor for a specific queue, ensuring only one consumer handles messages. However, if the exclusive consumer disconnects or stops, the application must register a new consumer to continue processing—unlike the Single Active Consumer, which automatically fails over to the next available consumer.
Example: Consumer exclusivity
import pika
connection = pika.BlockingConnection(pika.URLParameters('host-url'))
channel = connection.channel()
def callback(ch, method, properties, msg):
print(f"[✅] { msg }")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(
"ex_queue",
callback,
auto_ack=False,
exclusive**=**True
)
connection.close()
Ready to take the next steps? Here are some things you should keep in mind:
Managed LavinMQ instance on CloudAMQP
LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.
Help and feedback
We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.