33/37
Part 6 of 6 · Follow-upsLearn and do

Each step toward the disk costs more: write, flush, fsync

What durability costs per call, timed on the laptop's SSD under macOS, median of 5 runs of 2,000 appends of that 118-byte record:

import json
import os
import time

# 118 bytes
record = json.dumps({"op": "put", "key": "k" * 16, "value": "v" * 64}) + "\n"


def per_append(path, flush, fsync, n=2_000):
    with open(path, "a") as f:
        start = time.perf_counter()
        for _ in range(n):
            f.write(record)
            if flush:
                f.flush()
            if fsync:
                os.fsync(f.fileno())
        return (time.perf_counter() - start) / n
Predict

A plain write of the 118-byte record takes 0.21 µs. About how long with flush and os.fsync too?

Pick an answer above to see the rest