Understand Consumer Demand
Learn how the producer and consumer interact when they supply and process processes.
Consumer demand
We previously saw that PageProducer
was asked for 1,000 events. We may wonder where the number comes from. By default, stages of the :consumer
and :producer_consumer
type ensure that the demand for new events is between 500 and 1,000. We can configure this through the min_demand
and max_demand
settings. When a consumer subscribes to a producer in the init/1
function, we pass a tuple with some configuration options:
#`subscribe_to` options can be a list of tuples.
sub_opts = [{PageProducer, min_demand: 500, max_demand: 1000}]
{:consumer, initial_state, subscribe_to: sub_opts}
By configuring demand, we can control two things: max_demand
and min_demand
. The first sets the maximum number of events that a consumer can request. The second determines the lowest number of events available to the consumer before it can request more.
The consumer is greedy and will immediately demand 1,000 events on start, according to the max_demand
value we set. Let’s say the producer supplies 1,000 events. The consumer processes the first batch using a simple formula:
...