skip to content

Your team is moving a Rust CLI tool and a high-traffic HTTP API to async; how do you decide which should go async at all, and on which runtime?

level: principalimportance: should knowfreq 34%

answer

  1. concurrent waits vs sequential work
  2. async spreads up the call stack
  3. runtime choice binds libraries
  4. Tokio, smol, Embassy by use

basics

~20 s

Go async where many I/O waits overlap, like the API; a sequential CLI is simpler blocking, with block_on only for async-only libraries. Pick the runtime your libraries need: Tokio for most servers, smol for small tools, Embassy on microcontrollers.

solid answer

~50 s

Async pays off when a program spends its time waiting on many things at once: thousands of connections, fan-out to backends, long-lived streams. The HTTP API fits; a thread per request costs memory and context switches that tasks avoid. A CLI that reads a file, makes a few sequential requests and exits gains little and pays the costs: `async` spreads up the call stack, spawned tasks need `Send + 'static` futures, blocking calls need discipline, and back-traces get harder to read. Keep it synchronous, or call `block_on` at the edge if a needed library is async-only. For the runtime, the deciding factor is coupling: I/O types bind to one runtime's reactor, so you pick the one your libraries are built on. For most servers that is Tokio; smol suits small, fast-compiling tools and runtime-agnostic libraries; Embassy targets no_std microcontrollers. Then standardise on one runtime per binary.

go deeper

for a junior

Recall that async suits many overlapping I/O waits, not sequential or CPU-heavy work, and that Rust leaves the runtime choice to you.

for a middle

Explain the concrete costs: async spreading up the call stack, Send + 'static on spawned tasks, blocking discipline, and harder back-traces.

for a senior

Ground the decision in the workload and in which runtime the service's libraries are built on, and keep a CLI synchronous with block_on at the edge when that suffices.

for a principal

Own the standard: one runtime per binary, runtime-agnostic libraries, a blocking-call policy, and a clear trigger for revisiting the decision.

## The question behind the question "Should we go async?" is really "where does this program spend its time, and what will async cost the team?" Rust makes both halves concrete: the benefit is cheap concurrency for waiting, and the cost shows up in types, bounds and libraries. ## When async earns its keep Async helps when **many independent waits overlap**: - a server holding thousands of mostly idle connections; - a service that fans one request out to several backends and combines the answers; - long-lived streams, websockets or subscriptions; - a gateway that is mostly copying bytes between sockets. Here a task costs a small state machine instead of a thread with its own stack, and a single reactor can watch thousands of sockets. The high-traffic HTTP API in this scenario sits squarely in that list. It helps little when work is **sequential or CPU-bound**: a CLI that reads one file, makes three calls in order and prints a report; a batch job that compresses archives; a tool whose time goes into parsing. Threads, or plain sequential code, are simpler there, and parallel CPU work belongs on a thread pool rather than an async executor. ## What async costs in Rust specifically | cost | where it shows up | |---|---| | it spreads | an `async fn` can only be awaited from async code, so callers up the stack become async or call `block_on` | | spawn bounds | work-stealing executors require spawned futures to be `Send + 'static`, which constrains what you hold across `.await` | | blocking discipline | a synchronous call inside a task stalls a worker thread, and rustc does not warn | | cancellation | dropping a future stops it at its last `.await`, which every handler must tolerate | | tooling | deeper generic types, larger compile times and back-traces full of generated state machines | None of these are reasons to avoid async in a server; all of them are reasons not to adopt it in a tool that does not need it. ## Choosing the runtime: coupling decides The `Future` trait lives in `core`, so pure async code is portable. **Sockets and timers are not**: each belongs to one runtime's reactor, and libraries that open connections usually pick a runtime for you. In practice: 1. list the libraries the service needs (HTTP server and client, database driver, message-queue client) and note which runtime each is built on; 2. choose the runtime that most of them share. For network services that is very often Tokio, because much of the async networking ecosystem is built on it; 3. use **smol** when a small dependency tree, fast builds or runtime-agnostic code matter more than ecosystem breadth; it is a thin layer over separate executor, reactor and blocking-pool crates; 4. use **Embassy** for `no_std` microcontroller firmware, where its executor and hardware integration replace an operating system's threads; the embedded material covers it in depth. ## Rules worth setting for the team - **One runtime per binary**, created once, with no second runtime smuggled in by a dependency. - **`block_on` only at synchronous edges** such as `main`, tests and CLI commands. - **Libraries stay runtime-agnostic** where they can: depend on the `Future` and `futures` traits, take I/O as generic parameters, and document a runtime requirement when there is one. - **A blocking-call policy**: blocking or CPU-heavy calls go to a dedicated pool, and known offenders are banned on executor crates through clippy's `disallowed_methods`. - **Revisit with evidence**: if the CLI later needs dozens of concurrent downloads, that is the moment to add `block_on` around a small async core, not before. ## Applying it to the two programs - **HTTP API**: go async, on the runtime its server and database libraries share, with blocking work offloaded and a cancellation-safety rule for handlers. - **CLI tool**: stay synchronous unless a required library is async-only or concurrency is measurably needed; if so, a single `block_on` in `main` keeps the async surface small. There is no universally right answer. A strong answer names the workload, the library constraints and the costs, and reaches a decision the team can revisit. It also says what would change the decision: a sustained rise in concurrent connections pushes a synchronous service toward async, while a service whose async layer only ever wraps blocking calls is paying the costs for none of the benefit.

  • The CLI must use a library whose only API is async. Does that force the whole CLI to become async?
    No. Keep the CLI's code synchronous and call the library through one `block_on` at the point of use, with the runtime the library expects. The async surface stays inside that call, so the rest of the tool keeps plain functions, simple errors and readable back-traces.
  • Why not write the HTTP API with one OS thread per request, since Rust threads are safe?
    Safety is not the constraint; cost is. Each thread reserves a stack and adds scheduling overhead, so tens of thousands of mostly idle connections are cheaper as tasks watched by one reactor. A thread-per-request design can still be right for modest, steady traffic, which is why the decision starts from measured load rather than habit.

  • Claiming async always makes a Rust program faster.
  • Choosing a runtime before checking which runtime the libraries need.
  • Making a sequential CLI async just because one dependency is async.
  • Running CPU-heavy batch work directly on an async executor.
  • Saying Rust async needs Tokio because std cannot express futures.