Should I close the channel / connection after every post?

I use amqplib in Node.js and I don’t quite understand how best to use it in my code.

Basically, my current code calls amqp.connect() when the Node server starts up and then uses a different channel for each vendor and each user, never closing any of them. I would like to know if this makes sense, or if I need to create a channel, publish and close it every time I want to publish a message. What about communication? Is it a “good practice” to connect once and then keep it open for the life of my server?
On the consumer side - can I use one connection and one channel to listen in several queues?

Thanks for any clarification.

+5
source share
1 answer

In general, it is not recommended to open and close connections and channels for each message. Connections are durable, and resources are required to open and close them. For channels, they share a TCP connection with the connection, so they are lighter, but they will still consume memory and, of course, should not remain open after using them.

It is recommended to have a channel per stream and a channel for each consumer. But for publishing, it’s quite normal to use the same channel. But keep in mind that depending on the operations, the protocol can kill the channel in certain situations (for example, checking the existence of a queue), so prepare for this. There are also soft (customizable) and hard (usually 65535) restrictions on the maximum number of channels on many client implementations.

So, to summarize, depending on your use case, use one to several connections, open channels when you need them, and share them when it makes sense, but don't forget to close them when you're done.

The rabbitmq documentation explains the nature of the connections and channels (end of document). And the accepted answer to this question has good information on this issue.

+10
source

Source: https://habr.com/ru/post/1268532/


All Articles