KatInterview

Design a Crash-Safe High-Throughput Append Log

System DesignModel answer
HardModel answerSystem Designwrite-ahead logconcurrencydurability

Last updated

Design an embedded append-only log writer that handles thousands of concurrent producers, batches writes efficiently, and recovers cleanly from crashes.

Context

You are designing a low-level, in-process log writer for an embedded storage engine. Thousands of worker threads simultaneously produce variable-length byte payloads that must be durably recorded to a single append-only file on local NVMe storage.

This is not a distributed log service. Everything runs in one process, on one machine, writing to one file. The challenge is squeezing maximum throughput and minimum latency out of that setup while making strong correctness guarantees.

Functional Requirements

  • Write API: Expose an append(data: bytes) -> WriteReceipt call — synchronous or asynchronous — that returns a stable identifier for the record's position in the log (an offset or sequence number).
  • Ordering & Atomicity: Writes must appear in a strict total order. A reader must never see a partial record or two records interleaved. Each write is all-or-nothing.
  • Crash Recovery: After an unclean shutdown, a recovery pass must scan the file, find the last valid record, discard any incomplete write at the tail, and leave the log in a state ready for new appends.
  • Graceful Shutdown: Draining all queued records, issuing a final sync to disk, and closing file descriptors cleanly.

Non-Functional Requirements

  • Concurrency: Up to 5,000 threads calling append at the same time.
  • Throughput: Sustain at least 50,000 writes/sec and 200 MB/sec on a modern local SSD.
  • Durability Latency: In group-commit mode, every write must be persisted within 5 ms or when a batch reaches 1 MB in size, whichever comes first.
  • Memory Cap: No more than 64 MB of data may be buffered in flight at any time. When that limit is reached, producers must be slowed down in a predictable way.

What Your Answer Should Cover

  1. Thread coordination: How do 5,000 producers hand off data to the writer without creating contention hot spots?
  2. On-disk record format: What does each record's binary layout look like, and what fields make recovery reliable?
  3. Batching and I/O mechanics: How does group commit work? What OS primitives help?
  4. Backpressure and error handling: What happens when the buffer fills up, the disk is slow, or a write fails?
  5. Recovery algorithm: Step by step, how does the process find and discard torn writes at startup?
  6. Observability: Which metrics matter for detecting problems early?

Hints

Hint 1

Start with the math: average record size, batch composition, and sync budget. Those numbers justify almost every design decision that follows.

Hint 2

Think carefully about who owns the file descriptor. Limiting I/O to a single writer thread eliminates an entire class of ordering bugs and removes the need for locks around syscalls.

Hint 3

Recovery needs to distinguish a torn write at the tail (safe to truncate) from corruption in the middle of already-acknowledged data (not safe to silently discard). How does your record format support that distinction?