Restore the State for a Node
Explore techniques to restore state for graph nodes managed by Elixir processes. Understand how to trap exits, save state with agents on termination, and update the process graph with new PIDs for seamless node recovery.
State loss problem
Our process graph nodes are supervised, which means that they get restarted when they error and die—which is a very handy thing. But there’s a gotcha. They get reborn as new blank nodes with no previous state. Another process will be set up to take over from the old process.
Obviously, we would like to capture any state from the old process and forward it to the new process—rather like a baton handover in a relay race.
Workaround
As we know, we can make use of an agent to maintain process state at this handover point. The trick here is to push any process state onto the agent at process termination and to fetch that back at process initiation.
It’s time to fill ...