A key-value store associates arbitrary keys with values. In Elixir, it can serve as a compact example of process state, message passing, and callback-based design. section 2.8 section 6.1.3
A process-backed implementation typically:
- Keeps a map as its state and handles put and get requests with separate clauses. section 6.1.3
- Hides generic message-passing code behind start, put, and get interface functions. section 6.1.3
- Uses GenServer for a conventional implementation, but may encounter a single-process bottleneck under concurrency. section 10.3
- Considers ETS for shared key-value structures when performance and scalability matter, while recognizing its limited applicability. section 10.3
Basic interaction
Start the store, put :some_key with :some_value, then get the key to receive :some_value. section 6.1.3
Listing 10.7 GenServer-powered key-value store (key_value/lib/key_value.ex)
defmodule KeyValue do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
def put(key, value) do
GenServer.cast(__MODULE__, {:put, key, value})
end
def get(key) do
GenServer.call(__MODULE__, {:get, key})
end
def init(_) do
{:ok, %{}}
end
def handle_cast({:put, key, value}, store) do
{:noreply, Map.put(store, key, value)}
end
def handle_call({:get, key}, _, store) do
{:reply, Map.get(store, key), store}
end
end
Inspect a complete GenServer-backed implementation using a map, asynchronous puts, and synchronous gets. section 10.3







