liveBook · Manning

:pg

As explained in Elixir in Action, Third Edition

:pg is an Erlang library module for creating cluster-wide process groups and managing their membership.

:pg solves clustered process discovery when several processes must represent the same service or replicated data. It creates arbitrarily named, cluster-wide groups, propagates membership across connected nodes, and exposes group members for later requests. section 12.1.3

Use the group as a coordination boundary:

  • Start :pg on each participating node. section 12.1.3
  • Join processes with an arbitrary group term and each process PID. section 12.1.3
  • Query the group with :pg.get_members/1, then send updates to every replica or queries to one member. section 12.1.3
iex(node1@localhost)2> :pg.join({:todo_list, "bob"}, self())
:ok

iex(node2@localhost)3> :pg.join({:todo_list, "bob"}, self())
:ok

See two node-local processes join the same {:todo_list, "bob"} group. section 12.1.3

How Elixir in Action, Third Edition covers it

How deep it goes
Defines group creation, joining, membership queries, and replica request patterns without detailing implementation internals.
Examples
Uses two connected nodes and Bob’s replicated to-do list as the running process-group example.
What it assumes
Assumes connected distributed Erlang nodes and introduces starting :pg, joining processes, and querying membership.
What you can do after
Apply it via :pg.start_link/0, :pg.join/2, and :pg.get_members/1 to coordinate clustered process replicas.
How it connects
Connects process discovery with distributed systems, process groups, replicas, and GenServer.multi_call/4.

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.