Basic

Why async exists at all

A program that waits for a network call is a program that is holding a thread for no reason. Async exists to stop holding, and the reason it stops holding is a number you can read.

What you will see

One line. Naming the held resource is what makes the fix a decision.

About 1 min of reading

Try It

Count waiting versus working

1

Write the wait profile

printf 'wait 90\nwork 10\n' > /tmp/p.txt; awk '{s+=$2} END {print s}' /tmp/p.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
100. The total says nothing, the split says the thread is idle nine tenths of the time.
2

Compute the throughput ceiling

awk 'BEGIN {printf "%.1f\n", 8/90}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
0.1. Eight threads give one task per unit, which is the ceiling blocking gives.
3

Name the resource held

printf 'held=thread reason=socket-wait\n' > /tmp/h.txt; cat /tmp/h.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. Naming the held resource is what makes the fix a decision.

Data Flow

A waiting program to a paused one
1 The program reaches a network call
↓
2 The call is awaited instead of blocked
↓
3 The thread is released to the pool
↓
4 Another task is picked up in its place
Why this works
Why async is measured, not admired

The measure is tasks per second at a fixed thread count. A design that is not measured is a design that may be slower than the one it replaced.

The one symptom async fixes

Latency that is paid twice. A request that blocks while another request waits is the same latency counted twice.

Examples

Concurrency is not parallelism

Concurrency is many tasks in one place at a time. Parallelism is many places at once. Async gives the first, and the first is what a network needs.

The cost of waiting is a resource

A thread waiting on a socket is a thread that cannot be used. The number of waiting threads is the ceiling on everything else.

Why the keyword is not the point

The point is that the function can be paused in the middle. The keyword is only how the pause is written.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
A future is a recipe, not a result
A future is a recipe, not a result →