Skip to main content

Chapter 2: HDFS Architecture

The Hadoop Distributed File System (HDFS) is the storage foundation of the Hadoop ecosystem. Inspired by Google’s GFS, HDFS implements a distributed file system designed to run on commodity hardware and provide high-throughput access to application data.
Chapter Goals:
  • Understand HDFS architecture and component roles
  • Learn how NameNode and DataNodes interact
  • Master block replication and placement strategies
  • Explore read and write data flows
  • Compare HDFS with GFS and identify improvements

HDFS Architecture Overview

System Components

NameNode: The Master

The NameNode is the central authority in HDFS, managing the file system namespace and controlling access to files by clients.
What the NameNode Does:

DataNodes: The Workers

DataNodes store the actual data blocks and serve read/write requests from clients.

Storage Model

How Blocks Are Stored:
  • Each block: up to 128MB (default)
  • Stored as regular Linux files
  • Path: /data/hadoop/dfs/data/current/
  • File naming: blk_<block_id>
  • Separate metadata file: blk_<block_id>.meta
  • Contains checksums for integrity
  • Multiple blocks per disk

Responsibilities

What DataNodes Do:
  • Store and retrieve blocks
  • Serve read requests from clients
  • Execute write operations
  • Replicate blocks to other DataNodes
  • Send heartbeats to NameNode
  • Report blocks to NameNode
  • Delete blocks when instructed

Block Verification

Data Integrity:
  • Checksum per 512 bytes
  • Verify on read operations
  • Periodic background scanning
  • Report corrupt blocks to NameNode
  • Automatic re-replication from good replicas
  • Checksum stored in .meta file

Heartbeat Protocol

Communication with NameNode:
  • Heartbeat every 3 seconds
  • Contains capacity info
  • Receives commands from NameNode
  • Block reports every 6 hours
  • 10 minutes without heartbeat = dead
  • NameNode initiates re-replication

Block Replication and Placement

Replication Strategy

HDFS replicates blocks to ensure fault tolerance and enable data locality for MapReduce.

Replica Placement Details

Default 3-Way Replication:

Data Flow: Read Operations

Understanding how data flows through HDFS is crucial for optimization.

Read Path

Read Optimizations

Choosing the Closest Replica:
Reading Multiple Blocks Concurrently:
Bypassing Network Stack:
Caching Block Locations:

Data Flow: Write Operations

Write operations are more complex than reads due to replication.

Write Path

Write Optimizations and Reliability

Why Pipeline Instead of Sequential?:

HDFS vs GFS: Key Differences

Block Size

128MB vs 64MB:
  • HDFS default: 128MB
  • GFS: 64MB
  • HDFS evolved with hardware
  • Reduces metadata overhead
  • Better for larger files
  • Configurable per file

Secondary NameNode

Checkpointing:
  • HDFS: Secondary NameNode
  • Not a hot standby (misleading name!)
  • Creates fsimage checkpoints
  • Reduces edit log size
  • Later: Standby NameNode (HA)

File Permissions

POSIX-like Security:
  • HDFS: Full permission model
  • User, group, others
  • Read, write, execute
  • ACLs in later versions
  • GFS: Simpler model

Quotas

Resource Management:
  • HDFS: Directory quotas
  • Space quotas per directory
  • Name quotas (file count)
  • Enables multi-tenancy
  • GFS: No native quotas

Key Takeaways

Remember These Core Insights:
  1. Single NameNode Design: Simplifies metadata management but requires HA for production
  2. Block-Based Storage: 128MB blocks optimize for large files and reduce metadata overhead
  3. Replication for Reliability: 3x replication default survives single rack failures
  4. Rack Awareness: Intelligent replica placement balances fault tolerance and network cost
  5. Pipelined Writes: Streaming data through replica pipeline is 3x faster than sequential
  6. Data Locality: Moving computation to data is fundamental to Hadoop’s efficiency
  7. Metadata Separated from Data: NameNode handles metadata, clients stream from DataNodes
  8. Checksums Everywhere: Data integrity verified at every step—write, read, and background

Interview Questions

Expected Answer:HDFS has a master-worker architecture with three main components:
  1. NameNode (Master):
    • Manages file system namespace (directories, files)
    • Stores metadata (file-to-block mapping)
    • Coordinates file operations
    • Monitors DataNode health
    • All metadata in RAM for fast access
  2. DataNodes (Workers):
    • Store actual data blocks (128MB each)
    • Serve read/write requests from clients
    • Send heartbeats to NameNode (every 3s)
    • Report blocks they store (every 6h)
    • Execute replication commands
  3. Clients:
    • Contact NameNode for metadata
    • Read/write data directly from/to DataNodes
    • Maintain consistency with checksums
Key principle: Metadata and data flows are separated. NameNode only handles metadata; clients talk to DataNodes for actual data.
Expected Answer:HDFS handles DataNode failures through multiple mechanisms:Detection:
  • DataNodes send heartbeats every 3 seconds
  • If NameNode doesn’t receive heartbeat for 10 minutes, marks DataNode as dead
  • Immediate action triggered
