Use Channels in a Cluster
Explore how to use Phoenix Channels within Elixir clusters to build scalable real-time applications. Understand connecting multiple nodes, broadcasting messages across clusters, and managing challenges of distributed systems for resilient apps.
We'll cover the following...
Cluster
It’s critical to run multiple servers when deploying a production application. Doing so provides benefits for scalability and error tolerance. For example, the ability to double the number of servers in the event of a higher load is much more powerful than doubling the number of cores on a single server. It can take a few minutes to add more machines but could take much longer to move the application to a different machine with more cores. There may also be a time when a single machine is fully utilized, and we cannot add more CPU cores or memory.
Elixir makes connecting a cluster of BEAM nodes very easy. However, we have to ensure that we’re building our application to run across multiple nodes without error. Phoenix Channels handles a lot of this for us because PubSub is for all message broadcasts, which we’ll look at next.
Connecting a local cluster
Let’s jump right in by starting a local Elixir node (an instance of our application) with a name:
We use the --name switch to specify a name for our node. We can see the name on the input entry line; ours is located at server@127.0.0.1. Open a new terminal and type the following command to start a second node:
We started a second node that doesn’t run a web server by starting mix instead of mix phx.server. We used a different name, remote@127.0.0.1, which gives us two nodes running on the same host domain. We can use Node.list/0 to ...