this is my current project structure, the get request has a field that needs external validation
the idea is the endpint dispatch the validation async to a task or spawn (for what I researched, if I dont need a explicity return, spawn its more lightweith and swits me more) and then retrieve the result with a get request via another endpoint, and leave tha
the task to registry the result on the ets and the db, the possible states for the value
would be
value = processing
value = acceptd (waiting for payment)
value = denied ({error, reason}
can I trust the task to perform all its duties without being supervised?
the info registered on db will have a timestamp of 2 or 3 mins, after this everything will be dropped, so it the get request doesnt find the value on the ets for any reason (if too many request hits the post endpoint, the value may be dropped from the ets, I will yet think on the cache logic) neither on the db, it can be assumed that the response was {:error, reason}, but fot that I need to trust that everything delegated to the the asyncs will be executed
also, do I need to protect my system against ets random crashs? and if yes, besides using supervisor to restart it there is a way to retrieve the data?
fyi, the application will be running on cloud with 2cpu and 3gb ram
Short answer: not really, unless you take special care, which would mean (1) Oban jobs – possibly chunked – and (2) state inside the DB with explicit information about how far you got and what’s the current status of each step.
Nope. If ETS randomly loses your data then that’s a all-hands-on-deck OTP bug. Nothing you could. Don’t even think of protecting against that.
Can’t say I completely grasped your problem but IMO you should take special care to break down these separate jobs as a start.
As @dimitarvp says, if ETS itself crashes, that’s an OTP bug. However ETS tables are owned by a process and the tables disappear when the process exits (for whatever reason). So you may benefit from some additional resilience around the owner process. For that, ETS provides an heir option to :ets.new/2
. If the owning process dies, then the heir
process will take ownership.
The eternal library helps keep ETS table alive even when the owner process dies using the heir
mechanism.
Even so, if the owner and the heir keep crashing you can still create a situation when you lose the ETS data. So if its mission critical, serialising the ETS data periodically (at some appropriate “save point”) may still make sense.