The global module can lock a callback to one node of a BEAM cluster at a time
May 25, 2024
:global.trans/3 takes a global lock on a given ID. So with several BEAM nodes (instances of the Erlang VM, connected as a cluster), a callback only runs once at a time across the cluster.
def put_transaction(key, value) do
:global.trans(key, fn ->
put(key, value)
end, 0)
end
Fly.io uses this to build a simple key-value database without data races.