Ensure stable routing for x-modulus-hash exchange (backport #15849)#15859
Merged
Ensure stable routing for x-modulus-hash exchange (backport #15849)#15859
x-modulus-hash exchange (backport #15849)#15859Conversation
## What? This commit makes routing stable for the `x-modulus-hash` exchange: If the same destination queues stay bound to the exchange (i.e. do not bind or unbind queues after the "initial setup"), messages with the same domain entity (routing key) will always end up in the same destination queue, even across node restarts. ## How? With Mnesia this was guaranteed due to order_set tables. Khepri introduced a regression since it uses a bag ETS projection table. This commit simply sorts the destinations before picking the Nth destination. ## Why? Uses cases where message order matters are common. This commit allows to for example bind N quorum queues to an `x-modulus-hash` exchange instance (the binding key doesn't matter) and use the Single Active Consumer (SAC) feature on each quorum queue. This will provide * message ordering thanks to stable routing and SAC * concurrent consumption: N app instances can process messages in parallel * fault tolerance since the broker will deliver messages to another consumer when the active consumer crashes Using the consistent hash exchange is an alternative, but unnecessarily complex for this use case. Yet another alternative for this use case is using the murmur3 exchange type (#8319). (cherry picked from commit e7dfae5)
Move the x-modulus-hash exchange type from rabbitmq_sharding to rabbit since this exchange type is useful even without the sharding plugin. (cherry picked from commit 1229705)
This commit adds a test case to explicitly verify that the `x-modulus-hash` exchange supports weighted routing. Because the exchange uses `lists:sort/1` instead of `lists:usort/1` when fetching the bound destinations, a user can bind the exact same queue multiple times to the exchange (using different dummy binding keys) to increase its weight. If a queue is bound N times, it will appear N times in the destination list, giving it a proportionally higher chance of receiving messages. (cherry picked from commit d7b3d19)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
This PR makes routing stable for the
x-modulus-hashexchange: If the same destination queues stay bound to the exchange (i.e. do not bind or unbind queues after the "initial setup"), messages with the same domain entity (routing key) will always end up in the same destination queue, even across node restarts.Move the x-modulus-hash exchange type from
rabbitmq_shardingtorabbitsince this exchange type is useful even without the sharding plugin.How?
With Mnesia this was guaranteed due to order_set tables. Khepri introduced a regression since it uses a bag ETS projection table.
This PR simply sorts the destinations before picking the Nth destination.
Why?
Uses cases where message order matters are common. This PR allows to for example bind N quorum queues to an
x-modulus-hashexchange instance (the binding keys do not matter) and use the Single Active Consumer (SAC) feature on each quorum queue. This will provideUsing the consistent hash exchange is an alternative, but unnecessarily complex for this use case.
Yet another alternative for this use case is using the murmur3 exchange type (#8319).
Docs:
rabbitmq/rabbitmq-website#2494
This is an automatic backport of pull request #15849 done by [Mergify](https://mergify.com).