Could someone clarify what is meant here by serialize your requests on a single server
?
I’m assuming the receiving GenServer would also be running asynchronously on the remote node, so what is the difference with Tasks?
Taken from the docs
Both :rpc
and using a GenServer would serialize your requests on a single server, while tasks are effectively running asynchronously on the remote node, with the only serialization point being the spawning done by the supervisor.
All the approaches run code on the remote machine, but which process they do it in is different:
- a named GenServer will handle the message in its single process
-
:rpc
will perform the specified apply
in the RPC server process. There’s one of these per node
- a remote
Task.Supervisor
will handle the “start a task” request in its single process, which starts a separate process where the Task does its work
The difference is easier to think about if you imagine the “actual work” part of each of these takes a while - imagine Process.sleep(2000)
as a stand-in. If three requests in close succession are sent to the three approaches, here’s what happens:
- the GenServer and
:rpc
approach both take slightly more than 3x the time of one “actual work” step, because they handle messages one-at-a-time
- the
Task.Supervisor
approach will take slightly more than 1x the time of an “actual work” step, as it handles messages by starting a process for each
4 Likes
Thanks a lot for the thorough reply
So my initial assumption that the GenServer runs synchronously was wrong. Is this specific to call
? Would cast
take the same time as the Task.Supervisor
approach?
Both call
and cast
send messages to the GenServer, but they differ on what they do next.
-
cast
returns immediately; it cannot fail, even if the recipient PID has exited or never existed in the first place.
-
call
waits for a reply, blocking the process until the GenServer sends a reply
In the three-requests example, the three cast
s will all return immediately to their callers but the GenServer will process their messages one at a time
2 Likes
Makes perfect sense. Thanks a ton