3
votes

The common advice I've read for creating channels for RabbitMQ recommends using a single channel per thread. But in node.js, we don't manage threads at all. So when do we create channels?

My use case is that of a node web server, using AMQPLib, that needs to use a request/response pattern to communicate with a single RabbitMQ server. Each HTTP request may require multiple RabbitMQ requests in order to generate the HTTP response. I plan to use a single Rabbit connection per node process, but as far as how much to reuse channels for various requests or response queues, I'm not certain.

An add-on question: If the answer is to use a channel for each separate request, then will there be much of a latency penalty for having to create a channel before each message sent?

1
Create a channel for every units of work. - sturcotte06
Your first intuition is correct, you should use one connection per node process. The amqblib module is heavily based on streams, and therefore will not selfishly tie up your event loop. Additionally, there is a heavy cost to creating and tearing down channels relative to the cost of publishing data on a channel, so you should avoid unnecessary channel creation. If you're using multiple queues you should create one channel per queue per node process. - aembke

1 Answers

2
votes

Channels are an AMQP protocol-level construct. They really have nothing to do with the underlying connection (other than the obvious fact that a connection is required in order to have a channel). The .NET implementation of RabbitMQ client is so poorly written that it threadlocks on channels, hence one channel per thread - this is a code limitation, not a protocol limitation.

There is a comment stating that there is a "heavy cost to creating" channels - I don't see how this could be true based on the construct of a channel, but I don't know.

In any case, to answer your question: don't create more channels than you need. If you can operate using one channel (and it sounds like you can), do so. Don't create more work for yourself.