M:N Work-Stealing: Tokio's Dual Thread Pool Strategy

Tokio utilizes an M:N task scheduler, efficiently mapping numerous lightweight tasks to a smaller set of OS threads. It employs a work-stealing algorithm with local run queues for balanced task distribution and manages distinct thread pools for asynchronous and blocking operations.

The top 3

  1. Tokio's Dual Pool: Local & Global Queue Mechanics: Tokio's multi-threaded runtime utilizes a global queue and a dedicated local queue for each worker thread. Tasks are primarily added to local queues, but if a local queue exceeds 256 tasks, half are moved to the global queue to manage space. Idle workers first attempt to steal tasks from other local queues, then fall back to the global queue if no local work is found.
  2. M:N Work-Stealing: Tokio's Latency & Throughput Edge: This work-stealing approach significantly boosts performance, with some benchmarks showing up to a 10x speedup in earlier revisions of the scheduler. It minimizes contention by allowing processors to operate independently under load and enhances load balancing by redistributing tasks when unevenly distributed, leading to reduced latency for I/O and higher throughput for various workloads.
  3. Tokio's Work-Stealing Evolution: Solving Async Rust's Challenges: Tokio's shift to an M:N work-stealing scheduler with dual queues addressed challenges in earlier async runtimes, such as load imbalance and high scheduling overhead. This design was motivated by the need to efficiently multiplex many user-land tasks onto a smaller number of OS threads, preventing starvation and ensuring fair, timely execution of asynchronous computations.

Sources

Open the full topic