liveBook · Manning

Locking

As explained in Elixir in Action, Third Edition

A cluster-wide lock is a distributed mutual-exclusion mechanism that prevents competing operations on different nodes from proceeding simultaneously.

Mutual exclusion prevents competing operations on different nodes from proceeding simultaneously. In an Erlang cluster, :global provides arbitrarily named locks: after one process acquires a lock, no other process in the cluster can acquire it until release. section 12.1.5

A lock is identified by a resource term and a requester ID. Using the process ID as the requester normally limits ownership to one process. Acquisition communicates with other nodes; callers can wait indefinitely or configure a waiting limit. :global.trans/2 packages acquisition, execution, and release into one operation. section 12.1.5

Serialize work across two nodes

Acquire {:some_resource, self()} with :global.set_lock/1 on node1. A matching request on node2 waits until node1 calls :global.del_lock/1. For the acquire-release pattern, wrap the work with :global.trans/2.

Global registration uses the same mechanism internally: it locks the alias, checks whether it is already registered, informs all nodes, and releases the lock. This coordination creates inter-node message traffic, so locking is generally something to avoid when another design can provide the required behavior. section 12.1.3 section 12.1.5

How Elixir in Action, Third Edition covers it

How deep it goes
Explains lock identity, acquisition, waiting, release, transactional wrapping, and registration internals without detailing broader failure handling.
Examples
Demonstrates competing :global.set_lock/1 calls across two nodes, explicit deletion, and :global.trans/2 around protected work.
What it assumes
Assumes connected Erlang nodes, distributed processes, and familiarity with process identifiers and Erlang module calls.
What you can do after
Operate/configure cluster-wide locks with :global.set_lock/1, :global.del_lock/1, and :global.trans/2.
How it connects
Connects distributed mutual exclusion with global process registration and inter-node message passing.

Also explained in

Compare how 18 books explain locking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.