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
