CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 4 of 7 · 2 min read

Where the time goes

Think of a lift that takes the same 30 seconds for any trip. Sending one person at a time takes 30 seconds each, and sending 10 people together takes 30 seconds in all. The analogy stops in one place: a lift has a capacity limit, while a write has no such limit until the device's data rate is reached.

In plain words: The time is almost entirely the cost of waiting for the device to confirm. Compare the first row with the last. Writing the same 5,000 records without waiting takes 0.034 s, which is 6.8 microseconds each. With a wait after each record it takes 0.689 s, which is 138 microseconds each. The difference of 0.655 s over 5,000 calls is 131 microseconds for each fsync. That fixed cost is paid once for each call, and it is the same whether the call covers one record or one hundred.

A worked example. The cost of one fsync is about 131 microseconds in the first row. In the third row there are 50 calls, so the waiting is 50 x 131 = 6.6 ms, and the writing is 34 ms, so the sum is about 40 ms, and the measured time is 59 ms. The model is close enough. The time per record falls from 138 microseconds to 11.8, a speed-up of 0.689 / 0.059 = 11.7.

The key lines. The fixed cost does not depend on the size of the write. The design must therefore reduce the number of fsync calls and keep the amount of data the same. This is the same lesson as page 1: pay the fixed cost once for many bytes.

Common mistake

Do not remove the fsync to make the program fast without thinking. The last row is fast because nothing is safe until the end. A crash in the middle of such a run loses everything that was still in the page cache.

Check yourself: 1. What is the fixed cost of one fsync here? 2. Why does the time of a run follow the number of fsync calls? 3. Which row is the fastest, and what does it give up?