liveBook · Manning

Serialization

As explained in Elixir in Action, Third Edition

Serialization is the handling of competing operations one at a time in a defined sequential order.

Serialization handles competing operations one at a time in a defined sequence. A single server process naturally serializes requests, but can become a scalability bottleneck when many client processes depend on it. section 10.3

A server loop serializes access to mutable state by receiving messages, applying each request, and continuing with the resulting state. Extracting state manipulation into a separate module keeps the loop focused on message handling and state progression. section 5.3.4

Use serialization deliberately:

  • Keep independent tasks concurrent; avoid concentrating unrelated operations behind one process. section 10.3
  • Use a private loop to protect changing state and dispatch each request to a dedicated handler. section 5.3.4

TodoServer

TodoServer serializes add-entry and entries requests around a TodoList. Each message produces a new TodoList value, and the loop continues with that value while callers use a stable server process. section 5.3.4

Listing 5.10 TodoServer basic structure (todo_server.ex)
defmodule TodoServer do
  def start do
    spawn(fn -> loop(TodoList.new()) end)    #1
  end
 
  defp loop(todo_list) do
    new_todo_list =
      receive do
        message -> process_message(todo_list, message)
      end
 
    loop(new_todo_list)
  end
 
  ...
end

Inspect the minimal receive-and-loop structure that serializes access to TodoList state. section 5.3.4

How Elixir in Action, Third Edition covers it

How deep it goes
Shows message-loop implementation and a production bottleneck, but stops short of detailed ordering guarantees.
Examples
Uses a GenServer key-value store, benchmark results, and a TodoServer backed by a TodoList.
What it assumes
Assumes familiarity with Elixir processes and functional state values while demonstrating server state management.
What you can do after
Build it from scratch with a receive loop that applies requests sequentially and carries forward updated state.
How it connects
Connects serialization with processes, concurrency, message passing, mutable state, GenServer, and ETS tables.

Also explained in

Compare how 2 books explain serialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.