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.- Core Responsibilities
- Memory Structure
- Persistent Storage
- High Availability (HA) & QJM
- HDFS Federation
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
.metafile
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
- Standard Policy
- Re-Replication
- Decommissioning
- Balancer
Default 3-Way Replication:
Data Flow: Read Operations
Understanding how data flows through HDFS is crucial for optimization.Read Path
Read Optimizations
Data Locality
Data Locality
Choosing the Closest Replica:
Parallel Reads
Parallel Reads
Reading Multiple Blocks Concurrently:
Short-Circuit Reads
Short-Circuit Reads
Bypassing Network Stack:
Client-Side Caching
Client-Side Caching
Caching Block Locations:
Data Flow: Write Operations
Write operations are more complex than reads due to replication.Write Path
Write Optimizations and Reliability
- Pipelined Replication
- Failure Handling
- Write Consistency
- Append Operations
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:
- Single NameNode Design: Simplifies metadata management but requires HA for production
- Block-Based Storage: 128MB blocks optimize for large files and reduce metadata overhead
- Replication for Reliability: 3x replication default survives single rack failures
- Rack Awareness: Intelligent replica placement balances fault tolerance and network cost
- Pipelined Writes: Streaming data through replica pipeline is 3x faster than sequential
- Data Locality: Moving computation to data is fundamental to Hadoop’s efficiency
- Metadata Separated from Data: NameNode handles metadata, clients stream from DataNodes
- Checksums Everywhere: Data integrity verified at every step—write, read, and background
Interview Questions
Basic: Explain HDFS architecture and its main components
Basic: Explain HDFS architecture and its main components
Expected Answer:HDFS has a master-worker architecture with three main components:
-
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
-
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
-
Clients:
- Contact NameNode for metadata
- Read/write data directly from/to DataNodes
- Maintain consistency with checksums
Intermediate: How does HDFS handle DataNode failures?
Intermediate: How does HDFS handle DataNode failures?
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
- NameNode identifies all blocks on failed DataNode
- Checks which blocks are now under-replicated
- Prioritizes blocks by replication level:
- 0 replicas: Critical priority
- 1 replica: High priority
- 2 replicas: Normal priority
- Selects source (healthy replica) and target (new DataNode)
- Commands source to copy blocks to target
- Verifies checksums during copy
- Updates block location map
- 3x replication by default
- Rack-aware placement (survives rack failures)
- Continuous background verification
- Automatic re-replication maintains factor
Advanced: Explain HDFS's replication pipeline and why it's efficient
Advanced: Explain HDFS's replication pipeline and why it's efficient
Expected Answer:HDFS uses pipelined replication for writes, which is significantly more efficient than sequential replication:Pipeline Mechanism:Instead of:How it Works:
- Client gets pipeline: [DN1, DN2, DN3] from NameNode
- Client establishes connection to DN1
- DN1 connects to DN2, DN2 to DN3
- Client streams 64KB packets to DN1
- DN1 simultaneously:
- Writes to local disk
- Forwards packet to DN2
- DN2 does same (write + forward to DN3)
- ACKs flow backward: DN3→DN2→DN1→Client
- 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
- If DN2 fails, Client removes it from pipeline
- Continues with [DN1, DN3]
- Re-replicates to 3x after write completes
- No data loss, minimal interruption
System Design: How would you optimize HDFS for many small files?
System Design: How would you optimize HDFS for many small files?
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
-
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)
-
Sequence Files:
- Container format: key-value pairs
- Multiple small files → single SequenceFile
- Splittable for MapReduce
- Built-in compression
-
HBase:
- Store small files as rows in HBase
- HBase handles small data efficiently
- Random access support
- Better than HDFS for this use case
-
CombineFileInputFormat:
- MapReduce optimization
- Combines multiple small files into single split
- Reduces number of map tasks
- Better resource utilization
-
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
- 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
Deep Dive: Explain HDFS's rack awareness and replica placement policy
Deep Dive: Explain HDFS's rack awareness and replica placement policy
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)
- 1st replica: DN1 (same node as writer)
- Zero network cost
- Fast write initiation
- 2nd replica: DN4 (different rack, e.g., Rack2)
- Survives rack failure
- One off-rack transfer
- 3rd replica: DN5 (same rack as 2nd, Rack2)
- Rack-local transfer (faster)
- Still survives rack failure
- Survives any single node failure
- Survives any single rack failure
- Does NOT survive 2 rack failures (acceptable trade-off)
- Only 1 out of 3 transfers crosses racks
- 2/3 transfers are rack-local (10x faster)
- Balances reliability with performance
- Multiple racks → better read parallelism
- Readers choose closest replica
- Load distributed across racks
- 4th and beyond: Random, but max 2 per rack
- Diminishing returns on rack diversity
- Focus on load balancing
- Script:
topology.script.file.name - Returns rack ID for each host
- Example:
/rack1,/rack2
- 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!
- 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.