CheersThankYou

CheersThankYou

The need of GenServers in Phoenix

Hello,

I hope I am asking the question the right way here.

I’m curious as to the benefits of introducing additional GenServer usage for internal state management within an API build around phoenix framework. Does it offer any additional benefits over validating state using database constraints?

I see concurrency and fault tollerance mentioned with GenServers too, but doesn’t Phoenix already take care of this?

An example scenario:

I am a student.
I want to be able to create a study groups.
I want to make sure that a student can only be in one study group per subject they study
I want to be able to invite other students to the group I have created

All this to me, can be locked down at the database layer and the only thing I see the GenServer introducing is a bottleneck that all my group related actions have to go through.

And if I wanted to scale the app, it behaves the same. The database still manages concurrency and validation.

Is there something I am missing with this here?

edit: ah hell, something just dawned on me. If I want to allow multiple people to add people to the group in the future I am going to have issues aren’t I?

Most Liked

LostKobrakai

LostKobrakai

You’re not missing something. If you have a db it’s totally fine to protect invariants in the db. But there are cases where you don’t want to use the db (ephemeral data) or the db doesn’t scale well enough, so you e.g. shard access in the app layer. Those are the ones where additional processes can be quite useful. E.g. a few days ago I use a GenServer to act as a room for people to join via phoenix channels, which does only disappear after a certain timeout of nobody being connected. Sure I could’ve modeled this by putting information in the db and on channel connects see if the room is in the db and some “cron” like job to cleanup the db records after timeout, but it was way simpler to model as processes.

al2o3cr

al2o3cr

Building on what @LostKobrakai said - the best fit I’ve found so far for GenServers is for actions that need to happen spread out over time. (FWIW, the apps I work on haven’t reached the kind of scale that makes Postgres blink so I can’t speak to scaling advantages)

For instance, we use GenServers at work to track orders that are awaiting pickup by a courier and alert our support team if the pickup is late. In other languages, this might be done with a work queue / scheduler (think Resque / Sidekiq / etc) with a bunch of accounting to cancel the future job when the pickup happens. Using a GenServer (by way of GenStateMachine) makes the logic match the problem description, by using a state_timeout and transitioning on pickup.

hauleth

hauleth

Yes and no, they use case is completely different. GenServers are useful in cases where you need failure isolation.

Not Phoenix, but Cowboy which is underlying HTTP and WebSocket server.

If you use GenServer incorrectly it will be and bottleneck, but it has it’s use cases. For example imagine that you want to do background processing of data in a pool. Then you can use pool like poolboy and then each GenServer process will be responsible for one task. Other use case is when you want to provide simple cron-like features. Or you want to handle custom startup-shutdown features (for example connection draining). And so on. There is a lot of possible use cases outside of the REST-like HTTP connection handling.

Where Next?

Popular in Questions Top

beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement