Single Active Consumer in LavinMQ
Single active consumer (SAC) lets multiple consumers connect to a queue while only one processes messages at a time, keeping delivery in order and enabling automatic failover.
What is a single active consumer?
Single active consumer is a pattern where multiple consumers connect to a queue, but only one is active at a time. The queue allows a single consumer to process messages while the others stay on standby.
The pattern suits cases that need in-order message processing and automatic failover. It applies whenever messages sharing an identifier must be processed in chronological order. If the active consumer crashes or disconnects, another consumer takes over, and message ordering stays intact because only one consumer processes messages at any point.
See the Prosa case study for an example of message ordering that matters, and the single active consumer documentation for configuration details.
Demo
The following steps show the single active consumer behavior in practice.
A basic application consumes messages from a single-active-consumer-enabled queue named sac. The Python sample code covers this. Running two instances of the consumer in two separate terminals produces this output in both:
Terminal 1 - Consumer 1
->$ python sac_consumer.py
[OK] Connection over channel established
[WAIT] Waiting for messages. To exit press CTRL+C
Terminal 2 - Consumer 2
->$ python sac_consumer.py
[OK] Connection over channel established
[WAIT] Waiting for messages. To exit press CTRL+C
A second application publishes four messages to the sac queue, again using the Python sample code. Running it produces this output:
Terminal 3 - Producer
->$ python sac_producer.py
[OK] Connection over channel established
[SENT] Message sent to queue: SAC message 1
[SENT] Message sent to queue: SAC message 2
[SENT] Message sent to queue: SAC message 3
[SENT] Message sent to queue: SAC message 4
[CLOSED] Connection closed
Right after the messages are published, the first consumer handles all of them. The second consumer receives nothing, because the first consumer bound to the sac queue already became the active consumer.
Terminal 1 - Consumer 1
->$ python sac_consumer.py
[OK] Received: SAC message 1
[OK] SAC message processed!
[OK] Received: SAC message 2
[OK] SAC message processed!
[OK] Received: SAC message 3
[OK] SAC message processed!
[OK] Received: SAC message 4
[OK] SAC message processed!
Terminal 2 - Consumer 2
->$ python sac_consumer.py
[OK] Connection over channel established
[WAIT] Waiting for messages. To exit press CTRL+C
Running the producer again while terminating the first consumer right after its second message simulates an active consumer crash. As soon as the active consumer disconnects, the second consumer takes over and starts receiving the remaining messages, still in order.
Summary
Single active consumer delivers in-order processing and automatic failover without extra coordination logic in an application. Only one consumer is ever active on a queue, and if it disconnects, LavinMQ hands the role to the next consumer in line while message order stays intact.
The single active consumer documentation covers additional configuration options.
Nyior Clement