KatInterview

Design a Decentralized Large-File Distribution Network

System DesignModel answer
MediumModel answerSystem Designpeer-to-peerdistributed-systemsfile-transfer

Last updated

Design a peer-to-peer file sharing system where downloaders pull chunks directly from other peers, covering content identity, discovery, integrity verification, NAT traversal, and swarm health.

Design a Decentralized Large-File Distribution Network

You are tasked with designing a file distribution system where downloaders retrieve files directly from other users who already have copies, rather than pulling everything from a central file server. The goal is to offload bandwidth from any central infrastructure onto the participants themselves.

Walk the interviewer through your design in three parts. Before diving in, state any assumptions you are making and identify questions you would want answered first.

Assumptions to Confirm

  • Peers are regular consumer machines: they appear and disappear without warning, their upload speed is typically much lower than their download speed, and many are behind NAT or corporate firewalls.
  • Central components are allowed for coordination and metadata, but raw file data must never pass through them.
  • Files can reach several gigabytes, so transfers must support resuming after interruptions and downloading from multiple sources simultaneously.
  • Any peer in the network — intentionally or due to bugs — might send corrupted bytes. The system must catch bad data before it becomes part of the final file.
  • Content search, user authentication, and billing are out of scope unless the interviewer says otherwise.

Suggested Clarifying Questions

  • What scale should the design target — number of concurrent peers, number of shared files, and how skewed the popularity distribution is?
  • Is running a coordination service acceptable, or must the system function with zero infrastructure of our own?
  • Are files immutable after publication, or can publishers push updates under the same identifier?
  • Is every file public to anyone who has its identifier, or is access control needed?
  • Which clients matter — desktop only, or also mobile devices with battery and data constraints?

Part 1 — Identifying Content and Finding Peers

Explain how a file gets a canonical identifier, what metadata a downloader needs before the first byte is transferred, and how a peer that wants a file locates others who hold all or part of it. Compare at least two peer discovery approaches and argue for which you would build first.

Your answer should address:

  • A content-derived identifier and the minimum metadata required to start downloading
  • The trade-offs between a centralized tracker and a fully decentralized lookup
  • How peer lists stay accurate as nodes join, leave, and time out

Part 2 — Moving Bytes Efficiently and Correctly

Describe how a downloader actually assembles the file from many peers: how it divides the file into pieces, decides which piece to request and from which peer, verifies each piece without trusting the sender, and picks up where it left off after a crash or restart.

Your answer should address:

  • Splitting the file into chunks and tying each chunk to a trusted hash
  • Piece selection strategy, including how a brand-new peer with nothing gets started and how the final rare pieces are handled
  • Resume behavior: what the client persists to disk and how it recovers after an unexpected shutdown

Part 3 — Keeping the Swarm Healthy at Scale

Explain how the system remains functional given real-world conditions: peers sitting behind NAT, participants who only download and never contribute upload bandwidth, a file that suddenly goes viral, and a file whose last complete holder is about to go offline. Close with what you would instrument and monitor.

Your answer should address:

  • How peers behind NAT or firewalls establish connections, and what fallback exists when direct connection fails
  • A mechanism that discourages free-riding and ensures upload bandwidth is fairly contributed
  • How sudden popularity spikes are absorbed and how availability is maintained as peers churn
  • What observability looks like for a system running on machines the operator does not control

Hints

Hint 1

Start with content identity: a hash of the file's bytes gives you an immutable, self-verifying identifier that does not depend on any server staying alive. Think about what metadata a downloader needs before it can ask anyone for a single byte — file size, chunk boundaries, and per-chunk hashes. That bundle of metadata is itself a small file that can be distributed through any channel.

Hint 2

For peer discovery, the core trade-off is operational simplicity (a tracker you run) versus resilience (a DHT that survives your tracker going down). A tracker is far easier to build and debug; a DHT is more robust but introduces its own consistency and routing complexity. Consider starting with a tracker and adding DHT as a fallback rather than treating them as mutually exclusive.

Hint 3

Piece selection is where naive implementations fail at the edges. A sequential downloader leaves rare final chunks concentrated on few peers; a rarity-first strategy spreads them out. Think also about the brand-new peer problem: it has nothing to offer in an upload-for-upload incentive scheme, so it needs a grace period. And think about what 'resuming' actually requires — what must be written to d…