• This repository has been archived on 17/Nov/2020
  • Stars
    star
    303
  • Rank 137,629 (Top 3 %)
  • Language
    Erlang
  • License
    Other
  • Created over 10 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sharded logical queues for RabbitMQ: a queue type which provides improved parallelism and thoughput at the cost of total ordering

RabbitMQ Sharding Plugin

This was migrated to https://github.com/rabbitmq/rabbitmq-server

This repository has been moved to the main unified RabbitMQ "monorepo", including all open issues. You can find the source under /deps/rabbitmq_sharding. All issues have been transferred.

Overview

This plugin introduces the concept of sharded queues for RabbitMQ. Sharding is performed by exchanges, that is, messages will be partitioned across "shard" queues by one exchange that we should define as sharded. The machinery used behind the scenes implies defining an exchange that will partition, or shard messages across queues. The partitioning will be done automatically for you, i.e: once you define an exchange as sharded, then the supporting queues will be automatically created on every cluster node and messages will be sharded across them.

Project Maturity

This plugin is reasonably mature and known to have production users.

Overview

The following graphic depicts how the plugin works from the standpoint of a publisher and a consumer:

Sharding Overview

On the picture above the producers publishes a series of messages, those messages get partitioned to different queues, and then our consumer get messages from one of those queues. Therefore if there is a partition with 3 queues, it is assumed that there are at least 3 consumers to get all the messages from those queues.

Queues in RabbitMQ are units of concurrency (and, if there are enough cores available, parallelism). This plugin makes it possible to have a single logical queue that is partitioned into multiple regular queues ("shards"). This trades off total ordering on the logical queue for gains in parallelism.

Message distribution between shards (partitioning) is achieved with a custom exchange type that distributes messages by applying a hashing function to the routing key.

Messages Distribution Between Shards (Partitioning)

The exchanges that ship by default with RabbitMQ work in an "all or nothing" fashion, i.e: if a routing key matches a set of queues bound to the exchange, then RabbitMQ will route the message to all the queues in that set. For this plugin to work it is necessary to route messages to an exchange that would partition messages, so they are routed to at most one queue (a subset).

The plugin provides a new exchange type, "x-modulus-hash", that will use a hashing function to partition messages routed to a logical queue across a number of regular queues (shards).

The "x-modulus-hash" exchange will hash the routing key used to publish the message and then it will apply a Hash mod N to pick the queue where to route the message, where N is the number of queues bound to the exchange. This exchange will completely ignore the binding key used to bind the queue to the exchange.

There are other exchanges with similar behaviour: the Consistent Hash Exchange or the Random Exchange. Those were designed with regular queues in mind, not this plugin, so "x-modulus-hash" is highly recommended.

If message partitioning is the only feature necessary and the automatic scaling of the number of shards (covered below) is not needed or desired, consider using Consistent Hash Exchange instead of this plugin.

Auto-scaling

One of the main properties of this plugin is that when a new node is added to the RabbitMQ cluster, then the plugin will automatically create more shards on the new node. Say there is a shard with 4 queues on node a and node b just joined the cluster. The plugin will automatically create 4 queues on node b and "join" them to the shard partition. Already delivered messages will not be rebalanced but newly arriving messages will be partitioned to the new queues.

Consuming From a Sharded [Pseudo-]Queue

While the plugin creates a bunch of "shard" queues behind the scenes, the idea is that those queues act like a big logical queue where you consume messages from it. Total ordering of messages between shards is not defined.

An example should illustrate this better: let's say you declared the exchange images to be a sharded exchange. Then RabbitMQ creates several "shard" queues behind the scenes:

  • shard: - nodename images 1
  • shard: - nodename images 2
  • shard: - nodename images 3
  • shard: - nodename images 4.

To consume from a sharded queue, register a consumer on the "images" pseudo-queue using the basic.consume method. RabbitMQ will attach the consumer to a shard behind the scenes. Note that consumers must not declare a queue with the same name as the sharded pseudo-queue prior to consuming.

TL;DR: if you have a shard called images, then you can directly consume from a queue called images.

How does it work? The plugin will chose the queue from the shard with the least amount of consumers, provided the queue contents are local to the broker you are connected to.

NOTE: there's a small race condition between RabbitMQ updating the queue's internal stats about consumers and when clients issue basic.consume commands. The problem with this is that if your client issue many basic.consume commands without too much time in between, it might happen that the plugin assigns the consumers to queues in an uneven way.

