Drewll
Drewll

Reputation: 328

Is There a Way to Limit the Number of Consumers on a RabbitMQ Queue?

I've got a RabbitMQ setup with lots of queues. Due to the nature of the data in each queue, it has to be processed in strict order, so we can only permit a single consumer on each queue. This isn't a problem as such, but we do run the risk of accidentally starting a second consumer on a queue, which would be a bad thing. There's lots of queues and lots of app servers and it would just take a small typo for us to end up in this situartion.

Before I spend time changing the software to effectively "lock" a queue (storing that lock in a DB or something), is there anything in RabbitMQ that can limit the number of consumers a queue can have? If so, I can limit my queues to just one consumer and my risk of multiple consumers goes away.

Cheers!

Upvotes: 7

Views: 7598

Answers (3)

Most Noble Rabbit
Most Noble Rabbit

Reputation: 2776

I am late to the party, but for whoever might encounter this - if you need exactly one active consumer, you can use the new feature (since 3.8) - https://www.rabbitmq.com/consumers.html#single-active-consumer

Single Active Consumer

Single active consumer allows to have only one consumer at a time consuming from a queue and to fail over to another registered consumer in case the active one is cancelled or dies.

Upvotes: 1

kelgwiin
kelgwiin

Reputation: 817

If you are using sprig framework you can use the tag exclusive=truein the tag rabbit listener @RabbitListener. So with you are able to restrict the queue to one consumer.

@RabbitListener(queues = "${queue.name}", exclusive = true)

Upvotes: 3

user8808265
user8808265

Reputation: 2053

The exclusive flag in consume method serves this purpose.

Upvotes: 11

Related Questions