Hacker News new | ask | show | jobs
by Zarathu 1884 days ago
> In particular, I was itching to learn more about handling concurrency in Elixir. This, of course, led me to GenServers.

Might be a nitpicking here, but GenServers aren't useful for concurrency. They just manage state, and only process one message at a time. If you're using this as a cache, your reads will be bottlenecked by however quickly the GenServer can handle the read requests.

2 comments

If you've got 5 gen_servers and you cast each one a message then that is concurrency. The whole point of a gen_server is that it's a separate process doing it's own thing.

I can dump 1000s of things into its message queue and then do something else. It'll keep working away.

It's like saying threads aren't useful for concurrency because they can only do one thing at a time.

I would advise to not rely on casting too much because it lacks backpressure.
> GenServers aren't useful for concurrency.

Sure, maybe not by themselves but typically you would run many GenServers concurrently in your application as part of a supervision tree. Libraries like Broadway (and the underlying GenStage) are essentially just leveraging GenServer to make it easier to orchestrate concurrency and state synchronization across multiple processes in your application. But you could build a comparable system on your own just using GenServer and a dynamic supervisor.