Design a Distributed File System
Understanding the Problem
📁 What is a distributed file system? A distributed file system presents one tree of directories and files to thousands of client machines, while storing the bytes across thousands of servers. Programs use it through file operations, such as open, read, write, rename and list, rather than through an object API. Google's GFS and its successor Colossus, Hadoop's HDFS, CephFS, Lustre and cloud services like Amazon EFS are the well-known examples.
Interviewers ask this in two flavors. "Design GFS" or "design HDFS" is about huge files, high aggregate throughput for batch analytics, and a deliberately simplified file model. "Design a POSIX file system for hundreds of petabytes" adds the expectations of a local disk: random writes, atomic renames, permissions, locks and immediate visibility of other clients' changes. They share one architecture. We'll design the GFS/HDFS style first and then a deep dive on what full POSIX semantics cost.
Functional Requirements
Core Requirements
- Users should be able to create, list, rename and delete directories and files in a single hierarchical namespace, with permissions.
- Users should be able to write files (create and write sequentially, or append) and read them, sequentially or at random offsets.
- Many clients should be able to read the same file at once, and append to shared files, with well-defined results.
- Optionally, users should get POSIX semantics: overwriting in place, atomic rename, file locks and consistent metadata across clients.
Below the line (out of scope)
- Snapshots and quotas. Both live in the metadata layer, and we'll mention them only in passing.
- Cross-region replication.
- Running compute on the file system (MapReduce or Spark scheduling), though we'll expose block locations so a scheduler can use them.
Non-Functional Requirements
Core Requirements
- The system should store 300 PB in about 1.5 billion files, most of them large (average 200 MB), and grow without a redesign.
- The system should deliver high aggregate throughput, several TB/s of reads across the cluster, for batch jobs that scan whole data sets.
- The system should handle on the order of 100k-500k metadata operations a second (open, stat, list, create), from jobs with tens of thousands of tasks.
- The system must not lose data when disks, servers or whole racks fail, which at this size happens daily.
- The system should give clear consistency: once a file is closed, every reader sees its full contents; a rename is atomic.
Below the line (out of scope)
- Low-latency small random I/O like a database disk. That's block storage.
- Billions of tiny objects served over HTTP. That's an object store.
Let's size it, because two numbers change the design.
Data nodes. 300 PB with three replicas is 900 PB raw. A storage server with 24 drives of 20 TB holds 480 TB, so at about 70% full we need roughly 2,700 data servers, and more than 60,000 drives. With drive failure rates of a percent or two a year, a drive dies every few hours somewhere in the cluster. Recovery is continuous background work, not an emergency.
Metadata. Split files into 128 MB blocks: a 200 MB file is two blocks, so 1.5 billion files is about 3 billion blocks plus the directories. If the namespace lives in RAM on one server, a common rule of thumb is about 150 bytes per file or block object, so 4.5 billion objects need around 700 GB of memory, on one machine, in one process. That's past what a single metadata server can handle well, and it's the central scaling problem of this design.
Say the metadata math out loud. Bytes are easy to spread over thousands of machines; the namespace is a single tree with atomic operations like rename, and keeping it consistent while spreading it out is where the real design work is.