There is an important configuration parameter in the Spring AMQP project CachingConnectionFactory class to understand how your application will connect to the RabbitMQ cluster. Let’s talk a little bit about this parameter.
AddressShuffleMode configuration parameter is used to specify the strategy used to connect the client library to the RabbitMQ cluster. This parameter is actual only when you have multiple host names provided in the spring.rabbitmq.addresses configuration property and it defines in which order the client will try to connect the nodes provided.
This configuration parameter has 3 possible values:
| Value | Description | Comment |
| INORDER | Shuffle the addresses after opening a connection, moving the first address to the end. | available since version 2.3 |
| NONE | Do not shuffle the addresses before or after opening a connection; attempt connections in a fixed order. | default before version 3.0 |
| RANDOM | Randomly shuffle the addresses before opening a connection; attempt connections in the new order. | default starting version 3.0 |
You can define the required mode while creating CachingConnectionFactory instance:
@Bean
public CachingConnectionFactory ccf() {
CachingConnectionFactory ccf = new CachingConnectionFactory();
ccf.setAddresses("host1:5672,host2:5672,host3:5672");
ccf.setAddressShuffleMode(AddressShuffleMode.INORDER);
return ccf;
}
Each of these modes has its own benefits and drawbacks. Let’s review them now.
| Mode | Benefits | Drawbacks |
| INORDER | guarantee all addresses are attempted before retrying the first | if the first address is not available for some reason will trigger the error before retrying to the next one |
| NONE | simple and predictable behavior good when you have specific knowledge about the RabbitMQ nodes health status or you have specific RabbitMQ cluster configuration | if the first address is not available for some reason will trigger the error before retrying to the next one the first address can be overloaded with all connections coming there |
| RANDOM | spreads connection attempts across all available addresses, potentially improving the chances of successful connection | introduces non-deterministic behavior, making troubleshooting potential connection issues more difficult |
Now, with all these details you can decide which AddressShuffleMode configuration is more applicable to your particular scenario. This parameter is equally important to understand from both the consumer and producer sides to make sure the configurations used for the producer and consumer are compatible and provide the best possible result.
Title image by Wolfgang Weiser from Pixabay