unfode
How to achieve atomicity (all or nothing)?
Suppose I have two states. In C, I can mutate them atomically (either all or none are mutated) using locks:
bool atomic_mutation() {
lock(state1_lock);
lock(state2_lock);
bool success = mutate1(state1);
if (!success) {
unlock(state1_lock);
unlock(state2_lock);
return false;
}
success = mutate2(state2);
if (!success) {
// revert mutate1(state1)
unlock(state1_lock);
unlock(state2_lock);
return false;
}
unlock(state1_lock);
unlock(state2_lock);
return true;
}
I’m new to Elixir. What I’ve learned is that I normally use an Agent to manage a state. But how to achieve atomicity shown above in Elixir? Thanks!
Edit: I fixed the bug in the C code above as pointed out by @al2o3cr
Marked As Solved
al2o3cr
The BEAM provides the infrastructure, but you need to write the code to glue things together into a consistent distributed system - and to be clear, once you have TWO GenServers you’re trying to make change together you’re in distributed-system territory.
For instance, here’s a very basic “table with a lock” GenServer (see below for notes):
defmodule TableWithLock do
use GenServer
defstruct [:data, :owner]
def unlock(pid), do: GenServer.call(pid, :unlock)
def lock(pid), do: GenServer.call(pid, :lock)
def update(pid, fun), do: GenServer.call(pid, {:update, fun})
@impl true
def init(data) do
{:ok, %__MODULE__{data: data, owner: nil}}
end
@impl true
def handle_call(:lock, {pid, _tag}, state) do
cond do
is_nil(state.owner) ->
# unlocked, pid now owns lock
{:reply, :ok, %{state | owner: pid}}
state.owner == pid ->
# already locked by pid
{:reply, :ok, state}
true ->
# locked by another process
raise "oh no lock contention"
end
end
def handle_call(:unlock, {pid, _tag}, state) do
cond do
is_nil(state.owner) ->
# already not locked
raise "somebody already unlocked this???"
state.owner == pid ->
# locked by the caller
{:reply, :ok, %{state | owner: nil}}
true ->
# locked by somebody else
raise "unlocking somebody else's lock"
end
end
def handle_call({:update, fun}, {pid, _tag}, state) do
cond do
is_nil(state.owner) ->
# not locked
raise "not locked"
state.owner == pid ->
# locked by the caller
result = fun.(state.data)
{:reply, result, %{state | data: result}}
true ->
# locked by somebody else
raise "updater not holding the lock"
end
end
end
{:ok, pid1} = GenServer.start_link(TableWithLock, [1,2,3])
:ok = TableWithLock.lock(pid1)
result = TableWithLock.update(pid1, fn data -> Enum.map(data, & &1*2) end)
:ok = TableWithLock.unlock(pid1)
IO.inspect(result)
There are a LOT of places where this could be work better / handle concurrency better:
- crashing the table when a second process tries to take the lock is not realistic. A better implementation would keep a queue of pids that are currently trying to take the lock in
lockand pick the next one to reply to inunlock. - crashing the table on bogus unlocks isn’t realistic either. An alternative would be to return something from
handle_callthat the implementation ofunlock/1could use to crash the calling process, since unlocking a table that you haven’t locked is a logic error - if a process dies while holding the lock, it will never be unlocked. Tools like
Process.monitorcan help with this, at the cost of additional complexity.
Expanding this setup to TWO tables adds some extra complications:
-
if process A takes lock 1 and then tries to take lock 2, while at the same time process B takes lock 2 and tries to take lock 1 the system is in a classic DEADLOCK situation. The default 5s timeout on
GenServer.callwill eventually pick a winner, but real systems will detect this and complain -
coordinating changes to ensure that they either all appear or all do not is still just as tricky as always. You’d need a third process to coordinate the
TableWithLocksand roll back changes if a future change fails.Note that even the code in your example does not produce atomicity - if
mutate2returnsfalse, the changes frommutate1are still visible.Solving this problem correctly is capital-H Hard and the solutions are highly sensitive to exactly what tradeoffs your particular application can tolerate.
Also Liked
benwilson512
I would simply use the database itself for this, it will provide a ton of dedicated tooling for it.
codeanpeace
If you’re using Ecto, it has a built in way of achieving this via Ecto.Multi
Ecto.Multiis a data structure for grouping multiple Repo operations.
Ecto.Multimakes it possible to pack operations that should be performed in a single database transaction…defmodule PasswordManager do alias Ecto.Multi def reset(account, params) do Multi.new() |> Multi.update(:account, Account.password_reset_changeset(account, params)) |> Multi.insert(:log, Log.password_reset_changeset(account, params)) |> Multi.delete_all(:sessions, Ecto.assoc(account, :sessions)) end end
LostKobrakai
How do you make sure multiple actors changes are actually independent?
In the end you’ll surely get to the fact that immutability prevents certain optimizations, but generally the question should be how much those matter to the endproduct.
Last Post!
codeanpeace
If you’re using Ecto, it has a built in way of achieving this via Ecto.Multi
Ecto.Multiis a data structure for grouping multiple Repo operations.
Ecto.Multimakes it possible to pack operations that should be performed in a single database transaction…defmodule PasswordManager do alias Ecto.Multi def reset(account, params) do Multi.new() |> Multi.update(:account, Account.password_reset_changeset(account, params)) |> Multi.insert(:log, Log.password_reset_changeset(account, params)) |> Multi.delete_all(:sessions, Ecto.assoc(account, :sessions)) end end
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









