4 ms·
> 99.9% of the time you really shouldn't be writing genservers in elixir Woah! Why is that? I've written at least one GenServer in nearly all the Elixir proje
by AlchemistCamp 3y ago
> 99.9% of the time you really shouldn't be writing genservers in elixir
Woah! Why is that?
I've written at least one GenServer in nearly all the Elixir projects I've worked on. They seem like one of the base building-blocks to me. Also, if you squint a bit, you'll see that many libraries you're working with are essentially exposing a GenServer interface, with a few extra features.
- sodapopcan 3y ago> Also, if you squint a bit, you'll see that many libraries you're working with are essentially exposing a GenServer interface, with a few extra features. Like LiveViews :)
- pests 3y agoThe only thing I can think of is when developers overuse genserver not realizing it's involving another process and then serializing your requests (behind a queue even) into its mailbox. That may be desirable for some but in-process handling can sometimes be the better choice.
- throwawaymaths 3y agoI've never written a genserver in any professional project I've worked on.... Well, except maybe for a trivial 5 line wrapper enables a global ets. > Also, if you squint a bit, you'll see that many libraries you're working with are essentially exposing a GenServer interface, with a few extra features. I never said you shouldn't be using GenServers. I said you shouldn't write GenServers.
- sodapopcan 3y agoThere's no reason not to write GenServers if you need them. Just don't use them as a class replacement :) But if you need to model some simple runtime state or concurrency, that's what they're there for.
- throwawaymaths 3y agouse Tasks for simple concurrency, Jose has built an awesome module for us.
- sodapopcan 3y agoOh I agree! I have a couple of gen servers acting as very simple message queues—the kind that don't matter if they are nuked during a deploy.
- deleted 3y ago[deleted]
- jolux 3y ago> I've never written a genserver in any professional project I've worked on Wow, that's quite surprising! We use GenServers quite extensively at work. The most interesting usage is probably this application, which uses GenServers to model the state of real-world hardware (in this case, transit stop countdown clocks): https://github.com/mbta/realtime_signs/blob/main/lib/signs/supervisor.ex https://github.com/mbta/realtime_signs/blob/main/lib/signs/s... Curious what you use instead of GenServers. Plain processes? Agents? Something else? GenServers are probably overused but I still reach for them when I need a concurrent bundle of state with a specific interface and behavior. I think we're generally trying to write more GenStages than GenServers at the MBTA these days, but that's mainly because we're moving towards being event-driven. GenServers still have their place.
- throwawaymaths 3y agoI use other people's GenServers. Never agents. Tasks for concurrency (but almost never those either, just Oban). Ets for stateful datastore. Pedantically I guess gen_statem isn't a genServer, but I'm generally referring to the pattern, which gen_statem is. Mostly webdev, so, that stuff is all taken care of for me. MBTA is modeling reactive systems that exist in the real world, so yeah, that's a place where i would be surprised if you didn't use genservers, though I suppose you could just use a database-backed state machine. Surprised you use GenServers instead of gen_statem (though you might have been, as I was, less than pedantic)
- AlchemistCamp 3y agoAre you actively trying not to use GenServers in your application code or is it just that you have a set of 3rd party dependencies that remove the need?
- throwawaymaths 3y agoYes
- jolux 3y agoAh I see. Yeah, Phoenix definitely gives you a lot out of the box. I think for webdev it's probably enough. This is a pretty straightforward Phoenix app for example, only uses a couple GenServers: https://github.com/mbta/arrow https://github.com/mbta/arrow The question of when to introduce databases is a really interesting one and also one I'm still coming to terms with a few years into writing Elixir full-time. Many of our apps don't use them at all, but I think at least a few could benefit from more structure and durability in their datastores.