KatInterview

Design a Durable Multi-Threaded Append-Only Log Writer

System DesignModel answer
MediumModel answerSystem Designconcurrencydurabilityfile-io

Last updated

Design a thread-safe writer that lets thousands of threads durably append sub-1KB opaque records to one local file, with crash-safe framing and safe error handling.

You must design a small class, shared by a single-process application on one machine, that many worker threads use to append records to a file on the local disk.

class DataWriter:
    def __init__(self, file_path_on_disk: str): ...
    def push(self, data: bytes) -> None: ...

Each data argument is a bytes blob of at most 1 KB, and its contents are opaque — you never need to parse or transform them. Thousands of application threads call push concurrently and continuously.

A closely related variant of this problem states the core tension outright: every write to the single file must be synchronous (the caller only proceeds once the write has happened), and at the same time many threads are writing at once.

The interviewer will say: "Go ahead and start designing. Ask me anything you need before you begin."

What you are expected to deliver:

  • Defend a precise durability contract for push — when it returns, what is guaranteed to have survived a power loss?
  • Show a concurrency design that never loses, duplicates, or interleaves records, and that does not require one disk sync per call.
  • Specify an on-disk record layout that lets a later reader split the file back into the original records and detect a torn or corrupt tail after a crash.
  • Analyze the latency/throughput trade-off of batching and describe backpressure when the disk can't keep up.
  • Explain error handling when a write or sync fails, plus clean shutdown.
  • Provide working code or precise pseudocode for push and the flush path.

Assume (unless you argue otherwise in your clarifying questions): records are appended in some order across threads but each thread's own records stay in order; the file must be readable and split into records later; the class does not need rotation or truncation but does need a close.