Load Distribution and Consumer Balancing

As of RabbitMQ 3.8.1, the plugin is no longer affected by the queue master locator policy when using mirrored queues. Please read below if you use a previous version.

This plugin can be affected by queue master locator policy used in the cluster as well as client connection load balancing strategy.

"Minimum masters" is a queue master locator that is most in line with the goals of this plugin.

For load balancers, the "least connections" strategy is more likely to produce an even distribution compared to round robin and other strategies.

How Evenly Will Messages Be Distributed?

As with many data distribution approaches based on a hashing function, even distribution between shards depends on the distribution (variability) of inputs, that is, routing keys. In other words the larger the set of routing keys is, the more even will message distribution between shareds be. If all messages had the same routing key, they would all end up on the same shard.

Installing

RabbitMQ 3.6.0 or later

As of RabbitMQ 3.6.0 this plugin is included into the RabbitMQ distribution.

Like any other RabbitMQ plugin it has to be enabled before it can be used:

rabbitmq-plugins enable rabbitmq_sharding

You'd probably want to also enable the Consistent Hash Exchange plugin, too.

With Earlier Versions

Install the corresponding .ez files from our Community Plugins archive.

Then run the following command:

rabbitmq-plugins enable rabbitmq_sharding

You'd probably want to also enable the Consistent Hash Exchange plugin, too.

Usage

Once the plugin is installed you can define an exchange as sharded by setting up a policy that matches the exchange name. For example if we have the exchange called shard.images, we could define the following policy to shard it:

$CTL set_policy images-shard "^shard.images$" '{"shards-per-node": 2, "routing-key": "1234"}'

This will create 2 sharded queues per node in the cluster, and will bind those queues using the "1234" routing key.

About the routing-key policy definition

In the example above we use the routing key 1234 when defining the policy. This means that the underlying exchanges used for sharding will bind the sharded queues to the exchange using the 1234 routing key specified above. This means that for a direct exchange, _only messages that are published with the routing key 1234 will be routed to the sharded queues. If you decide to use a fanout exchange for sharding, then the 1234 routing key, while used during binding, will be ignored by the exchange. If you use the "x-modulus-hash" exchange, then the routing key will be ignored as well. So depending on the exchange you use, will be the effect the routing-key policy definition has while routing messages.

The routing-key policy definition is optional.

Building from Source

Get the RabbitMQ Public Umbrella ready as explained in the RabbitMQ Plugin Development Guide.

Move to the umbrella folder an then run the following commands, to fetch dependencies:

make up
cd deps/rabbitmq-sharding
make dist

LICENSE

See the LICENSE file.

Extra information

Some information about how the plugin affects message ordering and some other details can be found in the file README.extra.md

More Repositories

1

rabbitmq-server

Open source RabbitMQ: core server and tier 1 (built-in) plugins
Starlark
11,952
star
2

rabbitmq-tutorials

Tutorials for using RabbitMQ in various ways
Java
6,393
star
3

rabbitmq-dotnet-client

RabbitMQ .NET client for .NET Standard 2.0+ and .NET 4.6.2+
C#
2,056
star
4

rabbitmq-delayed-message-exchange

Delayed Messaging for RabbitMQ
Erlang
1,992
star
5

internals

High level architecture overview
1,444
star
6

amqp091-go

An AMQP 0-9-1 Go client maintained by the RabbitMQ team. Originally by @streadway: `streadway/amqp`
Go
1,319
star
7

rabbitmq-java-client

RabbitMQ Java client
Java
1,227
star
8

cluster-operator

RabbitMQ Cluster Kubernetes Operator
Go
865
star
9

ra

A Raft implementation for Erlang and Elixir that strives to be efficient and make it easier to use multiple Raft clusters in a single system.
Erlang
800
star
10

rabbitmq-website

RabbitMQ website
JavaScript
769
star
11

erlang-rpm

Latest Erlang/OTP releases packaged as a zero dependency RPM, just enough for running RabbitMQ
Shell
540
star
12

rabbitmq-management

RabbitMQ Management UI and HTTP API
Erlang
369
star
13

tls-gen

Generates self-signed x509/TLS/SSL certificates useful for development
Python
362
star
14

rabbitmq-perf-test

A load testing tool
Java
355
star
15

khepri

Khepri is a tree-like replicated on-disk database library for Erlang and Elixir.
Erlang
317
star
16

erlando

Erlando
Erlang
306
star
17

rabbitmq-peer-discovery-k8s

