Supervision
Build fault-tolerant systems with OTP-style supervision trees.
What is Supervision?
Supervision is a strategy for handling failures in actor systems. Instead of crashing the entire application when something goes wrong, supervisors can restart failed actors, isolating failures to small parts of the system.
cineyma's supervision is inspired by Erlang/OTP's "let it crash" philosophy:
- Actors form parent-child hierarchies
- Parents supervise their children
- Failures are isolated and can trigger restarts
- Panics never crash the runtime
Supervisor Strategies
cineyma provides three supervision strategies:
Strategy Details
Stop (Default)
When an actor fails, it stops permanently. The parent receives a Terminated message.
Restart
Restart the actor up to N times within a time window. If the limit is exceeded, the actor stops permanently.
The factory function || Worker::new() is called on each restart to create a fresh actor instance with clean state.
Escalate
Propagate the failure to the parent supervisor, which then handles it according to its own strategy. This creates fault-tolerance hierarchies.
Creating Supervision Trees
Build hierarchical supervision structures:
Panic Handling
cineyma catches all panics at actor boundaries. A panic in a handler triggers the supervision strategy:
When an actor panics during a send() call, the caller receives a MailboxError::MailboxClosed error. Design your system to handle this gracefully.
Best Practices
Use supervision for I/O actors
Actors that interact with external systems (databases, APIs) should have restart strategies to recover from transient failures.
Keep state minimal in restartable actors
Actors that might restart should minimize in-memory state. Store important state externally or reconstruct it on startup.
Use Escalate for critical failures
When a child failure indicates a broader problem, use Escalate to let the parent decide how to handle it.
Set appropriate restart limits
Too many restarts might indicate a persistent problem. Set limits that make sense for your failure modes.
Supervision Tree Example
When a Worker panics:
- Worker stops
- OrderDepartment receives Terminated
- If escalate: OrderDepartment treated as failed
- RootSupervisor restarts OrderDepartment
- Fresh OrderDepartment spawns new Workers
