Preemptive Scheduling via Reductions, Not OS

The BEAM virtual machine employs its own preemptive, priority-based scheduler that manages Erlang processes by allocating CPU time in small 'reductions' (units of work). This VM-level scheduling bypasses the slower OS kernel context switches, allowing for rapid, fine-grained switching and ensuring fair execution without any single process monopolizing resources.

The top 3

  1. BEAM's 'Reduction' Unit for Fair Scheduling: The BEAM scheduler uses 'reductions' as its internal unit of work, where each reduction represents an operation like a function call or message passing. Each Erlang process is allotted a fixed number of reductions (typically 2000-4000) before being preempted, ensuring fair CPU distribution independent of OS timers.
  2. BEAM vs. OS: Nanosecond Context Switch Advantage: BEAM achieves ultra-fast context switching by performing it entirely in user space within the VM, costing approximately 20 nanoseconds. This is significantly faster than typical OS kernel-level context switches, which involve saving and loading more extensive CPU state and can take 500-1000 nanoseconds (0.5-1 microsecond).
  3. Erlang's Custom Scheduler: Built for Mass Concurrency: Erlang's BEAM Virtual Machine employs its own preemptive scheduler, managing lightweight processes internally rather than relying on the operating system's scheduler. This custom design allows BEAM to efficiently handle millions of concurrent processes with superior fault tolerance and minimal overhead, which general-purpose OS schedulers are not optimized to achieve.

Sources

Open the full topic