Kernel vs. User-Space: The Microsecond vs. Nanosecond Divide

Kernel thread context switches typically cost between 1 to 300 microseconds, whereas lightweight user-space processes like those in the Erlang BEAM VM can switch contexts in approximately 20 nanoseconds, representing a difference of several orders of magnitude.

The top 3

  1. Kernel Switch Cost: TLB & CPU State Overhead: Kernel context switches incur overhead from saving/restoring CPU registers, cache invalidation, and Translation Lookaside Buffer (TLB) flushes, contributing to microsecond latencies.
  2. User-Space Switch: Minimal State, No Kernel: User-space schedulers achieve nanosecond context switches by avoiding costly kernel calls and saving only essential, process-local state directly within the application runtime, exemplified by Erlang's BEAM.
  3. Concurrency Scaling: Nanosecond's Million-Task Edge: The nanosecond speed of user-space context switches enables efficient management of millions of concurrent lightweight processes, a scale unachievable with heavier, microsecond-costing kernel threads.

Sources

Open the full topic