Is GenServer a good solution to queue db updates?

Hello, I am using psql(ecto). I have a table with a column selected with default value false.
I want to implement some code like that:

if selected value is false do
  update selected value to true
  send emails to users/make some api calls etc
else
 do nothing
end

Problem is, this code could be reached by many users at the same time. I really don’t want to risk sending same email/make same api calls twice or even more times. I was thinking about using GenServer to check and update selected value synchronously and after that send email or do nothing, but maybe there is some easier way?

1 Like

You can of course do that. The question to ask is:

  1. Will there be a mismatch state between GenServer and psql at any point ? Does it matter ? What would you do at that point.

It might be an absolute non-event in your case, however it’s a good question to consider.

Hey, I have been thinking about what you said. I guess I was focused on the wrong part of the problem.

I guess I will update records outside of GenServer. I am going to use GenServer to send emails/api calls as I want to do it only once, I’d store record’s id in the state and would compare it to check if the emails were already sent.

Not sure about one thing, though. Should I spawn another task from within GenServer to take care of the sending logic or should I just handle it within GenServer? I am afraid the latter would become a bottleneck at one point.

Usually I would start with the easiest which is to have the sending logic in the GenServer. After measuring and finding its a bottleneck would I bother doing a Task spawn. Given the sending logic as a function converting it to a Task becomes trivial.