RabbitMQ X-Consistent-Hashing with WSO2 ESB

Eranda Rajapakshe
3 min readAug 9, 2016

--

RabbitMQ support was given to WSO2 ESB from its 4.9.0 release. If you know the basic architecture of the RabbitMQ, you already know that RabbitMQ publishers are connected to exchanges and consumers are connected to queues. RabbitMQ exchanges and queues are connected with routing keys.

So lets say a publisher wants to make sure, total number of messages that it is sending shard between its consumer queues equally ( equal message distribution ) and to maintain this equal message distribution even when adding/removing queues.

In order to do this, user will have to add pretty complex configurations in the publishers’ side and in connecting queues to the exchange (using routing keys).But its almost impossible to do.

This is why we need RabbitMQ X-Consistent-Hashing exchanges. Its' operation can be simply explained as this,

RabbitMQ uses each messages’ routing key to generate a hash value. And the hash space will be divided among all the queues that are connected to the particular exchange. We can configure the ratio that messages should be divided between the queues using the routing keys. Message publisher should always set a new random value as the routing key for each new message that it sends to the RabbitMQ exchange.

I have created a simple use-case to demonstrate how the WSO2 ESB leverages the RabbitMQ X-Consistent-Hashing feature. I have you Linux based commands, but there won’t be much of a change for other Operating Systems as well.

1. Download, install and configure RabbitMQ [1]

All the initial configuration are mentioned here. [2]

Enabling X-Consistent-Hashing in RabbitMQ

  • List all the available plugins
sudo rabbitmq-plugins list
  • Enable consistent hashing plugin
sudo rabbitmq-plugins enable rabbitmq_consistent_hash_exchange
  • Restart RabbitMQ
sudo invoke-rc.d rabbitmq-server restart

2. Download, extract and configure WSO2 ESB

  • First download[3] and extract WSO2 ESB (4.9.0+)
  • Copy RabbitMQ library[4] to <ESB_HOME>/repository/components/lib/ directory.
  • Uncomment following line in <ESB_HOME>/repository/conf/axis2/axis2.xml
<transportSender name=”rabbitmq” class=”org.apache.axis2.transport.rabbitmq.RabbitMQSender”/>
  • Start WSO2 ESB

3. Configuring exchanges and queues in RabbitMQ

Abstract structure of the configuration in this example
  • Open the RabbitMQ Management UI
http://localhost:15672/
  • Create a new exchange named “hashedexchange” in the type “x-consistent-hash”
Create a new x-consistent-hash Exchange
  • Create several queues and bind them to the above created exchange

When we are creating a queue and bind it to the x-consistent-hashing exchange what we should know is that the routing key is always a numeric value and it represents the ratio out of the total number of messages how much of a proportion should go into that specific queue.

In this example I will create three queues. As follows,

hashed-consumer-1hashed-consumer-2hashed-consumer-3

hashed-consumer-1 and hashed-consumer-3 will be connected to hashedExchange with a routing key of 10. hashed-consumer-2 will connect with 20.

Create a new queue (Do this repeatedly for all the three queues)
Bind the newly created queue to the created hashed exchange (Do this repeatedly for all the three queues)

5. Configuring the Proxy in WSO2 ESB

Create the given RabbitMQHashing proxy in WSO2 ESB. In the address URI we have to specify following RabbitMQ Transport Sender parameters.

rabbitmq.server.host.name
rabbitmq.server.port
rabbitmq.exchange.name
rabbitmq.exchange.type

But we don’t have to provide a queue name or routing key. WSO2 ESB will generate a new random routing key for each processing message.

WSO2 ESB Proxy with RabbitMQ Transport Sender

6. Testing the scenario

In order to verify the functionality of the complete configuration, we need to send large number of messages into ESB RabbitMQHashing proxy. (Invoke the proxy) Its easier to use a JMeter script for this task.

Observation

According to the provided sample, hashed-consumer-1 , 2 and 3 will shared the incoming messages in the ratio of 1:2:1 respectively.

References

[1] https://www.rabbitmq.com/download.html

[2] http://notesbyeranda.blogspot.com/2015/04/start-with-rabbitmq.html

[3] http://wso2.com/products/enterprise-service-bus/

[4]. http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.rabbitmq%22%20AND%20a%3A%22amqp-client%22

--

--