Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Single active consumer #31

Closed
invalidred opened this issue Aug 20, 2023 · 2 comments
Closed

[Question]: Single active consumer #31

invalidred opened this issue Aug 20, 2023 · 2 comments

Comments

@invalidred
Copy link

invalidred commented Aug 20, 2023

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
Copy link
Owner

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants