ER

OTP Patterns

OTP design principles: gen_server, supervisor trees, and application structure for fault-tolerant Erlang systems

Details

Language / Topic
erlangErlang
Category
Architecture

Rules

balanced
- Use `gen_server` for stateful server processes — implement `init/1`, `handle_call/3`, `handle_cast/2`, and `handle_info/2` callbacks.
- Use `supervisor` with a `one_for_one` or `rest_for_one` strategy and set `MaxR`/`MaxT` restart intensity to prevent infinite restart loops.
- Structure each feature as an OTP application with its own `application` behaviour, supervisor tree, and `start/2` callback.
- Use `gen_statem` for processes with explicit state machine semantics — it provides `state_functions` and `handle_event_function` modes for different state dispatch styles.
- Use `gen_event` for event logging and notification systems where multiple handlers subscribe to the same event manager process.
- Use `sys:get_state/1` and `sys:replace_state/2` for debugging live `gen_server` state in a running node without stopping the process.
- Use `application:get_env(App, Key)` for runtime configuration — set defaults in the `.app.src` `{env, [...]}` section and override via `sys.config`.