Kubernetes-based peer discovery mechanism for RabbitMQ
Erlang
296
star
18

rabbitmq-objc-client

RabbitMQ client for Objective-C and Swift
Objective-C
241
star
19

chef-cookbook

Development repository for Chef cookbook RabbitMQ
Ruby
211
star
20

rmq-0mq

ZeroMQ support in RabbitMQ
Erlang
210
star
21

rabbitmq-consistent-hash-exchange

RabbitMQ Consistent Hash Exchange Type
Erlang
209
star
22

rabbitmq-auth-backend-http

HTTP-based authorisation and authentication for RabbitMQ
Makefile
199
star
23

rabbitmq-erlang-client

Erlang client for RabbitMQ
Erlang
185
star
24

rabbitmq-mqtt

RabbitMQ MQTT plugin
Erlang
173
star
25

rabbitmq-stream-go-client

A client library for RabbitMQ streams
Go
167
star
26

rabbitmq-stream-rust-client

A client library for RabbitMQ streams
Rust
148
star
27

rabbitmq-prometheus

A minimalistic Prometheus exporter of core RabbitMQ metrics
Erlang
145
star
28

looking_glass

An Erlang/Elixir/BEAM profiler tool
Erlang
139
star
29

hop

RabbitMQ HTTP API client for Java, Groovy, and other JVM languages
Java
137
star
30

messaging-topology-operator

RabbitMQ messaging topology operator
Go
123
star
31

rabbitmq-cli

Command line tools for RabbitMQ
Elixir
105
star
32

rabbitmq-stream-dotnet-client

RabbitMQ client for the stream protocol
C#
100
star
33

rabbitmq-web-stomp-examples

Makefile
94
star
34

rabbitmq-amqp1.0

AMQP 1.0 support for RabbitMQ
Erlang
93
star
35

rabbitmq-web-stomp

Provides support for STOMP over WebSockets
Erlang
89
star
36

rabbitmq-recent-history-exchange

RabbitMQ Recent History Exchange
Makefile
82
star
37

diy-kubernetes-examples

Examples that demonstrate how deploy a RabbitMQ cluster to Kubernetes, the DIY way
Makefile
82
star
38

rabbitmq-event-exchange

Expose broker events as messages
Erlang
78
star
39

rabbitmq-clusterer

This project is ABANDONWARE. Use https://www.rabbitmq.com/cluster-formation.html instead.
Erlang
72
star
40

rabbitmq-message-timestamp

A RabbitMQ plugin that adds a timestamp to all incoming messages
Makefile
72
star
41

rabbitmq-common

Common library used by rabbitmq-server and rabbitmq-erlang-client
Erlang
66
star
42

rabbitmq-c

The official rabbitmq-c sources have moved to:
C
65
star
43

tgir

Official repository for Thank Goodness It's RabbitMQ (TGIR)!
Makefile
65
star
44

rabbitmq-jms-client

RabbitMQ JMS client
Java
61
star
45

rabbit-socks

