Skip to content

Simple state with agents ​

Source: Elixir guide, Simple state with agents.

Elixir data is ImmutabilityElixir never changes data structures in place. Operations return new data structures. You can pass data around and know that nobody changes it in memory. They can only transform it.Read the page · Glossary and 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 share nothing by default. To share state, processes send messages. You rarely write those processes by hand. Instead, you use one of three abstractions:

  • Agent: a simple wrapper around state.
  • GenServer: a "generic server" that holds state and offers sync and async calls, code reloading, and more.
  • Task: an asynchronous computation. You start it now and read the result later.

This chapter builds KV.Bucket with an AgentA simple wrapper around state: a process that you read and update with functions that you pass to it.Read the page · Glossary.

Agents 101 ​

elixir
{:ok, agent} = Agent.start_link(fn -> [] end)
Agent.update(agent, fn list -> ["eggs" | list] end)
Agent.get(agent, fn list -> list end)    #=> ["eggs"]
Agent.stop(agent)

start_link/1 takes a function that returns the initial state. update/3 takes a function from the current state to the new state. get/3 takes a function from the state to the value to return. The state can become anything, so keep the Agent API inside one module:

KV.Bucket hides the agent behind four functions.

The test first ​

elixir
defmodule KV.BucketTest do
  use ExUnit.Case, async: true

  test "stores values by key" do
    {:ok, bucket} = KV.Bucket.start_link([])
    assert KV.Bucket.get(bucket, "milk") == nil

    KV.Bucket.put(bucket, "milk", 3)
    assert KV.Bucket.get(bucket, "milk") == 3
  end
end

async: true runs this test case in parallel with other async cases. Use it only when the test does not touch global state, for example the file system or a database.

The implementation ​

elixir
defmodule KV.Bucket do
  use Agent

  def start_link(opts) do
    Agent.start_link(fn -> %{} end, opts)
  end

  def get(bucket, key) do
    Agent.get(bucket, &Map.get(&1, key))
  end

  def put(bucket, key, value) do
    Agent.update(bucket, &Map.put(&1, key, value))
  end
end

By convention, start_link/1 takes a list of options and passes them to Agent.start_link/2. The next chapter explains use Agent.

Naming processes ​

Pass name: and use the name instead of the PIDA process identifier. spawn/1 returns it, and send/2 takes it to reach that process.Read the page · Glossary: KV.Bucket.start_link(name: :shopping_list). All processes on the node share the names. If two tests start :shopping_list at the same time, the names clash. To avoid this, name each test process after the test. Use the test context:

elixir
test "stores values by key on a named process", config do
  {:ok, _} = KV.Bucket.start_link(name: config.test)
  KV.Bucket.put(config.test, "milk", 3)
  assert KV.Bucket.get(config.test, "milk") == 3
end

get_and_update ​

Agent.get_and_update/2 reads and changes the state in one call. Use it for delete/2, which returns the removed value:

elixir
def delete(bucket, key) do
  Agent.get_and_update(bucket, &Map.pop(&1, key))
end

Client and server ​

Everything inside the function that you pass runs in the agent process, the server. Everything outside the function runs in the caller, the client.

The function goes to the agent and runs there. Its result comes back.

This matters for cost. Process.sleep(1000) on the client delays only that caller. On the server, it blocks every other request to that agent, and clients can time out. GenServerA "generic server": a process that holds state and offers synchronous (call) and asynchronous (cast) requests, code reloading, and more.Read the page · Glossary, in a later chapter, make the client and server split explicit.

Under the hood Our own text. It is not from the official docs.

What Agent.get sends. We traced the AgentA simple wrapper around state: a process that you read and update with functions that you pass to it.Read the page · Glossary 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 with :erlang.trace/3. One Agent.get/2 is two messages. The agent receives {:"$gen_call", {caller, alias}, {:get, fun}}. Then the agent sends back the reply. The fun is inside the first message. As with any message, the system copies it into 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 agent, together with the values that it captured. The agent then runs the function against its own state. This is why everything inside the function happens in the agent.

One Agent.get is two messages. The function goes to the agent and the reply comes back.

Sources: we traced with :erlang.trace(agent, true, [:send, :receive]) on Erlang/OTP 29. The shape of the message is an implementation detail of GenServer. It can change.

Site code: MIT. Pages and diagrams come from the Elixir documentation (Apache-2.0, snapshot of 2026-10-08, Elixir 1.20). This project is not affiliated with the Elixir Team.