Search code examples
elixirgen-servergenstage

Genstage and polling are a recommended pattern?


I'm developing my first medium/big application in elixir and I'm trying to get familiar with the common patterns in elixir system design and architectures.

At this moment I'm trying to implement an input queue (currently implemented using the erlang :queue), exposed through and API and I want to have consumers from that queue performing some processing and storing the data.

I have created the queue using an Agent and I have implemented a genStage that queries the queue for elements and a genstage consumer for performing all the processing that I need. In summary something like this

Queue Agent -------> GenStage producer ---------> GenStage consumer -----> database

My questions are the following:

  1. Is this pattern valid for Elixir?
  2. Is a valid pattern the polling that the GenStage producer is performing on the Queue, asking everytime that consumers requests for new elements?
  3. Are there any performance implication in this pattern, specially are there any possible blocking implications in the Queue Agent?

Solution

  • After some comments and reading documentation I realised that GenStage is based on GenServer and I have the possibility to manage the internal state from outside the GenStage.

    After this I refactored the code and now I have the :queue as part of the GenStage state, so I don't need to polling any queue outside the GenStage and I also have the possibility to add elements into the queue using the handle_cast method.

    Now I have the Queue working as a GenStage producer and I have been able to connect my database service as a consumer