cochranjd

cochranjd

Typical item ordering app - questions about GenServer, Actors, PubSub and some different approaches

Have an app I’ve been working on for a while that will be used by a small number of folks, but also serves as a good playground for learning new technologies.

I’ve been working on rewriting this app using Phoenix and have implemented pretty much all of it using LiveView, which has been great.

That said, I’ve started thinking about reworking some parts of it, and while I enjoy learning about how to take advantage of some things I’ve not used much to this point - like GenServers - I want to make sure that I’m not setting bad habits or falling into anti-patterns. I guess all of that is to say that in this particular case, I don’t necessary NEED anything complicated because the technical demands are low and the traffic is also low. At the same time, I like using it as a way to learn how to approach solving some of these problems and want to try and build a habit of using best practices.

I’ve done a lot of reading on GenSevers, including many warnings about not forcing it and using GenServers more than needed. Additionally, I’ve read the often recommended The Erlangelist - To spawn, or not to spawn?, which was great.

I mostly wanted to get thoughts on the pros and cons of different approaches to break up the order handling portion of the application, which for now is handled in the LiveView process like everything else.

Actors as GenServers

One thought was to take each part of the processing (verify/reserve inventory, persist order to DB, export to a 3rd party system folks use to view incoming orders, communicate with user via email/text) and break them into GenServers, utilizing PubSub for communication. I like the decoupling this provides, but after reading the article above, I worry that this is an example of using processes to organize code, which is frowned upon! I do see some things, like inventory, that might benefit from being a GenServer simply because it would force sequential handling of order requests vs having everything in LV where I could get a problem if folks are checking inventory in parallel LV processes, then placing their orders. If there is 1 item left, both may verify that the item they want is available, then store their order. If I’m understanding correctly, a GenServer would give me the ability to prevent this due to the fact that it can only do one thing at a time. This would also allow me to store the inventory in the GenServer and avoid having to check with the database outside of startup.

Orders as GenServers

The idea here would be to create a GenServer for each order, allowing that GenServer to handle the order from placement to conclusion. With that said, I’m not sure this offers many benefits over just doing everything in LivewView. I could still run into issues where folks ordering at the same time cause problems when verifying inventory. It would give the benefit of a failed order being a separate process though.

Inventory only as a GenServer

Given the benefits mentioned above, I could simply make my inventory management a GenServer, having all orders check with it for inventory support, but allowing the other parts of the ordering process (persisting, exporting persisted order to a 3rd party, communication) stay in LiveView. I would likely still move the 3rd party interactions to GenServers for protecting against failures, but not as “Actors” that utilize PubSub. Just as typical call/cast interactions.

I know I’ve got a lot to learn and I’m sure my mental models aren’t all correct here, so hoping to learn a ton. Just wanted to throw some of these ideas out and get a sense for what some of you that have done this for a while and have more knowledge about how to handle these things would recommend.

Thanks!

Where Next?

Popular in Questions Top

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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement