Free — no signup required

Context Switching: The Cost of Multitasking

2 min read

The "Stop-and-Go" Penalty

Swapping processes in and out of the CPU thousands of times per second is not free. Each swap is called a context switch, and it carries real overhead.

Imagine a surgeon (the CPU) performing an operation (Process A). The hospital administrator (the scheduler) suddenly says: "Stop — you need to go to operating room 2 immediately (Process B)." The surgeon cannot just teleport. They must:

  1. Pause the current procedure safely.
  2. Document exactly where they left off (suture count, instrument positions).
  3. Walk to the other room.
  4. Read the other patient's chart and resume from where that procedure was paused.

In computing, a context switch requires the kernel to:

  1. Save the running process's CPU register state (program counter, stack pointer, general-purpose registers) into its Process Control Block (PCB) in memory.
  2. Save the memory management state (page table pointer, TLB entries).
  3. Load the saved register state of the incoming process.
  4. Flush the CPU's Translation Lookaside Buffer (TLB) if the two processes have different address spaces (on most architectures).

The TLB flush is particularly expensive. The TLB is a hardware cache that speeds up virtual-to-physical memory address translation. After a context switch to a different process, the TLB contains stale entries for the old process, and the new process must rebuild it from scratch — causing a burst of slow memory lookups until the TLB warms up again.

A single context switch takes roughly 1–10 microseconds depending on hardware. At 1,000 switches per second, that is 1–10ms of pure overhead per second per core — CPU time spent switching rather than doing useful work. This is why the scheduler tries to balance fairness with minimizing unnecessary switches, and why long-running CPU-bound tasks benefit from larger time slices.

Interview Tip

A common SRE interview question is: "Your application's CPU usage is high but throughput is low. What would you look for?" One answer is excessive context switching. You can diagnose this with vmstat 1 (look at the cs column) or pidstat -w 1. If context switches per second are in the millions, the system may be thrashing — too many threads competing for too few cores. The fix is often reducing thread count or using async I/O to reduce the number of runnable threads.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free