Travis Smith - Blogging

MassTransit Distributor (Load Balancing)

MassTransit has a distributor built in now (actually for some time...). I'll try to explain what it does and how to use it.

What is the distributor? It provides the ability to load balancing across multiple endpoints for a given message.

Distributor Setup

For setting up the distributor, when doing the service bus configuration, call UseDisttributorFor(IEndpointFactory endpointFactory) on IServiceBusConfigurator. An instance of the endpoint factory is required.

For setting up the worker(s), when doing the service bus configuration, call ImplementDistributorWorker(Action consumeAction) on IServiceBusConfigurator.

Default IWorkerSelectionStrategy Implementation

The default worker selection strategy simple looks at the current message count in queue vs. the message count limits. It selects the worker with the smallest number of messages pending or in progress.

The data used to identify which worker is busy might be out of date. There is additional message traffic to keep this data in sync and it can fall behind when the system is under load.

Custom IWorkerSelectionStrategy

For setting up the distributor, when doing the service bus configuration, call UseDisttributorFor(IEndpointFactory endpointFactory, IWorkerSelectionStrategy workerSelectionStrategy) on IServiceBusConfigurator.

This requires a custom implementation of the IWorkerSelectionStrategy interface. There are two methods to implement.

  • HasAvaiableWorker - Using the list of candidate workers, indicates if one is available
  • SelectWorker - Using the list of candidate workers, find the best candidate available

Sample

Okay, so I know you're now asking if I'll get to any real code. There's a sample in the MT solution Grid.Distributor that shows the basics of using the Distributor.

There are two parts, the activator (who sends commands) and the workers (who completes the commands). You can spin up multiple workers to work on the commands.

blog comments powered by Disqus