💧 Elixir Intermediate

What is Elixir's concurrency model compared to threads?

Answer

Elixir/BEAM processes are fundamentally different from OS threads. OS threads: heavy (~1-2MB stack), managed by the OS kernel, share memory, require synchronization primitives (mutexes, semaphores), and context switching is expensive. Typical limit: thousands per machine. BEAM processes: extremely lightweight (~2KB initial heap), managed by BEAM schedulers (not the OS), share no memory (message passing only), require no synchronization, context switching is fast and cheap. Typical capability: millions per node. Scheduling: BEAM uses preemptive reduction-based scheduling — each process gets a fixed number of "reductions" (function calls) before yielding, ensuring fair scheduling. No process can monopolize the scheduler. Concurrency vs Parallelism: BEAM schedulers run one per CPU core, achieving true parallelism on multi-core machines. Memory isolation: each process has a private heap and GC — a GC cycle in one process never stops others. This model makes writing concurrent Elixir code dramatically safer than thread-based languages.