Websocket and Socket.IO support for RabbitMQ (deprecated -- see https://github.com/sockjs/sockjs-erlang instead)
Erlang
58
star
46

rabbitmq-web-mqtt

Provides support for MQTT over WebSockets
Erlang
55
star
47

rabbitmq-stream-java-client

RabbitMQ Stream Java Client
Java
55
star
48

rabbitmq-top

Adds top-like information on the Erlang VM to the management plugin.
Makefile
55
star
49

rabbitmq-shovel

RabbitMQ Shovel plugin
Erlang
53
star
50

rabbitmq-auth-mechanism-ssl

RabbitMQ TLS (x509 certificate) authentication mechanism
Makefile
52
star
51

aten

An adaptive accrual node failure detection library for Elixir and Erlang
Erlang
50
star
52

rabbitmq-stomp

RabbitMQ STOMP plugin
Erlang
49
star
53

rabbitmq-tracing

RabbitMQ Tracing
Erlang
48
star
54

mnevis

Raft-based, consensus oriented implementation of Mnesia transactions
Erlang
48
star
55

gen-batch-server

A generic batching server for Erlang and Elixir
Erlang
47
star
56

rabbitmq-priority-queue

Priority Queues
46
star
57

osiris

Log based streaming subsystem for RabbitMQ
Erlang
45
star
58

rabbitmq-management-visualiser

RabbitMQ Topology Visualiser
JavaScript
41
star
59

rabbitmq-oauth2-tutorial

Explore integration of RabbitMQ with Oauth 2.0 auth backend plugin
Shell
41
star
60

rabbitmq-peer-discovery-consul

Consul-based peer discovery backend for RabbitMQ 3.7.0+
Erlang
40
star
61

rabbitmq-federation

RabbitMQ Federation plugin
Erlang
39
star
62

rabbitmq-auth-backend-oauth2

RabbitMQ authorization backend that uses OAuth 2.0 (JWT) tokens
Erlang
38
star
63

rabbitmq-codegen

RabbitMQ protocol code-generation and machine-readable spec
Python
37
star
64

support-tools

A staging area for various support and troubleshooting tools that are not (or not yet) included into RabbitMQ distribution
Shell
36
star
65

ra-kv-store

Raft-based key-value store
Clojure
33
star
66

rabbitmq-perf-html

Web page to view performance results
JavaScript
33
star
67

rabbitmq-web-mqtt-examples

Examples for the Web MQTT plugin
JavaScript
32
star
68

rabbitmq-public-umbrella

Work with ease on multiple RabbitMQ sub-projects, e.g. core broker, plugins and some client libraries
Makefile
32
star
69

rules_erlang

Bazel rules for building Erlang applications and libraries
Starlark
32
star
70

rabbitmq-smtp

RabbitMQ SMTP gateway
Erlang
31
star
71

rabbitmq-metronome

RabbitMQ example plugin
Makefile
27
star
72

rabbitmq-rtopic-exchange

RabbitMQ Reverse Topic Exchange
Erlang
27
star
73

horus

Erlang library to create standalone modules from anonymous functions
Erlang
25
star
74

workloads

Continuous validation of RabbitMQ workloads
JavaScript
24
star
75

rabbitmq-tracer

AMQP 0-9-1 protocol analyzer
Java
24
star
76

rabbitmq-peer-discovery-aws

AWS-based peer discovery backend for RabbitMQ 3.7.0+
Erlang
24
star
77

rabbitmq-shovel-management

RabbitMQ Shovel Management
Makefile
23
star
78

rabbitmq-auth-backend-ldap

RabbitMQ LDAP authentication
Erlang
22
star
79

rabbitmq-management-themes

Makefile
22
star
80

rabbitmq-auth-backend-amqp

Authentication over AMQP RPC
Erlang
20
star
81

rabbitmq-amqp1.0-client

Erlang AMQP 1.0 client
Erlang
20
star
82

erlang-data-structures

Erlang Data Structures
Erlang
20
star
83

chocolatey-package

RabbitMQ chocolatey package
PowerShell
19
star
84

lz4-erlang

LZ4 compression library for Erlang.
C
19
star
85

rabbitmq-service-nodejs-sample

A simple node.js sample app for the RabbitMQ service/add-on
JavaScript
18
star
86

rabbitmq-auth-backend-oauth2-spike

See rabbitmq/rabbitmq-auth-backend-oauth2 instead.
Erlang
17
star
87

rabbitmq-msg-store-index-eleveldb

LevelDB-based message store index for RabbitMQ
Erlang
17
star
88

rabbitmq-management-agent

RabbitMQ Management Agent
Erlang
17
star
89

rabbitmq-auth-backend-cache

Authorisation result caching plugin (backend) for RabbitMQ
Erlang
17
star
90

rabbitmq-jsonrpc-channel

RabbitMQ JSON-RPC Channels
JavaScript
15
star
91

rabbitmq-ha

Highly available queues for RabbitMQ
Erlang
15
star
92

rabbitmq-jsonrpc

RabbitMQ JSON-RPC Integration
Makefile
15
star
93

stdout_formatter

Erlang library to format paragraphs, lists and tables as plain text
Erlang
15
star
94

rabbitmq-peer-discovery-etcd

etcd-based peer discovery backend for RabbitMQ 3.7.0+
Erlang
15
star
95

rabbitmq-federation-management

RabbitMQ Federation Management
Makefile
14
star
96

credentials-obfuscation

Tiny library/OTP app for credential obfuscation
Erlang
14
star
97

rabbitmq-server-release

RabbitMQ packaging and release engineering bits that do not belong to the Concourse pipelines.
Shell
13
star
98

seshat

Erlang
13
star
99

rabbitmq-jms-topic-exchange

Custom exchange that implements JMS topic selection for RabbitMQ
Erlang
13
star
100

rabbitmq-store-exporter

RabbitMQ Store Exporter
Erlang
12
star