KatInterview

Design a Self-Describing Columnar File Format for Time-Series Data

System DesignModel answer
HardModel answerSystem Designfile-formatscolumnar-storageserialization

Last updated

Design the binary layout, writer/reader interfaces, and operational invariants for an append-friendly columnar file format suited to high-throughput time-series workloads.

Context

A quantitative research platform stores billions of sensor or market events per day. Each event is a row in a wide table — potentially 20–40 typed columns — but most queries touch only two or three of them (e.g., event_time and a single measurement field). Row-oriented formats like CSV or newline-delimited JSON force the reader to deserialize every field just to extract the ones it needs, wasting I/O bandwidth at scale.

Your task is to design a compact, self-describing, append-friendly columnar file format — a simplified version of something like Apache Parquet — along with clean read and write interfaces.

What you need to define

1. Binary layout specification

Define the exact on-disk structure, including:

  • File header (magic bytes, version, flags)
  • How rows are partitioned into row groups (horizontal slices of, say, 64 K–1 M rows)
  • How each row group stores per-column column chunks made up of one or more data pages (and optionally a dictionary page)
  • The fixed-size page header fields (value counts, null counts, encoding, codec, lengths, checksum)
  • A self-describing footer written at the end of the file that contains the schema, per-chunk offset/length/statistics, and any user metadata
  • A fixed-size tail (footer length + CRC + magic) so a reader can locate the footer with two seeks

2. Writer and reader class interfaces

Write Python or C++ pseudo-code for:

  • ColumnarFileWriter — buffers column data in memory, flushes complete row groups to the sink, and writes the footer on close()
  • ColumnarFileReader — reads and validates the footer, exposes column projection (reading only requested columns) and row-group pruning via per-chunk min/max statistics

3. Invariants and edge cases

Address:

  • How partial writes and file truncation are detected and rejected
  • All-null columns (missing min/max statistics, predicate behaviour)
  • Variable row-group sizes (the last group is almost always smaller)
  • CRC validation at both the page and footer level

4. Testing strategy

Describe how you would verify:

  • Round-trip correctness for all supported types and encodings
  • That projection genuinely avoids touching un-requested column data
  • Encoding regressions (e.g., delta encoding producing wrong values after a codec change)
  • Corruption detection at the page and footer level

Constraints and assumptions

  • Supported types for this exercise: INT32, INT64, FLOAT64; all fields may be nullable
  • Integer byte order: little-endian throughout
  • Files are write-once / read-many; no in-place updates
  • The writer must stream in a single forward pass — no seeking back to patch the header
  • Compression is pluggable: at minimum RAW, LZ4, and a delta + bit-packing scheme for monotonic integer columns (e.g., nanosecond timestamps)

Expected output

A structured written answer covering all four areas above. Include concrete byte-level field tables where they clarify the layout, and commented pseudo-code for the interfaces. Call out trade-offs explicitly — do not just describe one option.

Hints

Hint 1

Start from the reader's perspective: what is the minimum information a reader needs to locate one column chunk inside one row group using a single contiguous read? Work backwards to determine what the footer must contain and where it must live on disk.

Hint 2

A streaming writer cannot seek back to patch a header once rows have been written. This is why the footer goes at the very end. Think about how a fixed-size tail (footer_len + CRC + magic) lets the reader find the footer with exactly two seeks regardless of file size.

Hint 3

Delta encoding works well for monotonically increasing timestamps because consecutive differences are small and can be bit-packed into far fewer bits than the raw 8-byte values. Consider what structural invariant (monotonicity) must hold for this to be safe, and how you would signal encoding choice per column chunk in the metadata.