📎 Webclip
Distributed PubSub in Elixir
The article walks through building a PubSub in Elixir from a local Registry-based version to a distributed version. It uses a named process registry for local subscriptions, then adds a per-node GenServer and pg group membership so each cluster node forwards messages to its own subscribers.
It also shows clustering with libcluster and LocalEpmd for local nodes, and tests the system with multiple IEx nodes and clients subscribed to different topics. The article ends by noting that the example is for learning only and that Phoenix.PubSub is the production-ready option.
Reading notes#
- PubSub is described as an asynchronous publish/subscribe pattern where a publisher broadcasts to a topic and subscribers consume the message.
- The local implementation is named Hermes and starts from a Registry with duplicate keys so multiple processes can subscribe to the same topic.
Registry.register/3is used to subscribe a process to a topic, andRegistry.dispatch/3is used to iterate over the registered processes and send them a message.- The app supervisor starts the Registry so subscriptions are available when the application boots.
Hermes.subscribe/1registers the current process, andHermes.publish/2broadcasts to every subscriber registered under the topic.- A single-node version works in IEx, but connected nodes do not share the Registry, so publishing on one node does not reach subscribers on another.
- Erlang
:pgis introduced as a process-group mechanism that can include processes from different nodes under a shared group name. - The article uses libcluster with the LocalEpmd strategy so nodes discover and connect to each other automatically.
- A
Hermes.PGServerGenServer starts:pgand joins the group{:hermes, Hermes.PubSub}. Hermes.publish/2is changed to send a broadcast message to the remoteHermes.PGServerprocesses in the group, while also broadcasting locally.Hermes.PGServerhandles{:broadcast_to_local, topic, message}by callingHermes.broadcast_local/2.- A simple
ClientGenServer subscribes to a topic and prints any received message. - The final test uses three nodes, Alice, Bob, and Carol, with clients split across
:"user.created"and:"user.updated"topics. - Messages published on one node are received by subscribers on the matching topic across the cluster.
- The article states that the implementation is a toy example for learning and recommends Phoenix.PubSub for production.
