...

/

Choose the Right Dispatcher

Choose the Right Dispatcher

Learn how GenStage uses different dispatchers to send events to consumers.

Introduction to dispatchers

There is one important component of GenStage we haven’t talked about yet—the dispatcher. When :producer and :producer_consumer stages send events to consumers, it is the dispatcher that sends the events. So far, we’ve used the default DemandDispatcher, but GenStage comes with two more. Let’s see what they do and how we can use them.

DemandDispatcher

We can specify the kind of dispatcher to use when we initialize the process using the :dispatcher key. The default is DemandDispatcher, which is equivalent to this configuration:

Press + to interact
def init(state) do
{:producer, state, dispatcher: GenStage.DemandDispatcher}
end

The DemandDispatcher dispatcher sends events to consumers with the highest demand first. It is the dispatcher that we’ll use most often. However, there are cases where we may want to route events to consumers using a different strategy, where BroadcastDispatcher and PartitionDispatcher come ...