fireproofsocks

fireproofsocks

:queue vs genstage state

I’m expanding my understanding of genstage and how to build systems that are more resilient to overload. The GenStage docs demonstrate use of the Erlang :queue module… however, I am not clear on why that is being demonstrated. The :queue module seems to offer some convenience functions for working with lists, but it also has some inconveniences. E.g. it can’t work with a range like 1..5 – enumerables need to be converted into explicit lists, and certain operations like split fail if the queue is too small, so its use requires some “defensive coding”. Under the hood, an Erlang :queue is just a list, so I would expect the performance characteristics to be pretty much the same, and I would expect both to be equally susceptible to being over-stuffed.

I tried 2 variants of the same GenStage producer just to get a bit more familiar with :queue, and I think the 2 variants are functionally equivalent (although, the “defensive coding” required to use :queue makes me favor the simple list implementation).

Perhaps the conversation should be more about identifying the inputs to your system that could be “3rd rails” and overload it… A simple list (or a :queue) is acceptable for buffering demand or cases where the data stored in-memory is expected to remain tangibly small, but not when the input might be huge and unbounded. Anyhow, using :queue seems to be a distraction.

Thoughts?

Most Liked

LostKobrakai

LostKobrakai

That’s not the case. Under the hood :queue uses a tuple with two separate lists, which has rather different performance characteristics for the usecase of a fifo queue (hence why :queue exists). With just a plain list either your queuing or dequeuing would be expensive due to how linked lists work. Queue shifts that by making both generally inexpensive at the expense of needing to rebalance head and tail ever so often.

But yes sheer size will eventually become a problem no matter how you store items.

LostKobrakai

LostKobrakai

More code for a more appropriate and well working data structure though. You’re comparing apples to oranges here.

LostKobrakai

LostKobrakai

It’s not as black and white. Yes simpler examples are generally a good thing. But using a list in the examples in question is not just making the examples simpler, but also a surefire way for people shooting themselves in the foot, when they copy from the example. Using a list to model a fifo queue is plain not what you want to do if this is meant to hold more than a handful of items.

Last Post!

stevensonmt

stevensonmt

FTR, I wasn’t trying to argue FOR my example over using :queue just stating why from a naive perspective it does seem puzzling.

Where Next?

Popular in Discussions Top

AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement