Backend development

Stage 1 · Lesson 6 of 10

async/await, Tokio and shared state

Your language and how HTTP works

An function in Rust returns a future, and a future does nothing until something awaits it. Tokio is the runtime that runs them. It spreads many small tasks over a few and parks any task that is waiting on the network or the database.

Axum, SQLx and most Rust network are built on Tokio. tokio::join! runs independent calls at the same time, and tokio::spawn starts a background task. Handlers run on many threads at once, so lives in an Arc, with a Mutex if it must change. The compiler checks that sharing with the Send and Sync traits.

Common trapblocking code inside async functions, such as std::thread::sleep, or a large synchronous file read. It stalls every task on that thread, so move it to spawn_blocking.

A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • BookFree
    The Rust Programming Language

    The Rust Project · Beginner

    The official book, free online: read ownership, enums, error handling and traits slowly

  • GuideFree
    Tokio Tutorial

    Tokio · Intermediate

    The official Tokio tutorial: tasks, shared state, channels and the async model Axum runs on

Practise

  • Small exercises that follow the book chapter by chapter, checked in your terminal

  • Your language track: small exercises with free mentor feedback on idiomatic code

Keep handy

  • Runnable examples to look up while you write: traits, generics, errors and Cargo

  • Read the HTTP overview, methods, headers and status code pages until they feel obvious

Also worth your time

Mark this stage as not done?