SkillSprout
Chapter 31 · Lesson 1
Intermediate
5 min read

How a Distributed File System Works (GFS and HDFS)

Overview

A distributed file system (DFS) stores the pieces of a file on many machines but shows users one big file system. You ask for "/logs/2024/clicks.log" and the system finds the pieces for you.

The classic designs are the Google File System (GFS) and the open-source Hadoop Distributed File System (HDFS). Learning how they work shows you a pattern used all over distributed systems: one small coordinator plus many workers.

Detailed Notes

What is a distributed file system

A DFS is a file system whose data lives on many networked machines (called nodes), hidden behind a single set of folders and file names.

There are two kinds of machines:

  • Metadata server (GFS: master, HDFS: NameNode): knows file names, which blocks make up each file, and which nodes hold each block. It never carries the file bytes.
  • Data nodes (GFS: chunk servers, HDFS: DataNodes): store the blocks on their local disks.

A block is a large fixed-size piece of a file (64 MB in GFS, 128 MB by default in HDFS). A replica is one stored copy of a block, and the replication factor (usually 3) is how many copies exist.

The problem it solves

One machine has limits: a finite disk, one network card, and one failure away from losing everything. Say a news app logs 100 GB per day. A nightly job reading 3 TB from a single disk at 200 MB/s takes about 4 hours, and one dead disk loses the history.

Spread the data across 100 machines and each reads only its 30 GB slice in parallel: minutes, not hours. Store each block on three machines and a dead disk loses nothing.

GFS and HDFS were built on some assumptions:

  • Failures are normal. With thousands of cheap machines, something is always broken, so software must heal automatically.
  • Files are huge, so big blocks keep the amount of bookkeeping small.
  • Write once, read many. Files are mostly appended, rarely edited in place.
  • Move compute to the data. Run processing on the machine that holds the block instead of copying terabytes over the network.

How it works

Reading a file

  1. The client asks the metadata server where the file's blocks are.
  2. The metadata server returns the nodes holding each block, nearest first.
  3. The client reads each block directly from a data node, often in parallel. Bulk data never flows through the metadata server, so it does not become a bottleneck.
  4. The client verifies a checksum (see the Checksums chapter). If a block is corrupt, it reads another replica.

Writing a file (the pipeline)

  1. The client asks the metadata server for nodes for the first block, say N1, N2, N3.
  2. The client sends data to N1, N1 forwards it to N2, and N2 forwards it to N3. The client sends the data only once.
  3. Acknowledgements flow back along the chain.
  4. The client repeats for the next block. On close, the metadata server records the file.

Rack-aware placement. A rack is a shelf of servers sharing one switch and power supply. If it fails, every copy inside it disappears. So a common policy puts one replica on the writer's rack and the other two on a different rack. That survives a whole-rack failure without making every write cross the slow links between racks.

Handling failures. Data nodes send heartbeats (see the Heartbeats chapter) plus a list of the blocks they hold.

  • A node goes silent: the metadata server marks it dead and orders other nodes to copy its blocks until every block has three replicas again. No human is involved.
  • A checksum mismatch: read another replica, delete the bad one, make a new good copy.
  • The metadata server dies: it is a single point of failure unless a standby takes over using a shared log of changes (HDFS High Availability).

The metadata server keeps its map in memory for speed and writes a journal (the edit log) to disk so it can be rebuilt after a restart. Its memory limits how many files a cluster can hold, which is why a DFS is poor at millions of tiny files.

A simple example

A 1 GB file with 128 MB blocks and replication factor 3 becomes 8 blocks and 24 block copies. Later a server holding 40 of those copies loses power. Reads continue from other replicas, and within minutes the cluster has re-copied those 40 blocks.

System design example

A company stores a year of click logs (tens of terabytes) on an HDFS-style cluster of 60 commodity nodes. A weekly report that took two days on one file server now runs in under an hour, because every node scans its local blocks at the same time. When two disks die in one month, nothing is lost.

Do not use a DFS for small, random-access records or millisecond reads. It is built for throughput, so use a database or cache for those.

Summary

  • A metadata server maps names to blocks and blocks to nodes; data nodes hold the bytes.
  • Large blocks are replicated (usually 3x) across racks.
  • Writes go through a pipeline of replicas; reads go straight to the nearest replica.
  • Heartbeats detect dead nodes and trigger automatic re-copying.
  • The metadata server is the scaling limit and needs a standby; tiny files are a bad fit.

Architecture Diagram

A file split across a distributed file system
Architecture
Client
Metadata servernames to blocks

Data nodes

Node 1
Node 2
Node 3

Control path asks the metadata server; data path streams from data nodes.

  1. 1The client asks the metadata server where the blocks of a file live.
  2. 2The metadata server returns the nodes holding each block.
  3. 3The client reads blocks directly from those nodes, in parallel.
  4. 4Each block exists on several nodes, so one failure is tolerated.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course