Introduction to Processing the Graph

Learn how to use Elixir processes as graph nodes using libgraph.

It’s time to come back to Elixir. We’ll return to native graphs and make use of some OTP machinery—agents, generic servers (or genservers), and supervisors.

We know that libgraph nodes could contain any Elixir term. Well, that also means we could use an Elixir process as a graph node.

Graph with processes

Let’s try something here. Let’s try creating a graph with processes for nodes and then build a supervision tree over that graph. We’ll use genservers for those processes and attach them to a supervisor to manage the process lifecycle.

To keep things simple, let’s just reuse one of our example graphs and map that to a process graph. And we’ll save the example graph node term in the process graph node state.

Challenges

So if we have the new process graph under a supervision tree, we should be able to terminate any of the processes and have that automatically restored. But there are a couple of issues we need to deal with. The new process will have no memory. When it’s generated, it won’t have any state that was carried by the old process. And worse, it’ll fall outside of the process graph—it won’t be connected to any other node.

Solution

But we can fix these issues. We just need a couple more processes and agents this time—one to cache the state of any failing process and another to save the process graph itself. The following figure shows the basic notion.

Get hands-on with 1200+ tech skills courses.