An overview of the


In RabbitMq, a channel is a logical or virtual connection that belongs to a TCP connection. Multiple channels can be created within a TCP connection, and in Rabbit MQ messages are sent and received based on channels.

With a TCP connection, a channel is needed for the following reasons:

  • Create and destroyTCPThe connection is time-consuming;
  • Open too muchTCPConnection, consumption of operating system resources, concurrency to a certain extent, the system throughput will be reduced;
  • The use of acollectionmorechannelTo improve connection utilization.

Therefore, it is reasonable to use multiple channels to multiplex a TCP connection.


The channel thread is unsafe


A channel is not thread-safe and can cause problems if threads concurrently access the same channel. There are several ways to handle this:

  1. Global common onechannelAnd use global locks to allow operationschannelQueuing. This obvious performance is not good;
  2. One thread creates a new onechannelBut deal with the maximum that a connection can supportchannelThe number of;
  3. One thread for each threadchannelBut fromchannelThe pool doesn’t create a new one every time. Once a thread completes onechannelIt will be returned to the pool, thus making thechannelCan be used for another thread.

For small quantities, use the second method. If the amount is large, the third method is recommended. After all, creating and destroying channels is also time-consuming and resource-consuming. In Spring AMQP, a scheme for caching channels is provided. You can specify the caching mode when you create a CachingConnectionFactory.

connectionFactory.setCacheMode(CachingConnectionFactory.CacheMode.CHANNEL);
connectionFactory.setChannelCacheSize(25);
Copy the code

The above two lines of code indicate that channels share a single connection, and that 25 channels are cached. Note that 25 does not mean that the connection can only create 25 channels, but that it can cache 25 channels at most. For example, suppose that concurrent sent 100 messages, in CachingConnectionFactory. CacheMode. CHANNEL mode, the moment will create 100 – CHANNEL, and then put into the cache 25 CHANNEL, when the flow down, The newly created extra channels will be closed automatically, leaving only 25 in the cache.

To use this method, ensure that the number of channels in the cache is not too small; otherwise, a large amount of traffic will cause frequent channel closure. We cannot create as many channels as possible, but we can limit the number of channels as possible:

connectionFactory.setChannelCheckoutTimeout(1000);
Copy the code

If the ChannelCheckoutTimeout value is greater than 0, the ChannelCacheSize value is the maximum number of channels that can be retrieved from the cache. If the ChannelCheckoutTimeout is not available, AmqpTimeoutException is thrown.

We can also implement channel Pool ourselves, but it is not recommended to do so, since Spring AMQP is quite mature and can be used directly.


Cachemode. CHANNEL mode performance


As mentioned above, cachemode. CHANNEL is one thread, one CHANNEL, and these channels share the same connection, i.e., the same socket. When there is a large amount of concurrency, multiple threads may want to write data to the socket at the same time. To avoid this, the only way to do this is to block the thread that can’t get the lock. Using spring-rabbit under high throughput stress test, 10 threads can send 1,000,000 messages at a time. The result is that the thread is blocked.

CacheMode.CONNECTION
stackoverflow
Spring CachingConnectionFactory limiting channels & causing Thread Blocking


The channel of the monitor


The RabbitMQ Admin UI provides an interface for monitoring channels. We focus on two main points:

  • Is it possible for a channel to leak? A channel is opened but not closed;
  • Rate at which a channel is opened and closed.

If the rate of channel open operations is consistently higher than the rate of channel close operations, a channel leak may occur. The diagram below:

It is also worth observing if the rate at which channels are opened and closed is high. There may be no cached channel. When the traffic continues to increase, there may be a situation of poor throughput, as shown in the following figure:

The original link


Spring RabbitMQ Channel understand