MassTransit - Competing Consumers

Download full source code.

This is a follow-up to a post I wrote last month which showed how to use MassTransit with a single producer and a single consumer. In this post, I’ll show how to use MassTransit with a single producer and a set of consumers that compete for available messages.

The code is the same as in that post. All you have to do is run multiple instances of the consumer.

Start MassTransit as shown in the previous post.

Download the zip. Open both the Producer and Consumer apps and compile them.

In a terminal, build, then run the producer app -

bin/Debug/net8.0/Producer

In another terminal, build, then run the consumer app -

bin/Debug/net8.0/Consumer

And now start the competing consumer in another terminal -

bin/Debug/net8.0/Consumer

You will see that the messages are evenly distributed between the two consumers in a round-robin fashion.

If you added a third consumer, you would see the messages distributed between all three consumers.

Producer on the left, consumers in the middle and right
Producer on the left, consumers in the middle and right

Download full source code.

comments powered by Disqus

Related