HDFS (Hadoop Distributed File System)
HDFS (Hadoop Distributed File System) - Cheat Sheet
🎯 Core Design Goals
1. Reliability - Overcome server failures
- Data always available through replication
- Analogy: Like keeping photocopies of important documents in different safes
2. High Performance - Fast data access
- Parallel reading through data partitioning
- Analogy: Splitting a book into chapters so multiple people can read simultaneously
3. Network Awareness - Data locality
- Keep data close to applications
- Minimize network transfer
- Analogy: Library system - get books from the closest branch
🏗️ HDFS Architecture
NameNode (Master) - ONE per cluster
The Brain of HDFS
Responsibilities:
- Manages all metadata (file → blocks → DataNodes mapping)
- Tracks DataNode status via heartbeats
- Orchestrates read/write operations
- Detects failures → triggers replication
- Rebalances cluster
⚠️ Critical: Single Point of Failure (SPOF) - use HA configuration in production
Analogy: Librarian who knows where every book is stored and directs readers
DataNode (Workers) - One per server
The Storage Units
Responsibilities:
- Stores actual data blocks
- Reports health via heartbeats (every 3 seconds)
- Ensures data integrity (checksums)
- Serves read/write requests
Timeout: 10 minutes of missed heartbeats → marked as dead
📦 Block Structure
| Aspect | Value | Why? |
|---|---|---|
| Default Size | 128 MB | Reduce metadata overhead |
| Production | 256 MB | Better sequential read performance |
| Traditional FS | 4-64 KB | 1000-2000x smaller! |
Why So Large?
- Fewer blocks to track → less NameNode memory
- Optimized for sequential reads (MapReduce)
- Reduces seek time overhead
🎨 Three Design Principles
1. Data Partitioning (Performance)
Split data into chunks across nodes
Benefits:
- High throughput
- Parallel reading
- Multiple users simultaneously
Formula: More partitions = More parallelism
2. Replication (Reliability)
Create multiple copies of each block
Default: N = 3 replicas
Storage Cost:
Placement Strategy (Rack-Aware):
- 2 copies in same rack (fast local replication)
- 1 copy in different rack (survives rack failure)
Rack 1: [Block B, Block B'] ← 2 copies
Rack 2: [Block B''] ← 1 copy
Analogy: Two backups at home + one in bank vault
3. Network Awareness (Locality)
Read Priority:
- Same node (local disk) ← Fastest
- Same rack
- Different rack
- Different datacenter
Analogy: Check your own bookshelf → roommate's → library → interlibrary loan
✍️ Write Operations
Write Process (3 Steps)
Step 1: Client → NameNode
- Request: "I want to write file X"
- NameNode checks permissions
- Returns: Pipeline of DataNodes
[DN1 → DN2 → DN3]
Step 2: Pipelined Write
Client → DN1 → DN2 → DN3
←─── ACK ←─── ACK ←─── ACK
Step 3: Acknowledgment
- Each DataNode confirms write
- Client only writes once (DN1 forwards to others)
⭐ Key Insight: Pipelined writes reduce client network cost from 3x to 1x
Update/Edit Operations
Problem: How to modify existing data?
Solution: Delete + Write New
1. Mark old blocks for deletion [B, B', B'']
2. Write new blocks [B_new, B_new', B_new'']
3. Update NameNode metadata
4. Garbage collect old blocks
Why no in-place updates?
- Simpler consistency model
- Maintains immutability
- Easier failure recovery
Real-world: Even Facebook/Twitter "edits" create new versions
📖 Read Operations
Read Process
Flow:
- Client → NameNode: "Where is file X?"
- NameNode → Client: List of DataNodes with blocks
- Client → DataNode: Read from closest replica
Network Awareness: Always pick closest copy to minimize latency
Reliability: If DataNode fails → automatically try another replica
🔥 Failure Handling
Detection & Recovery
Detection: Heartbeat monitoring (3-second intervals)
Timeline:
T=0: DataNode fails
T=10min: NameNode marks it dead
T=10min+: Re-replication begins
T=15-30min: Fully recovered
Example:
Before: DN1[B], DN2[B'], DN3[B''] ← DN2 fails
After: DN1[B], DN4[B'_new], DN3[B'']
Can You Lose Data?
Safe Scenarios:
- ✅ 1 replica fails → 2 remaining
- ✅ 2 replicas fail → 1 remaining
Danger:
- ❌ All 3 replicas fail simultaneously
Probability:
Where = individual server failure rate (1%)
Correlated Failures (Dangerous!)
Scenarios:
- Rack power supply fails → multiple servers down
- Network switch fails → entire rack isolated
- Software bug → same failure everywhere
- Natural disaster → entire datacenter
Mitigation:
- Multi-rack placement ✓
- Multi-datacenter replication
- Erasure coding (more efficient than 3x replication)
Real-world: Amazon S3 promises 99.999999999% (11 nines) durability
🔬 Research Challenges
1. Popularity-Based Optimization
Problem: Not all files are equally popular
- "Basketball" vs "hockey" searches
- Viral videos vs obscure content
Ideal Solution (NOT currently in HDFS):
- High popularity → More replicas (50 readers → 50 replicas)
- Low popularity → Fewer replicas (3 readers → 3 replicas)
What HDFS Actually Does:
- Load Balancing - Read from closest replica
- Data Locality - Move computation to data (MapReduce/Spark)
- OS Caching - Hot blocks in RAM (page cache)
Real-world:
- Facebook: Old photos (>1 year) → cold storage, reduced redundancy
- Netflix: Popular shows cached in memory (Alluxio)
2. I/O & Network Contention
Problem 1: Server Overload
Server A (Closest): [🔥🔥🔥] ← 10 requests (overloaded)
Server B (Medium): [___] ← Idle
Server C (Farthest): [___] ← Idle
Current Limitation: HDFS picks "closest" without checking load
Solutions:
- Load-aware scheduling
- Rate limiting per server
- Redistribute blocks
Problem 2: Network Bottlenecks
Scenario: Many tasks in Rack1 need data from Rack2
- Rack uplink becomes bottleneck
- Bandwidth shared among transfers
Current HDFS: Doesn't consider network congestion
Potential Solutions:
- Network-aware scheduling
- Traffic engineering
- Adaptive routing
Real-world: AWS uses VPC Traffic Mirroring to route around congestion
🆚 GFS vs HDFS
| Feature | GFS (Google) | HDFS (Hadoop) |
|---|---|---|
| Master | Master | NameNode |
| Worker | Chunkserver | DataNode |
| Unit | Chunk (64 MB) | Block (128 MB) |
| Write | Random writes ✓ | Append-only |
| Concurrency | Multiple writers | Single writer |
| Metadata | Operation log | Journal + Edit log |
Key Difference: HDFS simplified to append-only for stronger consistency
🌐 Apache Hadoop Ecosystem
Storage: HDFS (foundation)
Resource Management: YARN
Processing:
- MapReduce (batch)
- Spark (in-memory)
- Pig, Hive (SQL-like)
Databases: HBase (NoSQL)
ML: Mahout, Spark MLlib
Streaming: Kafka, Storm
Data Ingestion:
- Flume (unstructured)
- Sqoop (structured/databases)
Coordination: Zookeeper, Ambari
📊 Real-World Scale
Companies Using HDFS
Facebook:
- 300+ PB stored
- Custom warm/cold storage tiers
Yahoo:
- 40,000+ nodes
- 100+ PB processed daily
Uber:
- 100+ PB data lake
- Trip data, logs, ML models
Spotify:
- Music recommendations
- User listening history
Production Statistics (Historical)
Twitter:
- 6,000 tweets/second
- 50 million tweets/day
Facebook:
- 1.55 billion active users/month
- 136,000 photos uploaded/minute
Google: At least 1 server fails per day
Facebook: 1% of servers fail after each reboot
💡 Key Formulas
1. Replication Cost
2. Data Loss Probability
- = single server failure rate
- = number of replicas
3. NameNode Memory Estimation
Example:
- 1 PB data, 128 MB blocks
- Blocks = 1 PB / 128 MB = 8,388,608
- Memory ≈ 1.2 GB
🎓 Common Exam Questions
Q1: Why are HDFS blocks 128 MB?
Answer:
- Reduces NameNode memory overhead
- Optimizes sequential reads
- Minimizes seek time
- Better for MapReduce workloads
Q2: Can HDFS lose data?
Answer:
- Yes, if all 3 replicas fail simultaneously
- Probability: 0.0001% (extremely rare)
- Mitigated by rack-aware placement
Q3: Why write-once, read-many?
Answer:
- Simpler consistency model
- Easier failure recovery
- Optimized for append-heavy workloads
- Updates = delete old + write new
Q4: What if NameNode fails?
Answer:
- Entire cluster unavailable (SPOF)
- Solution: HA NameNode with ZooKeeper
- Secondary NameNode only does checkpointing
Q5: Can client skip NameNode?
Answer:
- ❌ NO - client doesn't know:
- Which DataNodes have space
- Where to place replicas
- Block IDs to use
- Authentication credentials
Analogy: Can't deposit money without going through bank teller
🔄 Modern Alternatives
Cloud Services
AWS:
- EMR (Managed Hadoop)
- S3 (Object storage)
- EBS (Block storage)
Google Cloud:
- Dataproc (Managed Hadoop)
- Colossus (GFS successor)
Azure:
- HDInsight (Managed Hadoop)
- Data Lake Storage
Newer Technologies
Object Storage:
- MinIO, Ceph (S3-compatible)
- Decouples storage from compute
Cloud-Native:
- Alluxio (memory-speed)
- JuiceFS (POSIX-compatible)
Erasure Coding:
- 1.5x overhead vs 3x replication
- Facebook's f4 for cold storage
📝 Quick Reference Table
| Aspect | Traditional FS | HDFS |
|---|---|---|
| Block Size | 4-64 KB | 128-256 MB |
| Replication | 0-1x | 3x |
| Write Model | Random | Append-only |
| Best For | General use | Big data batch |
| Typical Files | KB-MB | GB-TB |
⚡ Practice Problems
Problem 1: Storage Calculation
- Data: 1 PB
- Replication: 3x
- Answer: 3 PB total storage
Problem 2: Recovery Time
- Block: 128 MB
- Network: 100 MB/s
- Replicas needed: 2
- Answer: 1.28s × 2 = 2.6 seconds
Problem 3: Failure Impact
- Given: 10,000 servers, 1% failure rate
- Expected failures: 100 on maintenance day
- Monthly normal failures: 30+
🎯 Remember: Write-Once, Read-Many
HDFS is optimized for:
- ✅ Large sequential reads
- ✅ Batch processing (MapReduce)
- ✅ Append operations
- ✅ High throughput
HDFS is NOT good for:
- ❌ Random writes
- ❌ Low-latency reads
- ❌ Lots of small files
- ❌ Frequent updates
🔑 Critical Takeaways
- Replication = Reliability - 3 copies by default
- Large Blocks = Performance - 128-256 MB
- NameNode = Brain - Single point of failure (use HA!)
- Rack-Aware = Smart - 2 local + 1 remote replicas
- Write-Once = Simple - No in-place updates
- Locality = Speed - Read from closest replica
💾 Good luck on your exam, Google!