hubertlepicki

hubertlepicki

In-memory SAAS users account system - design questions

As an exercise in OTP for myself & my team, I was building an in-memory user accounts system. I am roughly following @lance’s path laid out in his book.

Each Account is a GenServer. The primary reason behind this is to enforce that the operations and state transitions between “unregistered”, “unconfirmed”, “active”, “deactivated” states are wrapped in application-level transaction and are no race conditions such as 2 users registered with the same email because double form submit etc. This is enforced by simple state machine module that guards the transition rules.

What I am doing is that I start an AccountsSupervisor with :simple_one_for_one strategy. It is responsible for starting new Account servers.

I want to be able to find each account either by ID, or by e-mail. Both are unique. So I created 2 unique registries: AccountsByEmail and AccountnsById. Whenever I start server for particular account, in it’s init function it tries to register in both registries. If this fails at any point - this likely means server is already started for particular account. When I attempt to log user in, I use registry AccountsByEmail, when I have account ID - the other one.

I have a few questions to the above:

  1. Is my use of Registry as node-wide unique index good? I am especially concerned by the fact that I had to create 2 indices because I am looking the Accounts up in 2 different ways.

  2. I am designing the persistence mechanism for this now. What I think is that along the Account, I will start an AccountDiskWriter or something similar - another process that would be a GenServer,accepting async casts whenever Account changes. It would serialize these changes and write to disk (most likely just insert to DETS).

  3. I need a way to bring everything up on the server start up. I was thinking about simply going through all my Accounts from DETS tables, and restoring it one by one on system start up. Does this make sense?

  4. Where do I load the saved state of the GenServer in case of restart by supervisor, or in case of 3)? I am having some trouble figuring out it. As far as I understand the init function is blocking the parent Supervisor until it returns, so it’ll become a bottleneck if I put the disk reads there. As alternative, I could send myself a message from the init function, then handle it in handle_cast, where I would read the state from DETS, and only then register to the both of my Registries. I am interested, however, from my parent process (like web worker) to know if the Account was started & restored properly. So in such solution I no longer can rely on the returned value of init function, as the account will likely to start properly always - it will fail later in it’s life cycle when it restores state. So I have to block waiting to see if it appeared in Registry, possibly with some timeout. Is this correct solution? I suspect there may be simpler one.

First Post!

mkaszubowski

mkaszubowski

Here are my thoughts on 4):

Why do you want to read the state from dets and only then register the process? I guess you can reverse the order, but maybe I am missing something.

For the rest, you’re correct. init will block the supervisor, so it may slow the system down.

One solution would be to send the message to self() and load the state in the separate handle_info/handle_cast. To be able to detect that the process is ready, you can use a function like this:

def start(...) do
  {:ok, pid} = AccountsSupervisor.start_child(...)
  Accounts.ensure_started(pid)
end

where Accounts.ensure_started(pid) uses a GenServer.call() and just replies with :ok in the callback. The message from GenServer.call is guaranteed to be delivered after the message sent from init so it will block the caller until the process state is restored, but will not block the supervisor.

On the other hand, one of the responsibilities of the supervisors is to ensure a predictable start sequence and give you some guarantees after the start. Maybe it’s a good idea to wait a bit longer when starting the node, but have the data available before any other action in the system can be performed? You might want to consider if this is possible for this server to crash and be restarted. If yes, maybe consider separating the state into one process and all the risky activities to another.

Here are some resources on that topic that might be valuable (including a bit of self-promotion :wink: )

http://mkaszubowski.pl/2017/09/02/On-Restoring-Process-State.html


https://ferd.ca/it-s-about-the-guarantees.html

Where Next?

Popular in Questions Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
clayschick
I'm trying to create a simple query to select distinct values from a column. If I only use select and distinct I get back a list of uniqu...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement