GenServer as a Coordinator
Learn how GenServer acts as a coordinator to help the concurrent systems.
We'll cover the following
In the previous lesson, we peeked at all of the concerns a GenServer handles for us when we attempted to implement a mutable counter
. At the same time, we have also shown how we can implement a very simple counter
with agents in four lines of code. Behind the scenes, agents are implemented with GenServers. Therefore, if we want to use a GenServer, we need more than just a mutable state. One such example is when we need a process to coordinate the activities of multiple other processes.
Allocation in concurrent systems
In a concurrent system, coordinating the allocation and release of resources is sometimes demanding. Elixir developers rarely rely on try/catch
or try/rescue
. In many cases, it’s cleaner to use tuples and pattern matching because operations such as File.read/1
return {:ok,contents}
or {:error, reason}
instead of failing with an exception. There’s more to the story, though.
The try
and catch
or rescue
operations are just not enough when processes are involved. If a
process terminates due to another linked process, we’re done. We can’t catch
, after
, or rescue
across processes.
Check this out, by running the commands written below.
Get hands-on with 1400+ tech skills courses.