# How Much to Buffer?

Let's do some maths to determine what should be the buffer size to ensure the best performance.

Now arises the following question: how many updates should LFS buffer before writing to disk? The answer, of course, depends on the disk itself, specifically how high the positioning overhead is in comparison to the transfer rate; see the FFS chapter for a similar analysis.

For example, assume that positioning (i.e., rotation and seek overheads) before each write takes roughly $T_{position}$ seconds. Assume further that the disk transfer rate is $R_{peak}\ MB/s$. How much should LFS buffer before writing when running on such a disk?

The way to think about this is that every time you write, you pay a fixed overhead of the positioning cost. Thus, how much do you have to write in order to **amortize** that cost? The more you write, the better (obviously), and the closer you get to achieving peak bandwidth.

To obtain a concrete answer, let’s assume we are writing out $D$ MB. The time to write out this chunk of data ($T_{write}$) is the positioning time $T_{position}$ plus the time to transfer $D$($\frac {D} {R_{peak}}$), or:

$T_{write} = T_{position} + \frac {D} {R_{peak}}$

And thus the effective *rate* of writing ($R_{effective}$), which is just the amount of data written divided by the total time to write it, is:

$R_{effective} = \frac {D} {T_{write}} = \frac {D} {T_{position} + \frac {D} {R_{peak}} }$

What we’re interested in is getting the effective rate ($R_{effective}$) close to the peak rate. Specifically, we want the effective rate to be some fraction F of the peak rate, where $0<F <1$ (a typical $F$ might be 0.9, or 90% of the peak rate). In mathematical form, this means we want $R_{effective}=F \times R_{peak}$

At this point, we can solve for $D$:

$R_{effective} = \frac {D} {T_{position} + \frac {D} {R_{peak}} } = F \times R_{peak}$

$D = F \times R_{peak} \times (T_{position} + \frac {D} {R_{peak}})$

$D = (F \times R_{peak} \times T_{position} ) + (F \times R_{peak} \times \frac {D} {R_{peak}})$

$D = \frac {F} {1-F} \times R_{peak} \times T_{position}$

Let’s do an example, with a disk with a positioning time of 10 milliseconds and peak transfer rate of 100 MB/s; assume we want an effective bandwidth of 90% of peak ($F$ = 0.9). In this case, $D$ would be:

$D=\frac {0.9} {0.1} \times 100 MB/s \times 0.01 \ seconds = 9 \ MB$

Try some different values to see how much we need to buffer in order to approach peak bandwidth:

Get hands-on with 1200+ tech skills courses.