Skip to content

Slow writes despite a fast disk: fsync after every write

Linuxtested on Ubuntu 24.04, Kernel 6.8, ext4, bcc 0.29

A service manages only a few hundred events per second although the CPU does hardly anything and the vendor says the disk can do hundreds of megabytes per second. The team wants to order a faster disk. The measurement says otherwise.

Symptom

journalctl -u protokoll --no-pager -o cat -n 1
20000 Zeilen, 482 Zeilen/s

iostat -sxz 1 2
Device             tps      kB/s    rqm/s   await  areq-sz  aqu-sz  %util
sdb            1555.00   8432.00   554.00    0.50     5.42    0.95  73.10

1,555 requests per second of 5.4 KiB – the disk is busy with tiny jobs.

Diagnosis

Count the service's system calls.

sudo timeout -s INT 25 syscount-bpfcc -p 77008 -d 5
SYSCALL                   COUNT
write                      2476
fsync                      2476

Causes

fsync = writeEvery line is synced

As many fsync as write: the service waits after every line until it is on disk.

fsync 1–4 msfsync latency limits throughput

Every write takes 16 to 63 µs, every fsync 1 to 4 ms. About 2 ms per line – so roughly 500 lines per second, no matter how many megabytes the disk could do.

sudo timeout -s INT 25 ext4dist-bpfcc -p 77008 5 1
operation = fsync
     usecs               : count     distribution
      1024 -> 2047       : 1361     |****************************************|
      2048 -> 4095       : 836      |************************                |

Fix

Sync less often (here every 1,000 lines) if the business requirement allows it.
sudo systemd-run --unit=protokoll -p User=ubuntu python3 /home/ubuntu/tag08/protokoll.py /daten/ereignisse.log 1000
journalctl -u protokoll --no-pager -o cat --since "-12s"
1920000 Zeilen, 126401 Zeilen/s

Don't: Order a faster disk. A disk twice as fast turns 2 ms into maybe 1 ms – batching gives 250 times the throughput.

▶
Break it yourself and find it – free
Linux system performance hands-on, day 8: file systems – cache, writeback, fsync and measuring latency per operation.
Go to day 8 →

FAQ

May I simply call fsync less often?

That is a business decision – how many events may be lost in a power failure? Technically one fsync per second or per block is often enough.

Does batching put more load on the disk?

No, less. In the course it dropped from 1,555 to 382 requests per second and from 73 to 26 percent utilization.

Related error patterns

More