All topics / Tokio: The Async Runtime

Tokio: The Async Runtime

Learn the runtime every async Rust web framework sits on: why Rust async needs a runtime at all, futures and how await works, spawning tasks, the multi-threaded work-stealing scheduler and blocking, channels and synchronization, and select with timeouts. The engine under axum, actix, and Rocket — made visible.

  1. What Tokio Is & Why Futures Need a Runtime Rust ships async/await and the Future trait but no runtime — futures are inert and do nothing until polled. Tokio is the engine that drives them, and #[tokio::main] wires it up.
  2. Async, Await & Futures How an async fn becomes a state machine implementing Future, what .await actually does at runtime, and why cooperative yielding means CPU-bound code starves Tokio.
  3. Tasks & Spawning Spawn independent work with tokio::spawn, await its JoinHandle, see why tasks are cheap green threads, learn the 'static/Send rules, and use join!/try_join! for concurrency without spawning.
  4. The Runtime & Scheduler How Tokio's multi-threaded work-stealing scheduler runs thousands of tasks on a handful of threads, why blocking a worker is the cardinal sin, and how spawn_blocking saves you.
  5. Channels & Synchronization How Tokio tasks coordinate: message passing with mpsc, oneshot, broadcast, and watch channels, plus async-aware Mutex versus std Mutex and the don't-hold-a-guard-across-await rule.
  6. select! & Timeouts Race several futures with tokio::select! — first to finish wins, the rest get cancelled. Master cancellation safety, timeout for bounding ops, interval for ticks, and graceful shutdown.
  7. Where to Go Next The payoff: see how axum, actix, and Rocket are Tokio apps, tour the async ecosystem (net/io/fs/time, hyper, tonic, reqwest, sqlx), weigh the alternatives, and pick something concurrent to build.