Recovery:
  1. NameNode identifies all blocks on failed DataNode
  2. Checks which blocks are now under-replicated
  3. Prioritizes blocks by replication level:
    • 0 replicas: Critical priority
    • 1 replica: High priority
    • 2 replicas: Normal priority
  4. Selects source (healthy replica) and target (new DataNode)
  5. Commands source to copy blocks to target
  6. Verifies checksums during copy
  7. Updates block location map
Prevention:
  • 3x replication by default
  • Rack-aware placement (survives rack failures)
  • Continuous background verification
  • Automatic re-replication maintains factor
Time to Recovery: Minutes to hours depending on data volume, but cluster remains operational during recovery.
Expected Answer:HDFS uses pipelined replication for writes, which is significantly more efficient than sequential replication:Pipeline Mechanism:
Instead of:
How it Works:
  1. Client gets pipeline: [DN1, DN2, DN3] from NameNode
  2. Client establishes connection to DN1
  3. DN1 connects to DN2, DN2 to DN3
  4. Client streams 64KB packets to DN1
  5. DN1 simultaneously:
    • Writes to local disk
    • Forwards packet to DN2
  6. DN2 does same (write + forward to DN3)
  7. ACKs flow backward: DN3→DN2→DN1→Client
Efficiency Benefits:
  • 3x faster: All transfers happen in parallel vs sequential
  • Network utilization: All links active simultaneously
  • Latency: ~1 second for 128MB vs ~3 seconds sequential
  • Scalability: Time independent of replication factor
Failure Handling:
  • If DN2 fails, Client removes it from pipeline
  • Continues with [DN1, DN3]
  • Re-replicates to 3x after write completes
  • No data loss, minimal interruption
Expected Answer:Small files are HDFS’s Achilles’ heel. Each file consumes ~150 bytes of NameNode RAM regardless of file size. Solutions:Problem Quantification:
  • 1 million files = 150MB NameNode RAM
  • 100 million files = 15GB RAM
  • 1 billion files = 150GB RAM + slow operations
  • Each file requires NameNode RPC = overhead
Solutions:
  1. HAR Files (Hadoop Archives):
    • Combine many small files into larger archive
    • Like tar for HDFS
    • Reduces NameNode metadata
    • Trade-off: Slower access (need to unpack)
  2. Sequence Files:
    • Container format: key-value pairs
    • Multiple small files → single SequenceFile
    • Splittable for MapReduce
    • Built-in compression
  3. HBase:
    • Store small files as rows in HBase
    • HBase handles small data efficiently
    • Random access support
    • Better than HDFS for this use case
  4. CombineFileInputFormat:
    • MapReduce optimization
    • Combines multiple small files into single split
    • Reduces number of map tasks
    • Better resource utilization
  5. HDFS Federation (Hadoop 3.x):
    • Multiple NameNodes, each managing subset
    • Horizontal scaling of namespace
    • Allows more total files
    • But doesn’t solve per-file overhead
Real-World Approach: Most companies use a combination:
  • Archive old small files into SequenceFiles
  • Use HBase for active small file workloads
  • Educate users to avoid small files
  • Set up quotas and monitoring
  • Consider cloud object storage (S3) for small files
Modern Alternative: Many modern systems (Snowflake, Delta Lake) use cloud object storage (S3, GCS) which handles small files better than HDFS.
Expected Answer:HDFS models the network as a tree and places replicas strategically:Network Topology:
Distance Calculation:
  • Same node: 0
  • Same rack: 2 (node→rack→node)
  • Different rack: 4 (node→rack→cluster→rack→node)
Default Placement (3 replicas):If writer on DN1 (Rack1):
  1. 1st replica: DN1 (same node as writer)
    • Zero network cost
    • Fast write initiation
  2. 2nd replica: DN4 (different rack, e.g., Rack2)
    • Survives rack failure
    • One off-rack transfer
  3. 3rd replica: DN5 (same rack as 2nd, Rack2)
    • Rack-local transfer (faster)
    • Still survives rack failure
Why This Policy?Fault Tolerance:
  • Survives any single node failure
  • Survives any single rack failure
  • Does NOT survive 2 rack failures (acceptable trade-off)
Network Cost:
  • Only 1 out of 3 transfers crosses racks
  • 2/3 transfers are rack-local (10x faster)
  • Balances reliability with performance
Read Optimization:
  • Multiple racks → better read parallelism
  • Readers choose closest replica
  • Load distributed across racks
For Replication Factor > 3:
  • 4th and beyond: Random, but max 2 per rack
  • Diminishing returns on rack diversity
  • Focus on load balancing
Configuration: Rack topology specified in:
  • Script: topology.script.file.name
  • Returns rack ID for each host
  • Example: /rack1, /rack2
Impact of Wrong Topology:
  • If NameNode doesn’t know racks, treats all as same rack
  • Loses fault tolerance benefit
  • Survives only node failures, not rack failures
  • Critical to configure correctly!
Real-World Considerations:
  • Cloud environments: Availability zones = racks
  • On-premises: Physical rack layout
  • Network switches as failure domains
  • Balance across power circuits

Up Next

In Chapter 3: MapReduce Framework, we’ll explore:
  • The MapReduce programming model in depth
  • Job execution flow and task lifecycle
  • Shuffle and sort mechanisms
  • Optimization techniques for MapReduce jobs
  • How MapReduce leverages HDFS data locality
We’ve mastered HDFS storage. Next, we’ll learn how to process that data efficiently with MapReduce.