Skip to content

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.

—

Back to TIL

Next: Cloudflare has a tool to purge a domain from the 1.1.1.1 DNS cache

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace