Chapter 4: YARN - Yet Another Resource Negotiator
YARN (Yet Another Resource Negotiator) represents a fundamental redesign of Hadoop introduced in Hadoop 2.0. It decouples resource management from data processing, transforming Hadoop from a MapReduce-only system into a general-purpose cluster operating system capable of running diverse workloads.- Understand why YARN was created and what problems it solves
- Master YARN architecture: ResourceManager, NodeManager, ApplicationMaster
- Learn resource allocation and scheduling algorithms
- Explore how different frameworks (MapReduce, Spark, Flink) run on YARN
- Compare YARN with Hadoop 1.x and modern alternatives like Kubernetes
Why YARN? The Evolution from Hadoop 1.x
Problems with Hadoop 1.x (MRv1)
The YARN Solution
- Separation of Concerns
- Flexible Resources
- Multi-Framework
- High Availability
YARN Architecture
Core Components
ResourceManager Deep Dive
ResourceManager Components
ResourceManager Components
Scheduling Algorithms
Scheduling Algorithms
Deep Dive: Hierarchical Queues and Preemption
In large production clusters, YARN is shared by multiple departments, teams, and project types. To manage this complexity, YARN uses Hierarchical Queues.1. Capacity Scheduler: The Organizations Choice
TheCapacityScheduler is designed for organizations with strict resource quotas.- Guaranteed Capacity: Each queue is guaranteed a percentage of cluster resources.
- Elasticity: If Queue A is idle, Queue B can use its resources.
- User Limits: Prevent a single user from hogging all resources in a queue.
2. Fair Scheduler: The Interactive Choice
TheFairScheduler ensures that all applications get an equal share of resources over time.- Fair Share: If two apps are running, they each get 50%.
- Dominant Resource Fairness (DRF): Handles multi-dimensional resources. If App A is CPU-heavy and App B is Memory-heavy, DRF calculates fairness based on the “dominant” resource each app consumes.
3. Preemption: Reclaiming Resources
What happens if Queue A is using its guaranteed 50%, and Queue B (also guaranteed 50%) is currently empty? Queue A will scale up to 100%. If a job is then submitted to Queue B, YARN must reclaim resources from Queue A.- Graceful Termination: YARN sends a signal to the AM of the over-allocated containers, asking them to finish up.
- Hard Kill: If the AM doesn’t release containers within a timeout (e.g., 15 seconds), the RM will forcefully kill those containers to satisfy the guarantee of Queue B.
Resource Requests
Resource Requests
High Availability
High Availability
NodeManager Deep Dive
- NodeManager Responsibilities
- Container Execution
- Resource Monitoring
- Log Aggregation
ApplicationMaster Deep Dive
ApplicationMaster Lifecycle
ApplicationMaster Lifecycle
MapReduce AM (MRAppMaster)
MapReduce AM (MRAppMaster)
Spark AM (SparkContext)
Spark AM (SparkContext)
Deep Dive: YARN State Machines and Lifecycle Internals
The robustness of YARN comes from its strict state-machine-driven design. Both theResourceManager and ApplicationMaster operate as complex event-driven state machines.
1. ResourceManager Application State Machine
The RM tracks every application from submission to completion. If the RM restarts, it reconstructs this state from theRMStateStore.
2. ApplicationMaster Scheduling Lifecycle
The AM’s primary job is to manage the lifecycle of individual tasks within its application.3. The Resource Negotiation Loop (Heartbeat)
The “Negotiation” in YARN happens entirely through the heartbeat mechanism.- NM -> RM Heartbeat (NodeStatusUpdater):
- Reports: “I have 4GB RAM free and 2 vCores.”
- Receives: “Kill Container X” or “Launch Container Y.”
- AM -> RM Heartbeat (ApplicationMasterService):
- Reports: “I need 10 containers with [1GB, 1Core] on Rack /Rack1.”
- Receives: “Here are 3 containers on Node A, B, and C.”
YARN Container Model
Container Abstraction
Comparing YARN with Alternatives
YARN vs Hadoop 1.x
- Architecture
- Resource Utilization
- Failure Handling
YARN vs Kubernetes
Key Takeaways
- YARN Decoupled Resource Management: ResourceManager knows only about resources, not applications. ApplicationMaster handles app-specific logic.
- Containers Not Slots: Flexible resource allocation (any memory/CPU combination) vs rigid slots. This alone improves utilization by 30-40%.
- Multi-Framework Platform: YARN enables Spark, Flink, Tez, and more to coexist, not just MapReduce.
- Scalability Beyond 10K Nodes: Separation of concerns allows YARN to scale far beyond Hadoop 1.x limits.
- High Availability is Built-In: ResourceManager HA and work-preserving recovery make YARN production-ready.
- ApplicationMaster Per App: Isolates application failures. One bad app doesn’t affect others.
- Data Locality Still Matters: YARN preserves HDFS data locality awareness, critical for performance.
- Evolution Path to Cloud: Understanding YARN helps understand Kubernetes, cloud resource management, and container orchestration.
Interview Questions
Basic: What problem does YARN solve that Hadoop 1.x couldn't?
Basic: What problem does YARN solve that Hadoop 1.x couldn't?
- Hadoop 1.x could ONLY run MapReduce jobs
- YARN supports any distributed application (Spark, Flink, Tez, etc.)
- Turns Hadoop into a general-purpose cluster OS
- JobTracker handled everything (resource mgmt + scheduling + monitoring)
- Single JVM couldn’t scale beyond ~4000-5000 nodes
- YARN: ResourceManager focuses only on resources, scales to 10K+ nodes
- Fixed map/reduce slots led to idle resources (map slots unused while reduce slots busy)
- YARN: Flexible containers can be allocated for any purpose
- Utilization improved from ~60% to ~90%
- JobTracker failure meant all jobs lost
- YARN: Built-in RM HA with ZooKeeper, work-preserving recovery
Intermediate: Explain the role of ApplicationMaster in YARN
Intermediate: Explain the role of ApplicationMaster in YARN
-
Resource Negotiation:
- Calculates resource needs (memory, CPU per task)
- Sends ResourceRequests to ResourceManager
- Receives Container allocations
-
Task Scheduling:
- Decides which tasks run in which containers
- Handles data locality (for HDFS-aware apps like MapReduce)
- Manages task dependencies (map before reduce)
-
Task Monitoring:
- Tracks progress of all tasks
- Detects task failures
- Requests replacement containers
-
Failure Handling:
- Re-launches failed tasks
- Implements speculative execution
- Handles node failures
-
Lifecycle Management:
- Starts when application begins
- Unregisters from RM when application completes
- Exits and releases all resources
Advanced: How does YARN achieve data locality for HDFS-aware applications?
Advanced: How does YARN achieve data locality for HDFS-aware applications?
- Node-local (best): Container on same node as HDFS block
- Rack-local (good): Container on same rack (faster network)
- Off-rack (acceptable): Any available node
- Capacity/Fair Schedulers try to satisfy locality first
- Wait a configured delay for node-local (default: ~3 seconds)
- If not available, relax to rack-local
- Last resort: any node
- Node-local: ~10 Gbps (local disk)
- Rack-local: ~1 Gbps (rack switch)
- Off-rack: ~500 Mbps (core switch)
- Node-local: ~1 second
- Rack-local: ~10 seconds
- Off-rack: ~20 seconds
System Design: Design a YARN scheduler for machine learning workloads
System Design: Design a YARN scheduler for machine learning workloads
- Long-Running Jobs: ML training jobs run for hours/days (vs minutes for MR)
- GPU Resources: Need GPU allocation, not just CPU/memory
- Gang Scheduling: Distributed training needs all workers to start together
- Preemption Concerns: Can’t kill ML job midway (lose expensive training progress)
- Priority: Critical experiments should preempt less important ones
- Reservation: Reserve resources for scheduled training runs
Deep Dive: How would you debug a YARN application that's stuck in ACCEPTED state?
Deep Dive: How would you debug a YARN application that's stuck in ACCEPTED state?
- Set appropriate AM memory (don’t request 10GB for AM!)
- Monitor cluster utilization
- Configure queue limits appropriately
- Set up alerts for unhealthy nodes
- Use preemption to free resources for high-priority apps
Further Reading
YARN Documentation
Hadoop: The Definitive Guide
YARN Paper
Spark on YARN
Up Next
In Chapter 5: Ecosystem, we’ll explore:- Hive: SQL on Hadoop
- Pig: Data flow language
- HBase: NoSQL database on HDFS
- Oozie: Workflow scheduling
- Kafka: Stream ingestion
- How these tools integrate with YARN and HDFS
Interview Deep-Dive
YARN replaced the Hadoop 1.x JobTracker. What specific problems did this solve, and what new problems did it introduce?
YARN replaced the Hadoop 1.x JobTracker. What specific problems did this solve, and what new problems did it introduce?
Compare YARN Capacity Scheduler and Fair Scheduler. When would you choose one over the other?
Compare YARN Capacity Scheduler and Fair Scheduler. When would you choose one over the other?
A Spark application on YARN is running slowly. Walk me through your debugging process.
A Spark application on YARN is running slowly. Walk me through your debugging process.