Lesson 1 · Unit 04
Waiting on I/O
Count the trips, batch them, and overlap the waits
What you practise
- Orders of magnitude from memory to disk to network, one query per item against one query for all, batching, doing waits at the same time
Try a question from this lesson
Pick one
A handler does 1,000 additions in memory and makes one request to a server on another continent. Where does nearly all of its time go?
Correct answer
The request: it outweighs all the additions by far
Why
A far network round trip is roughly a million times slower than touching memory, so a thousand additions are a rounding error next to it. Counting operations is not enough: you have to ask what kind each one is.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Three speeds
Rough ratios worth carrying in your head:
- a read from memory: 1
- a read from a fast disk: about 1,000
- a round trip to a far-away server: about 1,000,000
The exact figures change with hardware and distance. The gaps do not.
A program that is waiting on disk or network is called I/O-bound. Making its code faster changes almost nothing. Making fewer trips, or overlapping them, changes everything.
Concept
Overlapping waits
While a program waits for one answer, it can already have asked for another.
If two calls do not depend on each other, start both and wait for both. The total is the longest wait, not the sum.
A call that needs another call's result cannot overlap with it: those two form a chain, and a chain adds up.
So the time for a page is its longest chain. The servers still do every call's work; you have only stopped waiting in single file.
10 more exercises in the app
- 4× type the answer
- 3× multiple choice
- 1× order the lines
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.