Communication between processes in the Erlang VM happens through message passing. In fact, all communication across processes occurs through the same mechanism. All you need is an address and you can send a message to it, with the lower level building blocks being
send
andreceive
. This is true even if that address is on another node in a cluster of Elixir nodes that are aware of each other. In fact, it works in exactly the same way.There’s no special RPC syntax, no special error handling. In fact, you can write your code without knowing whether a message is destined for another node or process running on the same node. In an example
send(address, message)
theaddress
could be a local pid, “via tuple”, or a pid from another node. It could be a registered name combined with a node in a tuple like{node, MyProcess}
.send
will figure out what to do. This is also true when using the higher level versionsGenServer.cast/2
andGenServer.call/3
.It might not be obvious at first glance, but this is a superpower. Let’s take a simple example of some code that runs on a single node and then upgrade it to share state across a cluster of nodes. To follow along, all you’ll need is Elixir installed.
Note:This blog post is an extended version of an example from my Code BEAM Mexico keynote.
continue reading on blog.appsignal.com
⚠️ This post links to an external website. ⚠️
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.