KatInterview

Byte-Level File Equality and Fleet-Wide Deduplication

System DesignModel answer
HardModel answerSystem Designhashingdeduplicationsystem-design

Last updated

Design a hash-based pipeline to decide if two files hold identical bytes, then scale it to deduplicating content across many machines.

Two files are "the same" only if their byte sequences are the same length and identical in content. File names, permissions, ownership, timestamps, extended attributes, and paths must all be treated as irrelevant. You are asked to design, and justify, a system that answers this question — first for a pair of local files, then as a service that performs deduplication across many directories and many machines.

Your design should cover:

  1. Which hash to trust. Compare cryptographic and non-cryptographic hash families, explain which one is authoritative for declaring equality and which is only a cheap filter, and why.
  2. Staged checks. A pipeline that combines a length check, an optional cheap prefilter digest, an authoritative full-file digest, and optionally a direct byte comparison — and the conditions under which each stage executes.
  3. Collision risk. Quantify the chance of a false "identical" verdict, and explain how the threat model (benign data vs. deliberately crafted inputs) changes your choices.
  4. Metadata as a shortcut, not proof. Which filesystem attributes you inspect to skip work, to recognize two paths that point at the same underlying object, and to decide when a cached digest is still trustworthy.
  5. Hashing files larger than memory. Streaming reads, buffer sizing, parallelizing a single file's hash, files with holes, and protecting against a file that mutates while you read it.
  6. Deduplication at scale. Whole-file vs. fixed-block vs. content-defined boundaries, how to persist and query a digest index, and how a shared service would compute, cache, and serve equality answers.
  7. Where the cost actually is. I/O versus CPU versus memory, and how the design adapts to whichever dominates.

Hints

Hint 1

Metadata can rule files out or let you reuse an old answer, but it can never rule two files in — only bytes (via a strong digest) can do that.

Hint 2

Think about the birthday bound: a 64-bit non-cryptographic hash is fine as a filter at a million files, but the failure probability becomes material at a billion.

Hint 3

A file can change between your first stat and your last read; the fix is to stat the open descriptor before and after, and to key any cached digest on (device, inode, size, mtime, ctime).