Can you explain these 2 lines of the GenStage example from Discord's blog?

Hi,

I shared this link (https://discord.engineering/how-discord-handles-push-request-bursts-of-over-a-million-per-minute-with-elixirs-genstage-8f899f0221b4#.bvh5935u6) with a colleague and I was going through it step by step with him until we reached line 21 and line 26 of the first code sample.
I really don’t understand why we return these values.

Can anyone explain?

2 Likes

In most cases, GenStage producers work by sending events only when there is demand. However, they took a different approach, they decided to try to send event downstream whenever the producer gets data. This means that, if the consumers are fast, it will work just fine, otherwise the producer will buffer the data until consumers ask for it. That’s from where the buffer they are talking about comes from. It is the buffer in the producer because it is producing faster than consumers are consuming them.

The GenStage docs have a whole section on buffering where we discuss similar examples and more.

4 Likes

Thanks for the explanation. I did realise after posting that the docs had a whole section about buffering events and demand.

  • Correct me if I’m wrong but the producer can buffer if I take care of this myself by creating a queue in the state right?

  • If I don’t, it is the underlying dispatcher that will queue the events until there is a demand from a consumer?

  • when we “dispatch” events from a producer, we are not really sending/pushing them to the next stage(s) right? This would go against the back pressure model. Again, it seems that “dispatching” means passing events to the underlying dispatcher so they can be consumed, right?

  • does every stage have its own dispatcher? Is it a separate process?

Thanks Jose, it always feels great to receive an answer from the creator of the language himself!

Yes.

The producer itself will buffer the events. The dispatcher is not a separate entity, it is just a set of callbacks that the producer calls. The dispatcher knows how many items should be sent and how.

Dispatching is actually sending messages to a later stage.

Only producers and it is a set of functions the producer calls, not a separate process.

1 Like