liveBook · Manning

Child specification

As explained in Elixir in Action, Third Edition

child_spec/1 returns a supervision child specification describing how a process should be started and supervised.

A child specification tells a supervisor how to start a process, identify it, and manage its termination. Supervisors use these specifications to build fault-tolerant process trees and apply a restart strategy such as :one_for_one. section 8.3.3 section 8.4

A basic specification is a map containing:

  • :id, an arbitrary term that uniquely distinguishes the child among its supervisor's children. section 8.3.3
  • :start, a {module, function, arguments} triplet passed to apply/3; the invoked function must start and link the process. section 8.3.3
  • :type, which identifies a :worker or :supervisor; omitting it defaults to :worker. section 9.1.5
  • :shutdown, which controls graceful termination waiting time and can be an integer, :infinity, or :brutal_kill. section 9.1.6

Prefer module-based specifications

A direct map can start a cache with Todo.Cache.start_link(nil), but it duplicates the start-function signature. Passing {Todo.Cache, nil} delegates specification construction to Todo.Cache.child_spec(nil), while [Todo.Cache] uses the module's default argument form. This keeps supervisor code aligned with the child implementation. section 8.3.3

Assign unique identities to pooled workers

When several workers use the same default module-based specification, they receive the same module ID. Build each worker specification with Supervisor.child_spec(default_spec, id: worker_id) so the supervisor can distinguish and restart workers individually. section 9.1.5

Modules that do not receive an autogenerated child_spec/1 can define one explicitly. A supervisor child can itself be a supervisor by returning type: :supervisor and a start tuple for its supervision process. section 9.1.5

Use DynamicSupervisor when children are created on demand rather than known when the supervisor starts. Each DynamicSupervisor.start_child/2 call receives a child specification, such as {Todo.Server, todo_list_name}. section 9.2.2

Libraries can expose child_spec/1 without revealing their internal processes. Plug.Cowboy.child_spec/1 packages an HTTP listener and connection processes for insertion into an application's supervision tree. section 11.3.2

How Elixir in Action, Third Edition covers it

How deep it goes
Defines core fields, generated and explicit child_spec/1, unique worker IDs, shutdown behavior, callback modules, and dynamic children.
Examples
Uses Todo.Cache, database workers, Todo.Server, and Plug.Cowboy specifications to demonstrate static, pooled, dynamic, and library-provided children.
What it assumes
Assumes familiarity with Elixir modules and processes while teaching supervision, child startup contracts, and process-tree organization through runnable examples.
What you can do after
Builds supervision trees that start, identify, isolate, terminate, and restart worker or supervisor processes predictably.
How it connects
Connects supervisors with restart strategies, GenServer, DynamicSupervisor, process isolation, supervision trees, and Plug.Cowboy.

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.