Skip to content

Conversation

@mergify
Copy link

@mergify mergify bot commented Sep 17, 2025

Resolves #14531
Resolves #14533

What?

Increase end-to-end message throughput for messages routed via the fanout exchange by ~42% (see benchmark below).
In addition to the fanout exchange, a similar speed up is achieved for the following exchange types:

  • modulus hash
  • random
  • recent history

This applies only if Khepri is enabled.

How?

Use an additional routing table (projection) rabbit_khepri_route_by_source whose table key is the source exchange.
Looking up the destinations happens then by an ETS table key.

Prior to this commit, CPUs were busy compiling the same match spec for every incoming message.

For the bug fix of #14533, we also introduce a new projection called rabbit_khepri_route_by_source_key. This can be thought of a v2 of the old rabbit_khepri_index_route projection. The old rabbit_khepri_index_route projection gets deleted in the rabbitmq_4.2.0 feature flag callback. The two new projections are registered at boot time (as discussed in #14543 (comment))

Benchmark

  1. Start RabbitMQ:
make run-broker RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS="+S 5" \
    RABBITMQ_CONFIG_FILE="advanced.config" PLUGINS="rabbitmq_management"

where advanced.config contains:

[
 {rabbitmq_management_agent, [
  {disable_metrics_collector, true}
 ]}
].
  1. Create a queue and binding:
deps/rabbitmq_management/bin/rabbitmqadmin declare queue queue_type=classic durable=true name=q1 && \
deps/rabbitmq_management/bin/rabbitmqadmin declare binding source=amq.fanout destination=q1
  1. Create the load
java -jar target/perf-test.jar -p -e amq.fanout -u q1 -s 5 --autoack -z 60

Before this commit:

sending rate avg: 97394 msg/s
receiving rate avg: 97394 msg/s

After this commit:

sending rate avg: 138677 msg/s
receiving rate avg: 138677 msg/s

The CPU flamegraph shows that rabbit_exchange:route/3 consumes the following CPU amounts:

  • 13.5% before this commit
  • 3.4% after this commit

Downsides

Additional ETS memory usage for the new projection table.
However, the new table does not store any binding entries for the following
source exchange types:

  • direct
  • headers
  • topic
  • x-local-random
  • x-jms-topic

Also, care must be taken to not add extensive amounts of bindings with the same source exchange to projection rabbit_khepri_route_by_source. For fanout exchanges, binding an extensive amount of queues (e.g. thousands) to the same fanout exchanges is nonsensical anyway. One edge case could be binding thousands of queues to the random exchange type. But even such an exotic use case could be realised efficiently by using intermediate random exchanges (i.e. exchange-to-exchange bindings) 🙂


This is an automatic backport of pull request #14546 done by Mergify.

* Add test case for binding args Khepri regression

This commit adds a test case for a regression/bug that occurs in Khepri.
```
make -C deps/rabbit ct-bindings t=cluster:binding_args RABBITMQ_METADATA_STORE=mnesia
```
succeeds, but
```
make -C deps/rabbit ct-bindings t=cluster:binding_args RABBITMQ_METADATA_STORE=khepri
```
fails.

The problem is that ETS table `rabbit_khepri_index_route` cannot
differentiate between two bindings with different binding arguments, and
therefore deletes entries too early, leading to wrong routing decisions.

The solution to this bug is to include the binding arguments in the
`rabbit_khepri_index_route` projection, similar to how the binding args
are also included in the `rabbit_index_route` Mnesia table.

This bug/regression is an edge case and exists if the source exchange
type is `direct` or `fanout` and if different bindings arguments are
used by client apps. Note that such binding arguments are entirely
ignored when RabbitMQ performs routing decisions for the `direct` or
`fanout` exchange. However, there might be client apps that use binding
arguments to add some metadata to the binding, for example `app-id` or
`user` or `purpose` and might use this metadata as a form of reference
counting in deciding when to delete `auto-delete` exchanges or just for
informational/operational purposes.

* Fix regression with Khepri binding args

Fix #14533

* Speed up fanout exchange

Resolves #14531

 ## What?
Increase end-to-end message throughput for messages routed via the fanout exchange by ~42% (see benchmark below).
In addition to the fanout exchange, a similar speed up is achieved for the following exchange types:
* modulus hash
* random
* recent history

This applies only if Khepri is enabled.

 ## How?
Use an additional routing table (projection) whose table key is the source exchange.
Looking up the destinations happens then by an ETS table key.

Prior to this commit, CPUs were busy compiling the same match spec for every incoming message.

 ## Benchmark
1. Start RabbitMQ:
```
make run-broker RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS="+S 5" \
    RABBITMQ_CONFIG_FILE="advanced.config" PLUGINS="rabbitmq_management"
```
where `advanced.config` contains:
```
[
 {rabbitmq_management_agent, [
  {disable_metrics_collector, true}
 ]}
].
```

2. Create a queue and binding:
```
deps/rabbitmq_management/bin/rabbitmqadmin declare queue queue_type=classic durable=true name=q1 && \
deps/rabbitmq_management/bin/rabbitmqadmin declare binding source=amq.fanout destination=q1
```

3. Create the load
```
java -jar target/perf-test.jar -p -e amq.fanout -u q1 -s 5 --autoack -z 60
```

Before this commit:
```
sending rate avg: 97394 msg/s
receiving rate avg: 97394 msg/s
```

After this commit:
```
sending rate avg: 138677 msg/s
receiving rate avg: 138677 msg/s
```

The CPU flamegraph shows that `rabbit_exchange:route/3` consumes the following CPU amounts:
* 13.5% before this commit
* 3.4% after this commit

 ## Downsides
Additional ETS memory usage for the new projection table.
However, the new table does not store any binding entries for the following
source exchange types:
* direct
* headers
* topic
* x-local-random

* Add exchange binding tests

Test that exchange bindings work correctly with the new projection
tables `rabbit_khepri_route_by_source` and
`rabbit_khepri_route_by_source_key`.

* Always register all projections

Khepri won’t modify a projection that is already registered (based on its name).

* Protect ets:lookup_element/4 in try catch

See #11667 (comment)
for rationale.

(cherry picked from commit a66c716)
@mergify mergify bot assigned ansd Sep 17, 2025
@ansd ansd added this to the 4.2.0 milestone Sep 17, 2025
@ansd ansd merged commit 9835302 into v4.2.x Sep 17, 2025
287 checks passed
@ansd ansd deleted the mergify/bp/v4.2.x/pr-14546 branch September 17, 2025 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants