cody-greene / node-rabbitmq-client

RabbitMQ (0-9-1) client library with auto-reconnect & zero dependencies
MIT License
130 stars 9 forks source link

[Question]: Single active consumer #31

Closed invalidred closed 1 year ago

invalidred commented 1 year ago

RabbitMq supports single active consumer which is useful in my scenario where I run all my consumers in a Pod in Kubernetes with N replicas. For consumers where ordering is necessary, I set appropriate headers to ensure only one consumer will be processing messages (with concurrency of 1) from the queue, while the other consumer will wait for the active consumer to disconnect etc.. This works nicely as for consumers where ordering does not matter, I can increase throughput by increasing replica count.

I was wondering if I were to use this library (and I really LOVE the consumer/publisher abstractions... the best I've seen so far!), can we set single active consumer option or if that's how it works under hood?

I hope my questions makes sense.

cody-greene commented 1 year ago

Yes, according to the official docs:

Single active consumer can be enabled when declaring a queue, with the x-single-active-consumer argument set to true

So you can do that with:

const rabbit = new Connection()

// if you just want to create the queue (imperative)
await rabbit.queueDeclare({
  queue: 'my-queue',
  arguments: {'x-single-active-consumer': true}
})

// or when creating a consumer (declarative)
const sub = rabbit.createConsumer({
  queue: 'my-queue',
  queueOptions: {
    durable: true, // etc...
    arguments: {'x-single-active-consumer': true}
  },
}, (msg) => {
  console.log(msg)
})

I simply haven't added type definitions for all the x- extensions, so you may not see this sort of thing in my generated docs. See also QueueDeclare arguments:

arguments?: {
  ...
  [k: string]: any
},
invalidred commented 1 year ago

@cody-greene this is fantastic! Thank you so much!