What Is Asynchronous I/O and How Does an Async Runtime Work
Asynchronous I/O lets one thread wait on many sockets at once; an async runtime polls futures and sleeps in epoll until the kernel reports readiness.

A program that reads from a network socket spends almost all of that time waiting. The CPU is idle while a packet crosses a wire, and a thread blocked in read is a whole stack of memory and a scheduler entry doing nothing. Asynchronous I/O is the set of techniques that let one thread wait on thousands of such sockets and do useful work whenever any of them is ready. An async runtime, such as Tokio for Rust 1.90, is the library that organizes that waiting. This post defines each term from scratch, shows the smallest program that exercises a runtime, and explains what happens underneath when you write .await.
Blocking I/O and the thread-per-connection model
The traditional model is blocking I/O. You call read on a socket, and the kernel puts your thread to sleep until data arrives. To serve a thousand clients at once, you start a thousand threads, each blocked on its own socket. That works, and for modest loads it is the simplest design that exists. Its costs are per-thread: each thread reserves a stack (commonly 8 MiB of address space on Linux by default, though only touched pages are committed), the kernel schedules each one, and switching between thousands of them costs cache and time.
Non-blocking I/O changes the contract. You mark a socket as non-blocking, and read returns immediately with an error meaning "would block" when no data is there. Now one thread can try many sockets, but it needs a way to know which ones are worth trying. Spinning over all of them wastes the CPU you wanted to save.
Readiness: how the kernel tells you which socket is ready
Linux offers epoll for this. You register sockets with an epoll instance, then call epoll_wait, which sleeps until at least one registered socket is ready and returns the list of ready ones. The thread now does work only when there is work. The same idea exists as kqueue on macOS and BSD and as IOCP on Windows, which works on completion rather than readiness. The man page epoll(7) describes the Linux interface, and the Tokio runtime's I/O driver is built on the mio crate, which wraps these three.
Futures: work you can pause
Readiness events alone give you callbacks, and callbacks make straight-line logic hard to read. Rust's answer is the future. An async fn compiles into a state machine that implements the Future trait, whose one method is poll. Calling poll runs the state machine until it either finishes (Poll::Ready(value)) or reaches something that cannot proceed yet (Poll::Pending). Crucially, a future does nothing until somebody polls it. Creating one has no effect; it is a description of work.
When a future returns Pending, it has first arranged to be woken. It stores a Waker supplied by the executor, and whoever will eventually make progress possible (for example the epoll driver, when the socket becomes readable) calls wake() on it. The executor then polls that future again.
The runtime ties them together
An async runtime has three parts that matter here:
The executor keeps a queue of tasks (futures that have been spawned) and polls each one that is ready to run.
The reactor (Tokio calls it the I/O driver) waits in
epoll_waitwhen no task can run, and calls the matching wakers when sockets become ready.The timer does the same job for sleeps and timeouts, so
sleep(1s)does not block a thread.
The loop is therefore: run every task that has been woken; when none are left, sleep in the reactor until an event arrives; repeat. .await is the point where a task hands control back to that loop.
The smallest complete program
This program sleeps twice concurrently on a single thread, which shows the point of the model. With Tokio 1.x, add tokio = { version = "1", features = ["rt", "macros", "time"] } to Cargo.toml.
use std::time::{Duration, Instant};
use tokio::time::sleep;
async fn step(name: &str, ms: u64) {
sleep(Duration::from_millis(ms)).await;
println!("{name} done after {ms} ms");
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let start = Instant::now();
tokio::join!(step("a", 200), step("b", 100));
let total = start.elapsed().as_millis();
println!("both finished in about {} ms", (total + 50) / 100 * 100);
}b done after 100 ms
a done after 200 ms
both finished in about 200 msThe current_thread flavor runs everything on the one thread that called main. join! polls both futures from the same task. Each calls sleep and returns Pending, so the runtime has nothing to run and parks in its driver. At 100 ms the timer fires, wakes the task, and b finishes. At 200 ms a finishes. The total is the longer of the two sleeps, not their sum, because neither one held the thread while waiting. The last line rounds the measured time to the nearest 100 ms so the output is stable; real elapsed time is slightly above 200 ms and varies from run to run.
What goes wrong: blocking inside a task
The model has one rule you must not break: never block the thread inside a task. If a task calls std::thread::sleep, or does a long computation without ever hitting an .await, the executor cannot poll anything else on that thread.
#[tokio::main(flavor = "current_thread")]
async fn main() {
let a = async {
std::thread::sleep(std::time::Duration::from_millis(200)); // blocks the thread
println!("a");
};
let b = async { println!("b"); };
tokio::join!(a, b);
}a
bHere a runs to completion first because it never yielded, and b could not even start. With join! polling in argument order, the output is deterministic. The program works, but all other tasks on this thread stalled for 200 ms. The Tokio documentation on spawn_blocking is the fix for blocking calls, and a later post in this cluster shows how to use it.
Concurrency is not parallelism
A single-threaded runtime interleaves tasks; it never runs two at the same instant. That is concurrency. Tokio's default multi-threaded runtime also spreads tasks over a pool of worker threads, which adds parallelism for CPU-bound parts. Async helps when your program spends its time waiting on I/O. It does not make a computation faster, and a pure number-crunching loop gains nothing from being async.
When not to use async
Async has a cost in complexity: function colors (async functions can only be awaited from async contexts), Send constraints on futures, and harder stack traces. For a program that handles a few dozen connections, plain threads are shorter and easier to debug. Reach for a runtime when you hold many mostly idle connections, or when you need cancellation and timeouts across many operations in a uniform way.


