BEAM / OTP Design Principles
101. Let It Crash
Definition. Allow processes to fail and rely on supervision for recovery; don’t defensively handle every error.
Rationale. Defensive error handling bloats code and often leaves the system in a half-broken state. Letting a process crash and restarting it from a known good state is simpler and more robust.
Example. A worker that encounters unexpected state exits; its supervisor restarts it cleanly.
Anti-pattern / failure mode. Catching every error and continuing with corrupt state. Review signal: code full of try/catch that swallows failures.
Related. Supervision Tree (#104), Fault Isolation (#102).
102. Fault Isolation (BEAM/OTP Supervision)
Process-supervision scope — for service-level containment see Fault Isolation (Distributed Services).
Definition. Isolate failures within independent processes; a crash in one process should not take down others.
Rationale. Process isolation contains failures to their owner. Other processes continue serving, and supervisors can restart only what failed.
Example. A crashing chat session process doesn’t kill the whole server.
Anti-pattern / failure mode. Shared state or linked processes that propagate crashes. Review signal: one user action taking down the node.
Related. Share Nothing (#103), Bulkhead (#39), Fault Isolation (Distributed Services) (#49).
103. Share Nothing
Definition. Processes own their state instead of sharing mutable memory; communicate by copying messages.
Rationale. Shared mutable state causes races and coupling. Share-nothing processes are independent, easier to reason about, and safe to distribute.
Example. Each process holds its own state and receives updates via messages rather than shared ETS/heap.
Anti-pattern / failure mode. Processes reading/writing shared mutable structures. Review signal: race conditions and lock contention.
Related. Message Passing (#106), Fault Isolation (#102).
104. Supervision Tree Principle
Definition. Organize processes into hierarchical supervision structures; supervisors restart failed children according to strategy.
Rationale. Supervision makes recovery automatic and policy-driven. Hierarchies mirror the system’s structure and allow targeted restarts.
Example. A top-level supervisor restarts a whole subsystem on failure; a lower one restarts a single worker.
Anti-pattern / failure mode. Flat process structures with no supervision; manual restart logic. Review signal: processes that die and stay dead.
Related. Let It Crash (#101), Process-per-Responsibility (#105).
105. Process-per-Responsibility
Definition. Assign processes clear, independent responsibilities; one process, one job.
Rationale. Focused processes are easier to supervise, restart, and reason about. Mixing responsibilities makes failures ambiguous and recovery coarse.
Example. Separate processes for connection handling, business logic, and persistence.
Anti-pattern / failure mode. A single process doing connection handling, business logic, and DB access. Review signal: a crash loses multiple kinds of state at once.
Related. Supervision Tree (#104), Message Passing (#106).
106. Message Passing
Definition. Communicate between processes through asynchronous messages; avoid direct shared state access.
Rationale. Messages decouple processes in time and space, enabling concurrency and distribution. They make interactions explicit and traceable.
Example. Send a {:process, payload} message to a worker instead of calling a shared function.
Anti-pattern / failure mode. Synchronous calls or shared memory between processes. Review signal: blocking calls that stall schedulers.
Related. Share Nothing (#103), Location Transparency (#107).
107. Location Transparency
Definition. Design communication so local and remote process interactions can share similar abstractions, while recognizing their different failure and latency characteristics.
Rationale. Uniform abstractions simplify code, but remote calls have different failure modes. Design must account for latency and partial failure even when the API looks local.
Example. Use the same message protocol locally and over the network, but add timeouts and retries for remote calls.
Anti-pattern / failure mode. Assuming remote calls behave like local ones; no timeouts. Review signal: cascading failures from slow remote nodes.
Related. Message Passing (#106), Timeout (#41).
108. Preemptive Scheduling
Definition. Rely on the BEAM scheduler to prevent one process from monopolizing execution; keep work in small reductions.
Rationale. Preemptive scheduling keeps the system responsive even when one process does heavy work. Long-running computations should be broken into small steps.
Example. Chunk a large computation into batches and yield between them.
Anti-pattern / failure mode. Long-running NIFs or tight loops that block schedulers. Review signal: latency spikes during heavy computation.
Related. Concurrency over Parallelism (#61), Process-per-Responsibility (#105).
109. Behavior-Based Design
Definition. Use OTP behaviors to establish consistent lifecycle and interaction contracts; standard behaviors reduce boilerplate and errors.
Rationale. Behaviors (GenServer, Supervisor, etc.) encode proven patterns. Using them ensures correct lifecycle handling and reduces custom, error-prone code.
Example. Implement a worker as a GenServer rather than a hand-rolled process loop.
Anti-pattern / failure mode. Custom process loops reimplementing GenServer poorly. Review signal: inconsistent lifecycle or missing call handling.
Related. Supervision Tree (#104), Let It Crash (#101).
110. Explicit State Ownership
Definition. Make it clear which process or component owns and changes each piece of state; centralize mutable state behind a single owner.
Rationale. Ambiguous ownership causes races and inconsistent updates. A single owner makes state changes serial and predictable.
Example. One GenServer owns the counter; others request increments via messages.
Anti-pattern / failure mode. Multiple processes mutating the same state directly. Review signal: lost updates and inconsistent reads.
Related. Single Writer (#46), Share Nothing (#103).