Appearance
Client-server with GenServer
Read first: Simple state with agents
Source: Elixir guide, Client-server with GenServer.
The last feature is that a client can send SUBSCRIBE shopping. The client then gets real-time notifications (milk SET TO 1, milk DELETED) from a bucket anywhere in the cluster. An AgentA simple wrapper around state: a process that you read and update with functions that you pass to it.Read the page · Glossary cannot receive messages, so KV.Bucket becomes a GenServer.
Links and monitors
How do you know that a ProcessThe unit of concurrency in Elixir. All code runs inside a process. Processes are isolated and concurrent, and they communicate by messages. They are not operating system processes. They are light enough that you can run hundreds of thousands of them.Read the page · Glossary ended? You have two options:
- Links tie two processes together. If one process crashes, the other process crashes too. Supervisors are the exception, because they Trapping exitsThe call
Process.flag(:trap_exit, true). Exit signals then arrive as ordinary messages and do not end the process. Supervisors do this to survive their children.Read the page · Glossary withProcess.flag(:trap_exit, true). - Monitors only observe. When the MonitorA one-way method to track when a process dies, without a tie between exit signals. When the monitored process ends, the watcher gets a message, for any reason.Read the page · Glossary process ends, for any reason, you get a message and nothing else happens.
elixir
pid = spawn(fn -> Process.sleep(5000) end)
Process.monitor(pid)
flush() # nothing yet, then after five seconds:
#=> {:DOWN, #Reference<...>, :process, #PID<0.119.0>, :normal}A bucket uses monitors to learn that a subscriber went away. The bucket then stops sending messages to it. Without monitors, the list of subscribers grows without limit.
GenServer callbacks
A GenServerA "generic server": a process that holds state and offers synchronous (call) and asynchronous (cast) requests, code reloading, and more.Read the page · Glossary is a process that calls a small set of functions under specific conditions. With an agent, the client code and the server code are in one function. In a GenServer, they are separate:
elixir
# client
def put(bucket, key, value), do: GenServer.call(bucket, {:put, key, value})
# server callback
def handle_call({:put, key, value}, _from, state) do
{:reply, :ok, Map.put(state, key, value)}
endKV.Bucket as a GenServer:
elixir
defmodule KV.Bucket do
use GenServer
def start_link(opts), do: GenServer.start_link(__MODULE__, %{}, opts)
def get(bucket, key), do: GenServer.call(bucket, {:get, key})
def put(bucket, key, value), do: GenServer.call(bucket, {:put, key, value})
def delete(bucket, key), do: GenServer.call(bucket, {:delete, key})
@impl true
def init(bucket), do: {:ok, %{bucket: bucket}}
@impl true
def handle_call({:get, key}, _from, state) do
{:reply, get_in(state.bucket[key]), state}
end
def handle_call({:put, key, value}, _from, state) do
{:reply, :ok, put_in(state.bucket[key], value)}
end
def handle_call({:delete, key}, _from, state) do
{value, state} = pop_in(state.bucket[key])
{:reply, value, state}
end
endGenServer.start_link/3 takes the CallbackA function that your module defines and that a process behaviour calls under specific conditions, for example init/1 or handle_call/3 for a GenServer.Read the page · Glossary module, the argument for init/1, and options (such as :name). start_link runs on the client and init/1 runs on the server.
A call is synchronous. The server must reply while the client waits. A cast is asynchronous and has no reply. The server handles requests in order. The state is now a map with a :bucket key, so it has room for subscribers. get_in/1, put_in/2, and pop_in/1 reach into the map.
Under the hood Our own text. It is not from the official docs.
Why a long MailboxThe place where the messages that were sent to a process wait. receive/1 searches it for a message that matches a pattern. A send does not block: the sender puts the message in the mailbox and continues.Read the page · Glossary hurts. The mailbox of a ProcessThe unit of concurrency in Elixir. All code runs inside a process. Processes are isolated and concurrent, and they communicate by messages. They are not operating system processes. They are light enough that you can run hundreds of thousands of them.Read the page · Glossary is a queue in the order of arrival. receive looks at the messages from the front until one message matches. We measured a message that matched at the end of the mailbox. With 10 messages ahead of it, receive took less than a microsecond. With 10,000 messages ahead of it, it took about 40 microseconds. With 1,000,000 messages ahead of it, it took about 5 milliseconds (41 and 5,149 microseconds in one run, 40 and 4,919 in another).
A GenServerA "generic server": a process that holds state and offers synchronous (call) and asynchronous (cast) requests, code reloading, and more.Read the page · Glossary works through its mailbox in order. If a server gets requests faster than it handles them, every client waits behind the queue. GenServer.call also gives the caller a unique alias, and the server sends the reply to that alias. The trace in the AgentA simple wrapper around state: a process that you read and update with functions that you pass to it.Read the page · Glossary example showed this. The alias prevents the reply from mixing with other messages.
Sources: we measured with a plain receive and :timer.tc/1 on Erlang/OTP 29. The scan cost depends on the pattern and on the VM version.
Subscriptions
elixir
def subscribe(bucket), do: GenServer.cast(bucket, {:subscribe, self()})
@impl true
def handle_cast({:subscribe, pid}, state) do
Process.monitor(pid)
{:noreply, update_in(state.subscribers, &MapSet.put(&1, pid))}
end
@impl true
def handle_info({:DOWN, _ref, _type, pid, _reason}, state) do
{:noreply, update_in(state.subscribers, &MapSet.delete(&1, pid))}
endinit/1 adds subscribers: MapSet.new(), and put and delete broadcast to it:
elixir
defp broadcast(state, message) do
for pid <- state.subscribers, do: send(pid, message)
endReal code uses a call for subscribe, because a call gives back-pressure. This example uses cast to show the callback.
Wiring it into the server
Parse ["SUBSCRIBE", bucket] into {:subscribe, bucket}. Then add a run clause that subscribes, switches the socket to active mode, and loops:
elixir
def run({:subscribe, bucket}, socket) do
lookup(bucket, fn pid ->
KV.Bucket.subscribe(pid)
:inet.setopts(socket, active: true)
receive_messages(socket)
end)
end
defp receive_messages(socket) do
receive do
{:put, key, value} ->
:gen_tcp.send(socket, "#{key} SET TO #{value}\r\n")
receive_messages(socket)
{:delete, key} ->
:gen_tcp.send(socket, "#{key} DELETED\r\n")
receive_messages(socket)
{:tcp_closed, ^socket} -> {:error, :closed}
_ -> receive_messages(socket)
end
end:gen_tcp.recv/3 is passive mode. It blocks until data arrives. In active mode, the socket delivers {:tcp, socket, data} and {:tcp_closed, socket} to the MailboxThe place where the messages that were sent to a process wait. receive/1 searches it for a message that matches a pattern. A send does not block: the sender puts the message in the mailbox and continues.Read the page · Glossary of the process. One process can then receive TCP events and bucket messages together. Many systems use active: :once to prevent a flood in the mailbox. The bucket monitors the process, so when the connection closes, the bucket removes the subscription.
The subscription also works across nodes. With two connected nodes, the changes on one node go to a subscriber on the other node, because everything is message passing.
call, cast, or info?
| Callback | Use for |
|---|---|
handle_call/3 | Synchronous requests. This is the default, because the wait for a reply gives back-pressure. |
handle_cast/2 | Asynchronous requests when you do not need a reply. Use it rarely, because nothing confirms the delivery. |
handle_info/2 | All other messages: plain send/2 messages and monitor :DOWN messages. |
Agent or GenServer?
Agents are a subset of GenServers, and they use GenServers, as SupervisorA process that detects when its children die and starts new children in their place. Processes are often linked to a supervisor for this reason.Read the page · Glossary and Registry do. GenServers are the basic building block of concurrent, fault-tolerant systems. Some people never use agents. Others use agents for small amounts of state. Both choices are correct.
Check yourself
Answer in your head. Then open the answer.
What is the difference between a call and a cast?
A call is synchronous: the server must send a response, and the client waits for it. A cast is asynchronous: the server sends no response and the client does not wait.
When do you use a monitor and not a link?
When you only want to know that a process died, and you do not want to tie your exit signals to it. The death of a monitored process arrives as a message, for any reason.