2011-09-25 02:21:18 +08:00
# RabbitMQ Consistent Hash Exchange Type
2016-08-14 12:22:45 +08:00
## Introduction
2015-06-04 19:50:52 +08:00
2016-08-14 12:19:10 +08:00
This plugin adds a consistent-hash exchange type to RabbitMQ. This
2018-08-19 19:09:00 +08:00
exchange type uses consistent hashing (intro blog posts: [one ](http://www.martinbroadhurst.com/Consistent-Hash-Ring.html ), [two ](http://michaelnielsen.org/blog/consistent-hashing/ ), [three ](https://akshatm.svbtle.com/consistent-hash-rings-theory-and-implementation )) to distribute
2016-08-14 12:19:10 +08:00
messages between the bound queues. It is recommended to get a basic understanding of the
2018-08-19 19:09:00 +08:00
concept before evaluating this plugin and its alternatives.
2011-09-25 02:21:18 +08:00
2016-08-14 12:22:45 +08:00
[rabbitmq-sharding ](https://github.com/rabbitmq/rabbitmq-sharding ) is another plugin
that provides a way to partition a stream of messages among a set of consumers
while trading off total stream ordering for processing parallelism.
## Problem Definition
2018-08-19 19:09:00 +08:00
In various scenarios it may be desired to ensure that messages sent to an
exchange are reasonably [uniformly distributed ](https://en.wikipedia.org/wiki/Uniform_distribution_(discrete )) across a number of
queues based on the routing key of the message, a [nominated
header](#routing-on-a-header), or a [message property ](#routing-on-a-header ).
Technically this can be accomplished using a direct or topic exchange,
binding queues to that exchange and then publishing messages to that exchange that
2015-09-03 00:33:32 +08:00
match the various binding keys.
2011-09-25 02:21:18 +08:00
However, arranging things this way can be problematic:
1. It is difficult to ensure that all queues bound to the exchange
2018-08-19 19:09:00 +08:00
will receive a (roughly) equal number of messages (distribution uniformity)
without baking in to the publishers quite a lot of knowledge about the number of queues and
2011-09-25 02:39:28 +08:00
their bindings.
2011-09-25 02:21:18 +08:00
2018-08-19 19:09:00 +08:00
2. When the number of queues changes, it is not easy to ensure that the
2011-09-25 02:21:18 +08:00
new topology still distributes messages between the different queues
2022-07-01 16:29:00 +08:00
roughly evenly.
2011-09-25 02:21:18 +08:00
2019-03-20 16:13:58 +08:00
[Consistent Hashing ](https://en.wikipedia.org/wiki/Consistent_hashing )
2011-09-25 02:21:18 +08:00
is a hashing technique whereby each bucket appears at multiple points
throughout the hash space, and the bucket selected is the nearest
2011-10-18 22:36:28 +08:00
higher (or lower, it doesn't matter, provided it's consistent) bucket
to the computed hash (and the hash space wraps around). The effect of
this is that when a new bucket is added or an existing bucket removed,
only a very few hashes change which bucket they are routed to.
2011-09-25 02:21:18 +08:00
2022-07-01 16:29:00 +08:00
## Purpose
2018-08-19 19:09:00 +08:00
2022-07-01 16:29:00 +08:00
The purpose of this exchange type is to help developers achieve
a reasonably even message flow distribution between a number of queues.
2018-08-19 19:09:00 +08:00
2022-07-01 16:29:00 +08:00
## Installation
2018-08-19 19:09:00 +08:00
2022-07-01 16:29:00 +08:00
This plugin ships with RabbitMQ.
2018-08-19 19:09:00 +08:00
2020-06-13 22:28:11 +08:00
## Enabling the Plugin
2018-08-19 21:59:30 +08:00
2020-06-13 22:28:11 +08:00
This plugin ships with RabbitMQ. Like all other [RabbitMQ plugins ](https://www.rabbitmq.com/plugins.html ),
it has to be enabled before it can be used:
2018-08-19 21:59:30 +08:00
``` sh
rabbitmq-plugins enable rabbitmq_consistent_hash_exchange
```
2020-06-13 22:28:11 +08:00
## Provided Exchange Type
2020-06-12 13:39:12 +08:00
The exchange type is `"x-consistent-hash"` .
2018-08-19 19:09:00 +08:00
## How It Works
2015-06-04 19:50:52 +08:00
2011-09-25 02:21:18 +08:00
In the case of Consistent Hashing as an exchange type, the hash is
2018-08-19 19:09:00 +08:00
calculated from a message property (most commonly the routing key).
2020-06-13 22:28:11 +08:00
When a queue is bound to this exchange, it is assigned one or more
partitions on the consistent hashing ring depending on its binding weight
(covered below).
For every property hash (e.g. routing key), a hash position computed
and a corresponding hash ring partition is picked. That partition corresponds
to a bound queue, and the message is routed to that queue.
Assuming a reasonably even routing key distribution of inbound messages,
routed messages should be reasonably evenly distributed across all
ring partitions, and thus queues according to their binding weights.
2011-09-25 02:21:18 +08:00
2022-07-01 16:29:00 +08:00
### Bindings and Hash Ring Buckets
#### One Binding Per Queue
This exchange type **assumes a single binding between a queue and an exchange** .
2024-07-24 17:42:59 +08:00
This will be enforced in the code:
2022-07-01 16:29:00 +08:00
when multiple bindings are created, only the first one will actually update the ring.
This limitation makes most semantic sense: the purpose is to achieve
a reasonably even message flow distribution between queues.
#### Weights
2011-09-25 02:21:18 +08:00
2018-08-19 19:09:00 +08:00
When a queue is bound to a Consistent Hash exchange, the binding key
is a number-as-a-string which indicates the binding weight: the number
of buckets (sections of the range) that will be associated with the
target queue.
2022-07-01 16:29:00 +08:00
In most environments, using **one bucket per binding** (and thus queue)
is highly recommended as it is the simplest way to achieve reasonably
even balancing.
2018-08-19 19:09:00 +08:00
### Consistent Hashing-based Routing
The hashing distributes *routing keys* among queues, not *message payloads*
2015-10-29 05:12:01 +08:00
among queues; all messages with the same routing key will go the
same queue. So, if you wish for queue A to receive twice as many
routing keys routed to it than are routed to queue B, then you bind
the queue A with a binding key of twice the number (as a string --
binding keys are always strings) of the binding key of the binding
2016-06-21 00:34:10 +08:00
to queue B. Note this is only the case if your routing keys are
evenly distributed in the hash space. If, for example, only two
distinct routing keys are used on all the messages, there's a chance
both keys will route (consistently!) to the same queue, even though
other queues have higher values in their binding key. With a larger
set of routing keys used, the statistical distribution of routing
keys approaches the ratios of the binding keys.
2011-09-25 02:39:28 +08:00
2020-06-12 13:39:12 +08:00
Each message gets delivered to at most one queue. On average, a
message gets delivered to exactly one queue. Concurrent binding changes
2022-07-01 16:29:00 +08:00
and queue primary replica failures can temporarily affect this but
over the long term, assuming equal weights of every binding,
the distribution should be roughly even.
2011-09-25 02:21:18 +08:00
2020-06-12 13:39:12 +08:00
### Node Restart Effects
Consistent hashing ring is stored in memory and will be re-populated
from exchange bindings when the node boots. Relative positioning of queues
on the ring is not guaranteed to be the same between restarts. In practice
this means that after a restart, all queues will still receive roughly
the same number of messages routed to them (assuming routing key distribution
does not change) but a given routing key now **may route to a different queue** .
In other words, this exchange type provides consistent message distribution
2022-07-01 16:29:00 +08:00
between queues but cannot guarantee stable queue routing locality for messages
with a given routing key.
2011-09-25 02:21:18 +08:00
2015-06-04 19:50:52 +08:00
2018-08-19 19:20:35 +08:00
## Usage Example
2015-06-04 19:50:52 +08:00
2018-08-19 19:20:35 +08:00
### The Topology
2015-06-04 19:50:52 +08:00
2018-08-19 19:09:00 +08:00
In the below example the queues `q0` and `q1` get bound each with the weight of 1
in the hash space to the exchange `e` which means they'll each get
roughly the same number of routing keys. The queues `q2` and `q3`
however, get 2 buckets each (their weight is 2) which means they'll each get roughly the
same number of routing keys too, but that will be approximately twice
2018-08-19 19:20:35 +08:00
as many as `q0` and `q1` .
2018-08-19 19:09:00 +08:00
Note the `routing_key` s in the bindings are numbers-as-strings. This
is because AMQP 0-9-1 specifies the `routing_key` field must be a string.
2018-08-19 19:20:35 +08:00
### Choosing Appropriate Weight Values
The example uses low weight values intentionally.
2022-07-01 16:29:00 +08:00
2018-08-19 19:20:35 +08:00
Higher values will reduce throughput of the exchange, primarily for
workloads that experience a high binding churn (queues are bound to
and unbound from a consistent hash exchange frequently).
2022-07-01 16:29:00 +08:00
Equal weights of 1 for all bindings are recommended (and sufficient for most use cases).
2018-08-19 19:20:35 +08:00
### Inspecting Message Counts
The example then publishes 100,000 messages to our
exchange with random routing keys, the queues will get their share of
messages roughly equal to the binding keys ratios. After this has
completed, message distribution between queues can be inspected using
RabbitMQ's management UI and `rabbitmqctl list_queues` .
## Routing Keys and Uniformity of Distribution
2018-08-19 19:09:00 +08:00
It is important to ensure that the messages being published
2018-08-19 19:20:35 +08:00
to the exchange have varying routing keys: if a very
2018-08-19 19:09:00 +08:00
small set of routing keys are being used then there's a possibility of
2018-08-19 19:20:35 +08:00
messages not being evenly distributed between the bound queues. With a
large number of bound queues some queues may get no messages routed to
them at all.
If pseudo-random or unique values such as client/session/request identifiers
are used for routing keys (or another property used for hashing) then
reasonably uniform distribution should be observed.
2015-06-04 19:50:52 +08:00
2018-08-21 23:49:18 +08:00
### Executable Versions
Executable versions of some of the code examples can be found under [./examples ](./examples ).
2018-08-19 21:35:53 +08:00
### Code Example in Python
2018-08-19 20:59:31 +08:00
2018-08-20 20:13:28 +08:00
This version of the example uses [Pika ](https://pika.readthedocs.io/en/stable/ ), the most widely used Python client for RabbitMQ:
2018-08-19 20:59:31 +08:00
``` python
#!/usr/bin/env python
import pika
import time
conn = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
ch = conn.channel()
2018-08-19 21:35:53 +08:00
ch.exchange_declare(exchange="e", exchange_type="x-consistent-hash", durable=True)
2018-08-19 20:59:31 +08:00
for q in ["q1", "q2", "q3", "q4"]:
2018-08-19 21:35:53 +08:00
ch.queue_declare(queue=q, durable=True)
ch.queue_purge(queue=q)
2018-08-19 20:59:31 +08:00
for q in ["q1", "q2"]:
2018-08-19 21:35:53 +08:00
ch.queue_bind(exchange="e", queue=q, routing_key="1")
2018-08-19 20:59:31 +08:00
for q in ["q3", "q4"]:
2018-08-19 21:35:53 +08:00
ch.queue_bind(exchange="e", queue=q, routing_key="2")
2018-08-19 20:59:31 +08:00
n = 100000
for rk in list(map(lambda s: str(s), range(0, n))):
2018-08-19 21:35:53 +08:00
ch.basic_publish(exchange="e", routing_key=rk, body="")
2018-08-19 20:59:31 +08:00
print("Done publishing.")
print("Waiting for routing to finish...")
# in order to keep this example simpler and focused,
# wait for a few seconds instead of using publisher confirms and waiting for those
time.sleep(5)
print("Done.")
conn.close()
```
2018-08-20 18:39:35 +08:00
### Code Example in Java
Below is a version of the example that uses
the official [RabbitMQ Java client ](https://www.rabbitmq.com/api-guide.html ):
``` java
package com.rabbitmq.examples;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;
public class ConsistentHashExchangeExample1 {
private static String CONSISTENT_HASH_EXCHANGE_TYPE = "x-consistent-hash";
2018-08-20 19:10:23 +08:00
public static void main(String[] argv) throws IOException, TimeoutException, InterruptedException {
2018-08-20 18:39:35 +08:00
ConnectionFactory cf = new ConnectionFactory();
Connection conn = cf.newConnection();
Channel ch = conn.createChannel();
for (String q : Arrays.asList("q1", "q2", "q3", "q4")) {
ch.queueDeclare(q, true, false, false, null);
ch.queuePurge(q);
}
ch.exchangeDeclare("e1", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
for (String q : Arrays.asList("q1", "q2")) {
ch.queueBind(q, "e1", "1");
}
for (String q : Arrays.asList("q3", "q4")) {
ch.queueBind(q, "e1", "2");
}
ch.confirmSelect();
AMQP.BasicProperties.Builder bldr = new AMQP.BasicProperties.Builder();
for (int i = 0; i < 100000 ; i + + ) {
ch.basicPublish("e1", String.valueOf(i), bldr.build(), "".getBytes("UTF-8"));
}
ch.waitForConfirmsOrDie(10000);
System.out.println("Done publishing!");
System.out.println("Evaluating results...");
// wait for one stats emission interval so that queue counters
// are up-to-date in the management UI
Thread.sleep(5);
System.out.println("Done.");
conn.close();
}
}
```
2018-08-19 21:35:53 +08:00
### Code Example in Ruby
2018-08-19 20:59:31 +08:00
Below is a version that uses [Bunny ](http://rubybunny.info ), the most widely used
Ruby client for RabbitMQ:
``` ruby
#!/usr/bin/env ruby
require 'bunny'
conn = Bunny.new
conn.start
ch = conn.create_channel
ch.confirm_select
q1 = ch.queue("q1", durable: true)
q2 = ch.queue("q2", durable: true)
q3 = ch.queue("q3", durable: true)
q4 = ch.queue("q4", durable: true)
[q1, q2, q3, q4]. each(& :purge)
x = ch.exchange("chx", type: "x-consistent-hash", durable: true)
[q1, q2].each { |q| q.bind(x, routing_key: "1") }
[q3, q4].each { |q| q.bind(x, routing_key: "2") }
n = 100_000
n.times do |i|
x.publish(i.to_s, routing_key: i.to_s)
end
ch.wait_for_confirms
2018-08-21 23:31:56 +08:00
puts "Done publishing!"
2018-08-19 20:59:31 +08:00
# wait for queue stats to be emitted so that management UI numbers
# are up-to-date
sleep 5
conn.close
puts "Done"
```
2018-08-19 21:35:53 +08:00
### Code Example in Erlang
2015-06-04 19:50:52 +08:00
2018-08-19 20:59:31 +08:00
Below is a version of the example that uses
the [RabbitMQ Erlang client ](https://www.rabbitmq.com/erlang-client-user-guide.html ):
2011-09-25 02:21:18 +08:00
2018-08-19 20:59:31 +08:00
``` erlang
2015-06-04 19:50:52 +08:00
-include_lib("amqp_client/include/amqp_client.hrl").
2016-06-21 00:34:10 +08:00
2015-06-04 19:50:52 +08:00
test() ->
{ok, Conn} = amqp_connection:start(#amqp_params_network{}),
{ok, Chan} = amqp_connection:open_channel(Conn),
Queues = [< < "q0">>, < < "q1">>, < < "q2">>, < < "q3">>],
amqp_channel:call(Chan,
2018-08-19 21:35:53 +08:00
#'exchange.declare'{
2015-06-04 19:50:52 +08:00
exchange = < < "e">>, type = < < "x-consistent-hash">>
}),
2018-08-19 21:35:53 +08:00
[amqp_channel:call(Chan, #'queue.declare'{queue = Q}) || Q < - Queues ] ,
[amqp_channel:call(Chan, #'queue.bind'{queue = Q,
exchange = < < "e">>,
routing_key = < < "1">>})
2015-06-04 19:50:52 +08:00
|| Q < - [ < < " q0 " > >, < < "q1">>]],
2018-08-19 21:35:53 +08:00
[amqp_channel:call(Chan, #'queue.bind' {queue = Q,
exchange = < < "e">>,
routing_key = < < "2">>})
2015-06-04 19:50:52 +08:00
|| Q < - [ < < " q2 " > >, < < "q3">>]],
2018-08-19 21:59:30 +08:00
RK = list_to_binary(integer_to_list(random:uniform(1000000))),
2018-08-19 21:35:53 +08:00
Msg = #amqp_msg {props = #'P_basic'{}, payload = << >>},
2015-06-04 19:50:52 +08:00
[amqp_channel:call(Chan,
#'basic.publish'{
exchange = < < "e">>,
2018-08-19 21:59:30 +08:00
routing_key = RK
2018-08-19 21:35:53 +08:00
}, Msg) || _ < - lists:seq ( 1 , 100000 ) ] ,
2015-06-04 19:50:52 +08:00
amqp_connection:close(Conn),
ok.
2016-06-21 00:34:10 +08:00
```
2011-09-25 02:21:18 +08:00
2018-08-19 19:20:35 +08:00
## Configuration
### Routing on a Header
2013-01-09 19:10:05 +08:00
Under most circumstances the routing key is a good choice for something to
2018-08-19 21:35:53 +08:00
hash. However, in some cases it is necessary to use the routing key for some other
2013-01-09 19:10:05 +08:00
purpose (for example with more complex routing involving exchange to
2018-08-19 21:35:53 +08:00
exchange bindings). In this case it is possible to configure the consistent hash
2013-01-09 19:10:05 +08:00
exchange to route based on a named header instead. To do this, declare the
exchange with a string argument called "hash-header" naming the header to
2018-08-19 21:35:53 +08:00
be used.
2024-07-24 17:42:59 +08:00
When a `"hash-header"` is specified, the chosen header should be provided.
2018-08-19 21:59:30 +08:00
If published messages do not contain the header, they will all get
routed to the same **arbitrarily chosen** queue.
2018-08-19 21:35:53 +08:00
#### Code Example in Python
``` python
#!/usr/bin/env python
import pika
import time
conn = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
ch = conn.channel()
args = {u'hash-header': u'hash-on'}
ch.exchange_declare(exchange='e2',
exchange_type='x-consistent-hash',
arguments=args,
durable=True)
for q in ['q1', 'q2', 'q3', 'q4']:
ch.queue_declare(queue=q, durable=True)
ch.queue_purge(queue=q)
for q in ['q1', 'q2']:
ch.queue_bind(exchange='e2', queue=q, routing_key='1')
for q in ['q3', 'q4']:
ch.queue_bind(exchange='e2', queue=q, routing_key='2')
n = 100000
for rk in list(map(lambda s: str(s), range(0, n))):
hdrs = {u'hash-on': rk}
ch.basic_publish(exchange='e2',
routing_key='',
body='',
properties=pika.BasicProperties(content_type='text/plain',
delivery_mode=2,
headers=hdrs))
print('Done publishing.')
print('Waiting for routing to finish...')
# in order to keep this example simpler and focused,
# wait for a few seconds instead of using publisher confirms and waiting for those
time.sleep(5)
print('Done.')
conn.close()
```
2018-08-20 19:10:23 +08:00
#### Code Example in Java
``` java
package com.rabbitmq.examples;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
public class ConsistentHashExchangeExample2 {
public static final String EXCHANGE = "e2";
private static String EXCHANGE_TYPE = "x-consistent-hash";
public static void main(String[] argv) throws IOException, TimeoutException, InterruptedException {
ConnectionFactory cf = new ConnectionFactory();
Connection conn = cf.newConnection();
Channel ch = conn.createChannel();
for (String q : Arrays.asList("q1", "q2", "q3", "q4")) {
ch.queueDeclare(q, true, false, false, null);
ch.queuePurge(q);
}
Map< String , Object > args = new HashMap< >();
args.put("hash-header", "hash-on");
ch.exchangeDeclare(EXCHANGE, EXCHANGE_TYPE, true, false, args);
for (String q : Arrays.asList("q1", "q2")) {
ch.queueBind(q, EXCHANGE, "1");
}
for (String q : Arrays.asList("q3", "q4")) {
ch.queueBind(q, EXCHANGE, "2");
}
ch.confirmSelect();
for (int i = 0; i < 100000 ; i + + ) {
AMQP.BasicProperties.Builder bldr = new AMQP.BasicProperties.Builder();
Map< String , Object > hdrs = new HashMap< >();
hdrs.put("hash-on", String.valueOf(i));
ch.basicPublish(EXCHANGE, "", bldr.headers(hdrs).build(), "".getBytes("UTF-8"));
}
ch.waitForConfirmsOrDie(10000);
System.out.println("Done publishing!");
System.out.println("Evaluating results...");
// wait for one stats emission interval so that queue counters
// are up-to-date in the management UI
Thread.sleep(5);
System.out.println("Done.");
conn.close();
}
}
```
2018-08-19 21:35:53 +08:00
#### Code Example in Ruby
``` ruby
#!/usr/bin/env ruby
require 'bundler'
Bundler.setup(:default, :test)
require 'bunny'
conn = Bunny.new
conn.start
ch = conn.create_channel
ch.confirm_select
q1 = ch.queue("q1", durable: true)
q2 = ch.queue("q2", durable: true)
q3 = ch.queue("q3", durable: true)
q4 = ch.queue("q4", durable: true)
[q1, q2, q3, q4]. each(& :purge)
x = ch.exchange("x2", type: "x-consistent-hash", durable: true, arguments: {"hash-header" => "hash-on"})
[q1, q2].each { |q| q.bind(x, routing_key: "1") }
[q3, q4].each { |q| q.bind(x, routing_key: "2") }
n = 100_000
(0..n).map(& :to_s).each do |i|
x.publish(i.to_s, routing_key: rand.to_s, headers: {"hash-on": i})
end
ch.wait_for_confirms
puts "Done publishing!"
2018-08-21 23:31:56 +08:00
# wait for queue stats to be emitted so that management UI numbers
# are up-to-date
2018-08-19 21:35:53 +08:00
sleep 5
2018-08-21 23:31:56 +08:00
conn.close
2018-08-19 21:35:53 +08:00
puts "Done"
```
#### Code Example in Erlang
With RabbitMQ Erlang client:
2013-01-09 19:10:05 +08:00
2018-08-19 19:20:35 +08:00
``` erlang
2018-08-19 21:35:53 +08:00
-include_lib("amqp_client/include/amqp_client.hrl").
test() ->
{ok, Conn} = amqp_connection:start(#amqp_params_network{}),
{ok, Chan} = amqp_connection:open_channel(Conn),
Queues = [< < "q0">>, < < "q1">>, < < "q2">>, < < "q3">>],
2013-01-09 19:10:05 +08:00
amqp_channel:call(
2018-08-19 21:35:53 +08:00
Chan, #'exchange.declare'{
2013-01-09 19:10:05 +08:00
exchange = < < "e">>,
type = < < "x-consistent-hash">>,
2018-08-19 21:35:53 +08:00
arguments = [{< < "hash-header">>, longstr, < < "hash-on">>}]
}),
[amqp_channel:call(Chan, #'queue.declare'{queue = Q}) || Q < - Queues ] ,
[amqp_channel:call(Chan, #'queue.bind' {queue = Q,
exchange = < < "e">>,
routing_key = < < "1">>})
|| Q < - [ < < " q0 " > >, < < "q1">>]],
[amqp_channel:call(Chan, #'queue.bind' {queue = Q,
exchange = < < "e">>,
routing_key = < < "2">>})
|| Q < - [ < < " q2 " > >, < < "q3">>]],
RK = list_to_binary(integer_to_list(random:uniform(1000000))),
Msg = #amqp_msg {props = #'P_basic'{headers = [{<< "hash-on">>, longstr, RK}]}, payload = << >>},
[amqp_channel:call(Chan,
#'basic.publish'{
exchange = < < "e">>,
2018-08-19 21:59:30 +08:00
routing_key = < < "">>,
2018-08-19 21:35:53 +08:00
}, Msg) || _ < - lists:seq ( 1 , 100000 ) ] ,
amqp_connection:close(Conn),
ok.
2015-09-03 00:33:32 +08:00
```
2013-01-09 19:10:05 +08:00
2018-08-19 19:20:35 +08:00
### Routing on a Message Property
2015-09-03 00:33:32 +08:00
2024-07-09 22:47:02 +08:00
Instead of a value in the header property, you can route on the
2018-08-19 21:59:30 +08:00
``message_id``, ``correlation_id``, or ``timestamp`` message properties. To do so,
declare the exchange with a string argument called ``"hash-property"`` naming the
property to be used.
2024-07-09 22:47:02 +08:00
The `"hash-header"` and `"hash-property"` are mutually exclusive.
2015-09-03 00:33:32 +08:00
2024-07-24 17:42:59 +08:00
When a `"hash-property"` is specified, the chosen property should be provided.
2018-08-19 21:59:30 +08:00
If published messages do not contain the property, they will all get
routed to the same **arbitrarily chosen** queue.
2018-08-20 19:10:23 +08:00
#### Code Example in Python
2018-08-19 21:59:30 +08:00
``` python
#!/usr/bin/env python
import pika
import time
conn = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
ch = conn.channel()
args = {u'hash-property': u'message_id'}
ch.exchange_declare(exchange='e3',
exchange_type='x-consistent-hash',
arguments=args,
durable=True)
for q in ['q1', 'q2', 'q3', 'q4']:
ch.queue_declare(queue=q, durable=True)
ch.queue_purge(queue=q)
for q in ['q1', 'q2']:
ch.queue_bind(exchange='e3', queue=q, routing_key='1')
for q in ['q3', 'q4']:
ch.queue_bind(exchange='e3', queue=q, routing_key='2')
n = 100000
for rk in list(map(lambda s: str(s), range(0, n))):
ch.basic_publish(exchange='e3',
routing_key='',
body='',
properties=pika.BasicProperties(content_type='text/plain',
delivery_mode=2,
message_id=rk))
print('Done publishing.')
print('Waiting for routing to finish...')
# in order to keep this example simpler and focused,
# wait for a few seconds instead of using publisher confirms and waiting for those
time.sleep(5)
print('Done.')
conn.close()
2015-09-03 00:33:32 +08:00
```
2018-08-20 19:10:23 +08:00
#### Code Example in Java
``` java
package com.rabbitmq.examples;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
public class ConsistentHashExchangeExample3 {
public static final String EXCHANGE = "e3";
private static String EXCHANGE_TYPE = "x-consistent-hash";
public static void main(String[] argv) throws IOException, TimeoutException, InterruptedException {
ConnectionFactory cf = new ConnectionFactory();
Connection conn = cf.newConnection();
Channel ch = conn.createChannel();
for (String q : Arrays.asList("q1", "q2", "q3", "q4")) {
ch.queueDeclare(q, true, false, false, null);
ch.queuePurge(q);
}
Map< String , Object > args = new HashMap< >();
args.put("hash-property", "message_id");
ch.exchangeDeclare(EXCHANGE, EXCHANGE_TYPE, true, false, args);
for (String q : Arrays.asList("q1", "q2")) {
ch.queueBind(q, EXCHANGE, "1");
}
for (String q : Arrays.asList("q3", "q4")) {
ch.queueBind(q, EXCHANGE, "2");
}
ch.confirmSelect();
for (int i = 0; i < 100000 ; i + + ) {
AMQP.BasicProperties.Builder bldr = new AMQP.BasicProperties.Builder();
ch.basicPublish(EXCHANGE, "", bldr.messageId(String.valueOf(i)).build(), "".getBytes("UTF-8"));
}
ch.waitForConfirmsOrDie(10000);
System.out.println("Done publishing!");
System.out.println("Evaluating results...");
// wait for one stats emission interval so that queue counters
// are up-to-date in the management UI
Thread.sleep(5);
System.out.println("Done.");
conn.close();
}
}
```
#### Code Example in Ruby
2018-08-19 21:59:30 +08:00
``` ruby
#!/usr/bin/env ruby
require 'bundler'
Bundler.setup(:default, :test)
require 'bunny'
conn = Bunny.new
conn.start
ch = conn.create_channel
ch.confirm_select
q1 = ch.queue("q1", durable: true)
q2 = ch.queue("q2", durable: true)
q3 = ch.queue("q3", durable: true)
q4 = ch.queue("q4", durable: true)
[q1, q2, q3, q4].each(& :purge)
x = ch.exchange("x3", type: "x-consistent-hash", durable: true, arguments: {"hash-property" => "message_id"})
[q1, q2].each { |q| q.bind(x, routing_key: "1") }
[q3, q4].each { |q| q.bind(x, routing_key: "2") }
n = 100_000
(0..n).map(& :to_s).each do |i|
x.publish(i.to_s, routing_key: rand.to_s, message_id: i)
end
ch.wait_for_confirms
puts "Done publishing!"
2018-08-21 23:31:56 +08:00
# wait for queue stats to be emitted so that management UI numbers
# are up-to-date
2018-08-19 21:59:30 +08:00
sleep 5
conn.close
2018-08-21 23:31:56 +08:00
puts "Done"
2018-08-19 21:59:30 +08:00
```
2018-08-20 19:10:23 +08:00
#### Code Example in Erlang
2018-08-19 21:59:30 +08:00
``` erlang
-include_lib("amqp_client/include/amqp_client.hrl").
test() ->
{ok, Conn} = amqp_connection:start(#amqp_params_network{}),
{ok, Chan} = amqp_connection:open_channel(Conn),
Queues = [< < "q0">>, < < "q1">>, < < "q2">>, < < "q3">>],
amqp_channel:call(Chan,
#'exchange.declare'{
exchange = < < "e">>, type = < < "x-consistent-hash">>,
2024-02-06 01:26:25 +08:00
arguments = {< < "hash-property">>, longstr, < < "message_id">>}
2018-08-19 21:59:30 +08:00
}),
[amqp_channel:call(Chan, #'queue.declare'{queue = Q}) || Q < - Queues ] ,
[amqp_channel:call(Chan, #'queue.bind'{queue = Q,
exchange = < < "e">>,
routing_key = < < "1">>})
|| Q < - [ < < " q0 " > >, < < "q1">>]],
[amqp_channel:call(Chan, #'queue.bind' {queue = Q,
exchange = < < "e">>,
routing_key = < < "2">>})
|| Q < - [ < < " q2 " > >, < < "q3">>]],
RK = list_to_binary(integer_to_list(random:uniform(1000000)),
Msg = #amqp_msg {props = #'P_basic'{message_id = RK}, payload = << >>},
[amqp_channel:call(Chan,
#'basic.publish'{
exchange = < < "e">>,
routing_key = < < "">>,
)
}, Msg) || _ < - lists:seq ( 1 , 100000 ) ] ,
amqp_connection:close(Conn),
ok.
```
2015-09-03 00:33:32 +08:00
2018-08-19 19:09:00 +08:00
2015-06-04 19:50:52 +08:00
## Getting Help
2018-08-19 20:59:31 +08:00
If you have questions or need help, feel free to ask on the
2015-03-25 02:15:56 +08:00
[RabbitMQ mailing list ](https://groups.google.com/forum/#!forum/rabbitmq-users ).
2015-06-04 19:50:52 +08:00
2018-08-19 19:09:00 +08:00
## Implementation Details
The hash function used in this plugin as of RabbitMQ 3.7.8
2018-08-21 23:41:57 +08:00
is [A Fast, Minimal Memory, Consistent Hash Algorithm ](https://arxiv.org/abs/1406.2294 ) by Lamping and Veach. Erlang's `phash2` function is used to convert non-integer values to
an integer one that can be used by the jump consistent hash function by Lamping and Veach.
### Distribution Uniformity
2018-08-19 19:09:00 +08:00
2018-08-20 20:12:27 +08:00
A Chi-squared test was used to evaluate distribution uniformity. Below are the
results for 18 bucket counts and how they compare to two commonly used `p-value`
thresholds:
|Number of buckets|Chi-squared test result|Degrees of freedom|p-value = 0.05|p-value = 0.01|
|-|-----------|------------------|--------|--------|
|2|0.5|1|3.84|6.64|
|3|0.946|2|5.99|9.21|
|4|2.939|3|7.81|11.35|
|5|2.163|4|3.49|13.28|
|6|2.592|5|11.07|15.09|
|7|4.654|6|12.59|16.81|
|8|7.566|7|14.07|18.48|
|9|5.847|8|15.51|20.09|
|10|9.790|9|16.92|21.67|
|11|13.448|10|18.31|23.21|
|12|12.432|11|19.68|24.73|
|13|12.338|12|21.02|26.22|
|14|9.898|13|22.36|27.69|
|15|8.513|14|23.69|29.14|
|16|6.997|15|24.99|30.58|
|17|6.279|16|26.30|32.00|
|18|10.373|17|28.87|34.81|
|19|12.935|18|30.14|36.19|
|20|11.895|19|31.41|37.57|
2018-08-21 23:41:57 +08:00
### Binding Operations and Bucket Management
2018-08-20 20:12:27 +08:00
2018-08-19 19:09:00 +08:00
When a queue is bound to a consistent hash exchange, the protocol method, `queue.bind` ,
carries a weight in the routing (binding) key. The binding is given
a number of buckets on the hash ring (hash space) equal to the weight.
When a queue is unbound, the buckets added for the binding are deleted.
These two operations use linear algorithms to update the ring.
To perform routing the exchange extract the appropriate value for hashing,
hashes it and retrieves a bucket number from the ring, then the bucket and
its associated queue.
The implementation assumes there is only one binding between a consistent hash
2018-08-20 19:10:23 +08:00
exchange and a queue. Having more than one binding is unnecessary because
2018-08-19 19:09:00 +08:00
queue weight can be provided at the time of binding.
2018-08-21 23:41:57 +08:00
### Clustered Environments
2018-08-20 19:10:23 +08:00
The state of the hash space is distributed across all cluster nodes.
2015-06-04 19:50:52 +08:00
## Copyright and License
2024-02-06 01:26:25 +08:00
(c) 2007-2024 Broadcom. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
2015-06-04 19:50:52 +08:00
2020-07-12 02:45:03 +08:00
Released under the Mozilla Public License 2.0, same as RabbitMQ.
2018-08-20 19:10:23 +08:00
See [LICENSE ](./LICENSE ) for details.