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 https://www.theerlangelist.com/article/spawn_or_not, 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

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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 49522 488
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
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 ...
New
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement