Design a Durable Local Event Writer
Design a thread-safe single-process writer that durably appends small opaque events to one local file under heavy concurrency.
Problem statement
Design a single-server DataWriter(file_path) shared by thousands of threads in one application. Its push(data) method accepts an opaque payload of at most 1 KB.
For this exercise, assume push returns success only after the complete framed record is durably flushed to local disk; every acknowledged record must be recovered after a process crash or ordinary reboot. The writer assigns a monotonically increasing sequence to accepted calls.
Concurrent calls may be ordered arbitrarily, but a call that completes before another begins must appear first. Records include sequence, length, and checksum; restart may discard an incomplete or invalid trailing record but must fail closed on corruption inside the committed prefix. One process owns one file path.
Multi-process appends, network replication, remote storage, rotation, retention deletion, indexing, querying, and payload interpretation are outside scope.
What to build
- For this exercise, constructing DataWriter(file_path) exclusively opens a file and recovers its last valid acknowledged sequence before accepting new pushes.
- For this exercise, any calling thread can push an opaque payload of at most 1 KB and receive a success only after its framed record is durably included in the ordered file prefix.
- For this exercise, the writer applies bounded backpressure and returns explicit overload, closed, invalid-input, or I/O failure outcomes without silently dropping or acknowledging data.
At scale
- Source fact: thousands of threads in one application share one writer and append opaque payloads no larger than 1 KB to local disk.
- For this exercise, assume up to 5,000 calling threads and peaks of 100,000 pushes per second, with an average payload of 512 bytes.
- For this exercise, target 20 milliseconds p95 for healthy push completion while capping pending in-memory data at 64 MiB.
- For this exercise, assume one local disk and one file owner; acknowledged records survive a process crash and ordinary reboot, while unacknowledged tail work may be absent after recovery.