Erlang
concurrency
efficiency
lightweight processes
OS threads

Technically, why are processes in Erlang more efficient than OS threads?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Erlang, a functional programming language designed for building concurrent, distributed systems, is renowned for its ability to handle a high number of lightweight processes efficiently. Understanding why Erlang processes are more efficient than operating system (OS) threads involves looking at several technical distinctions. These differences are rooted in how concurrency is managed and leverage Erlang's virtual machine, the BEAM (Bogdan/Björn's Erlang Abstract Machine).

Key Differences: Erlang Processes vs. OS Threads

1. Lightweight Nature

Erlang processes are extremely lightweight, typically requiring only a few kilobytes of memory. In contrast, OS threads are heavier, often consuming megabytes due to additional OS-level overhead.

  • Erlang Processes: Created and managed within the Erlang VM, do not require a separate OS-level process allocation.
  • OS Threads: Require OS intervention, leading to higher memory and context switching overhead.

2. Context Switching

Context switching is an operation that moves the CPU from one process or thread to another and can significantly affect performance due to the time and resources involved.

  • Erlang: Context switching is handled within the BEAM, making it faster, as it is context switching across Erlang processes in an optimized manner.
  • OS Threads: Context switching is managed by the OS kernel, involving saving and loading registers, memory maps, and more, which is substantially more costly.

3. Scheduling

  • Erlang's Scheduler: The BEAM uses preemptive scheduling for its processes, efficiently managing thousands of processes using a small fixed number of scheduler threads. Also, individual processes receive small timeslices, optimizing for responsiveness.
  • OS Scheduler: OS thread schedulers can be less efficient for high levels of concurrency, often not as optimized for the massive concurrency scaling Erlang aims for.

4. Isolation and Fault Tolerance

Erlang's processes are completely isolated; they do not share memory and communicate via message passing. This isolation contributes significantly to fault tolerance and reliability.

  • Erlang: Each process runs independently, and error in one process does not affect others; the absence of shared state simplifies concurrent operations.
  • OS Threads: Threads may share memory, leading to potential for data races and making fault isolation more challenging.

5. Messaging

  • Erlang: Uses message passing to handle interactions between processes. This asynchronous and non-blocking communication model does not contend for shared resources, thereby reducing the complexity and performance overhead in concurrent programming.
  • OS Threads: Typically need locks, semaphores, or other synchronization mechanisms to communicate or share data, which introduce complexity and performance penalties.

Example Scenario: Chat Server

Consider the implementation of a chat server, which needs to handle thousands of simultaneous connections. Implementing it with OS threads might quickly become impractical due to memory constraints and performance overheads associated with context switching.

Erlang, on the other hand, can spawn a process for each connection efficiently:

erlang
1start_chat_server() ->
2   process_flag(trap_exit, true),
3   loop_lists:foreach(fun start_user_process/1, user_list()).
4
5start_user_process(User) ->
6    spawn(fun() -> chat_session(User) end).

In this scenario, each user connection is mapped to an Erlang process, and the processes communicate using Erlang's message-passing semantics, ensuring responsiveness and scalability without cumbersome synchronization mechanisms.

Summary Table: Erlang Processes vs. OS Threads

Feature/AspectErlang ProcessesOS Threads
WeightLightweight (KB scale)Heavyweight (MB scale) due to OS overhead
Context SwitchEfficient, fast within BEAMSlower, more resource-intensive involving OS kernel
IsolationComplete process isolationShared memory, prone to data races
CommunicationMessage passing, asynchronousSynchronization mechanisms needed (locks, semaphores)
Fault ToleranceHigh, due to isolation and supervision treesLower, due to shared memory

Conclusion

Erlang's design principles target high availability, scalability, and efficient concurrency, breaking away from typical OS-level threading paradigms. By employing lightweight processes, efficient context switching, preemptive scheduling, isolation, and message-passing, Erlang significantly outshines traditional OS thread models for applications requiring massive concurrency, such as telecommunications systems, online games, chat servers, and more.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.