Matt

Matt

I have a question about the structure of an OTP application. Given a fictitious employee time tracking application for my example, I am trying to reason which application layout would make the most sense using OTP.

Note: This is just an example fictiicous application, nothing I am working on. Just an example to better understand OTP orchestration.

The fictitious employee time tracking application will have employees, schedules, time tracking (clock in/clock out), vacation requests, and overtime submissions. Following OTP, each one should be a process, gen_server for example. Which application makes the most sense:

Option Number One

  • Each employee is a gen_server process.
    The employee process holds the employees name, id number, etc.

  • Each schedule is a gen_server process.
    A schedule holds an employee’s schedule, which days they work, etc. The schedule process is linked to an employee, or a worker process under an employee gen_server?

  • Each time tracking event is a gen_server process.
    This process holds a clock-in or clock-out event. Also linked to an employee, or supervised by an employee gen_server, not sure which is better. This does seem like too many processes may accumulate over time though.

  • Each vacation request is a gen_server process
    This process holds information regarding an employees vacation request, dates, and approval information. Also linked, or supervised by an employee gen_server process.

  • Each overtime submission is a gen_server process
    This process holds overtime information, and who may have approved it. Also linked to an employee, or supervised by an employee gen_server process.

This setup seems like it might spawn too many gen_server processes especially for the clock-in/clock-out. So would the second option be more appropriate?

Option Number Two

  • Each employee is a gen_server process.
    The employee process holds the employees name, id number, etc.

  • Each schedule is a gen_server process.
    A schedule holds an employee’s schedule, which days they work, etc. The schedule process is linked to an employee, or a worker process under an employee gen_server?

  • Each time events is a gen_server process.
    This process holds a list of clock-in or click-out events. One process for each employee. So, instead of one process for each clock-in or click-out even, just one process holding a list of clock-in and click-out events.

  • Each vacation requests is a gen_server process
    Same as the above, one process per employee, containing a list of vacation requests instead of each request being a process itself.

  • Each overtime submissions is a gen_server process
    Same as the above, one process per employee, containing a list of overtime requests instead of each request being a process itself.

I’m very curious to hear your thoughts. Please remember this application is not real, not a web app question and not a persistence question. Just simply curious about people’s opinion on structuring processes.

Showing Posts 1 to 10

idi527

idi527

Have you considered using ETS? It is part of OTP after all.

ericmj

ericmj

Elixir Core Team

It’s a common misconception that you should use processes to structure your application. Instead use modules to organize your application and use processes when you need concurrency or shared state between processes.

I think you should start by considering the external interface of your application, right now it’s a black box so you can design it however you want. But if you add an HTTP interface you probably want to handle requests in parallel, so the web server will start a process per connection or request. So now you need shared state between these processes which can be solved as you proposed, but your solutions are overly complex. Why a process per employee, why not a single process for all employees, or a single process for your whole database?

You should also be careful about using processes to store information that is not transient. You don’t want to lose employee information because the employee process crashed.

13
Post #2
outlog

outlog

lack of persistence, comes to mind.. what happens in catastrophic events?

and then the “to spawn or not to spawn”

eg. your usage of processes could be construed as OO programming with all the side effects and “spaghetti code” of that..

Wouldn’t the employees also work in different locations, and then in different departments, shifts, etc. and that is perhaps also were a genserver boundary starts to make sense..

I like this talk as a good introduction to context mapping/DDD etc.

Matt

Matt OP

It’s just an example, to ask about processed. Persistence isn’t in question. But I know what you’re saying.

Matt

Matt OP

My question is just about processes only. Not concerned about persistence, nor Phoenix— the application isn’t even real :smile: just curious to hear about people’s opinion about structuring processes.

Matt

Matt OP

Perhaps I didn’t form my question correctly. This application isn’t real, my focus isn’t the interface, nor disk bases persistence. I’m not even saying such an application is practical. Just structuring an application with processes — a pure Elixir OTP application using gen servers. The persistence in my example are gen servers is all.

Is it really a misconception to structure an app with processes when you need state? Is that not the whole idea of thinking in processes?

I think you’re right, there’s pretty much no standard it’s all up in the air as to how you want to do it.

idi527

idi527

ETS tables are stored in memory, so I didn’t suggest it for persistence – I suggested it because having a single “source of truth” for state is simpler (at least in my experience) to manage than if it is distributed over multiple processes.

Plus :ets has many other helpful functions for iterating over the data which you’d probably have to reimplement yourself if you go with bare genservers.

ericmj

ericmj

Elixir Core Team

Yes, that’s the misconception. You should start by structuring your application using modules and then add processes where they are actually needed to solve your business problems.

Matt

Matt OP

Yes, I get it. Thank you for your suggestion. What I’m really after and it’s hard to get answers like this because we are all speed readers :laughing: is I’m just curious about people’s opinion structuring an all gen_server application, essentially. One process for each element, one process holding a list of elements, etc.

Matt

Matt OP

Yes, ets is awesome. I use it, and mnesia. I don’t know why people hate on mnesia so much. Again, my question isn’t about easy — just: if you were limited to just gens how would you structure this app. Maybe that’s how I should have asked the question :smile:

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews