Erlang / Erlang Basics Interview questions
Explain the execution flow of a gen_server call?
gen_server:call/2 looks like a normal function call, but underneath it's a request/reply
protocol built out of plain message passing plus a monitor for safety.
sequenceDiagram
participant Caller
participant GenServer as gen_server process
Caller->>GenServer: {'$gen_call', {Caller,Ref}, Request}
Note over GenServer: handle_call/3 runs
GenServer-->>Caller: {Ref, Reply}
Note over Caller: call/2 unwraps Reply and returns it
Step by step: the caller sends a tagged message containing its own PID and a fresh unique reference, then
sets up a monitor on the server and blocks in a receive waiting for a reply tagged with that same
reference (with a default 5-second timeout). The server's handle_call/3 callback runs, returning
{reply, Reply, NewState}, and the framework sends Reply back tagged with the original
reference. The monitor exists so that if the server dies mid-call, the caller gets a 'DOWN' message
instead of hanging forever.
More Related questions...