rapidfsub

rapidfsub

Why require_atomic? defaults to true?

Just one use of “manage_relationship” makes action not able to be atomic.
I think the default value of require_atomic? should be “false”.
Why is it “true”?

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

require_atomic? is a safety guard. It’s like marking code as safe or unsafe in another language. It has little to no value if it defaults to false. It is expected that you will have to turn it off relatively often. That is still important though, as when you see that error you should consider if there is anything to be done for it.

This configuration:

config :ash, :require_atomic_by_default?, false, was made only to assist with the upgrade and will be removed in the future. I would not suggest using it at all.

Also Liked

zachdaniel

zachdaniel

Creator of Ash

Writing atomic code is harder, but here is an example of a case where it will pay off significantly:

update :add_ten_to_score do
  change fn changeset, _ -> 
    Ash.Changeset.change_attribute(changeset, :score, changeset.data.score + 10)
  end
end

The action above raises an error about it not being atomic. In this case, the effect here is that if you ran this same update concurrently on the same record, it’s possible to have inconsistent results.

i.e with a record that has a score of 0
Action 1: record is read, current score: 0
Action 2: record is read, current score: 0
Action 1: new score is determined, new score 10
Action 2: new score is determined, new score 10
Action 1: data is written, new score 10
Action 2: data is written, new score 10

So now even though the action was called twice, and you’d expect a new score of 20, you actually have a new score of 10.

By using an action like this:

update :add_ten_to_score do
  change atomic_update(:score, expr(score + 10))
  # or use the built in
  change increment(:score, amount: 10)
end

you now have a fully atomic update that does not have the same problems as described above

I’ve made some tweaks to the atomic update docs to potentially help with clarity. Update Actions — ash v3.29.3

rapidfsub

rapidfsub

I think you are not an ash framework user.
You don’t get what “not that simple” means.

Just one use of manage_relationship makes action not able to be atomic.
And manage_relationship is a basic feature in ash,
which is for creating two related resources in one action.

Last Post!

rapidfsub

rapidfsub

I am also a race condition worrying guy.
But I think many folks would write a spark dsl for “automatically setting require_atomic? false”,
because require_atomic? true is rare case for most apps. :rofl:

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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

We're in Beta

About us Mission Statement