Wait, or carry on

Think of it when a screen freezes while it waits, a page makes its requests one after another, or a slow job sits between a user and the page they want.

In everyday life

At a café you order, take a buzzer, and sit down to read until it buzzes. Standing at the counter is synchronous. Ordering three coffees one at a time, each after the last arrives, is slow either way.

One case

A music app’s home screen fetches 10 playlist covers, one request after another. Each answer takes 0.3 seconds, almost all of it spent waiting on the network. The screen still scrolls, but the last cover appears after 3 seconds. Started at the same time, the 10 requests are all back in about 0.3 seconds.

Asking one after another took 10 times longer
Two ways, cell by cell1 cell = 0.3 seconds
Covers ready, if asked together · the better way
010101010101010101010
Covers shown, asked one after another
012345678910
Seconds to load, one after another
3.0
Seconds, all together
0.3
The server was never busy. The app waited for each cover before it asked for the next.

Every case

Most of a program’s time goes to waiting: for a disk, a network, another server. Synchronous code blocks: it does nothing else until the answer comes, and on the main threadThe part of an app that draws the screen and reacts to taps. If it stops to wait, the screen freezes. that freezes the screen. Asynchronous code starts the request and carries on, but it still loses time if it asks for each answer only after the last one arrives. Start independent requests together; the cost is that answers arrive in any order, and some never arrive, so the code must handle both.

The word

Engineers call this

sync vs asyncSynchronous: ask, and wait for the answer before you do anything else. Asynchronous: ask, carry on with other work, and handle the answer when it arrives.

Wait only when the next step needs the answer. Start independent requests together, and never make the user wait for a slow job they do not need.

Where this idea leads

Skills that encode it

  • swarm pstackSpawns all N workers in one message, then drains their results: independent work starts together.
  • principle-never-block-on-the-human pstack“The human supervises asynchronously”: the agent carries on with reversible work instead of waiting for an answer.