Distributed Scheduler Design: FairShare
Overview
FairShare is a distributed scheduler for cluster workloads (e.g., batch jobs, microservices, ML training) across 100s–1000s of nodes. It ensures fairness by combining:
- Priorities: Higher-priority jobs preempt or get preferential allocation.
- Leases: Time-bound resource grants with heartbeats for liveness.
- Backpressure: Overloaded nodes signal to throttle submissions.
- Preemption: Kill/reschedule low-priority tasks for high-priority ones.
Inspired by Kubernetes, Mesos, and Nomad, but with integrated priority-aware Dominant Resource Fairness (DRF) for multi-resource (CPU, RAM, GPU) equity.
Goals:
- Maximize cluster utilization (>90%).
- Enforce fairness: No user/job starves; dominant resource shares respected.
- Handle failures: Leader election, eventual consistency.
- Scale: O(log N) scheduling latency.
Assumptions:
- Homogeneous/heterogeneous cluster.
- Resources: CPU cores, RAM GB, GPU units.
- Workloads: Tasks (atomic units, e.g., containers/pods) grouped in Jobs.
Architecture
Clients ───┐
│ Submit Jobs/Tasks
▼
+-----------------------------------+
| FairShare Master(s) |
| - Leader (Raft/Zab election) |
| - Job/Task Queue (Priority+Fair) |
| - Scheduler Loop |
| - Gang Scheduler (for Jobs) |
+───┬───┬───┬───────────────────────┘
│ │ │ Heartbeats/Offers
▼ ▼ ▼
+───────+ +───────+ +───────+
| Agent1| | Agent2| ... | AgentN| (One per node)
| - Local Queue |
| - Executor/Preemptor |
| - Metrics/Backpressure |
+──────────────┼───────────────+
│ RPC/Updates
▼
+───────────────+
| Storage |
| - Etcd/Cassandra |
| (Jobs, Tasks, |
| Allocs, Shares)|
+───────────────+
Components
- Clients: Submit Jobs via gRPC/REST. Jobs specify: tasks, resources, priority (1-10), user/group, deadline.
- Master(s) (3–5 for HA):
- Leader: Runs scheduler loop (every 100ms).
- Job Queue: Priority queue (heap) + per-user fair share queue.
- State: Tasks pending/running, user shares (in Etcd).
- Agents (one/node):
- Report heartbeats (every 10s): available resources, running tasks, load (e.g., CPU>80%).
- Local scheduler: FIFO within node for co-located tasks.
- Storage: Etcd for config/state (WAL for durability).
Data Models
message Resource {
int32 cpu_cores;
int64 ram_gb;
int32 gpus;
}
message Task {
string id;
Resource req;
string image; // Docker/OCI
int32 priority = 5; // 1=low, 10=crit
string user;
Lease lease; // Optional
}
message Allocation {
string task_id;
string agent_id;
Resource alloc;
Timestamp lease_expiry;
}
message UserShare {
string user;
Resource used; // Running allocs
Resource share; // Fair target (e.g., DRF)
double dominance; // Max resource fraction
}
Core Mechanisms
1. Scheduling Loop (Master Leader)
While true:
1. Receive Agent Heartbeats → Resource Offers (available + backpressure flag)
2. Filter Offers: Ignore if backpressure (load > threshold)
3. Compute Fair Shares: DRF per user/group + priority boost
4. Priority Queue Dequeue: High-pri → low-pri
5. Match Tasks to Offers: Binpack (BestFit) respecting shares
6. If no fit & high-pri: Trigger Preemption
7. Send TaskAssignments to Agents
8. Update State/Shares
Fairness: Priority-Aware DRF
- Base: DRF – Allocate to user maximizing
min(used_r / share_r) over resources r, preventing dominance.
- Priority Boost: Effective share = base_share * (priority / 5.0).
- Shares Update: On alloc/kill:
user.used +=/- alloc. Recalculate every 1s.
- Gang Scheduling: For multi-task jobs (e.g., distributed training), allocate all-or-nothing.
2. Leases
- On alloc: Agent grants lease (e.g., 60s expiry).
- Task must heartbeat to Master/Agent every 30s → renew.
- Expiry: Agent kills task, frees resources, notifies Master.
- Benefits: Detect node/task failures fast. Master can reassign.
- Graceful drain: On node shutdown, extend leases + evict.
Heartbeat: { task_id, agent_id, load_metrics, lease_renew=true }
3. Backpressure
- Agent Metrics: CPU utilization, queue length, disk I/O.
- Thresholds: If CPU>90% or queue>10 → set
backpressure=true in heartbeat.
- Master Reaction:
- Ignore offers from backpressured agents.
- Throttle client submissions per-user if >50% agents backpressured.
- Exponential backoff: Clients retry after 2^x seconds.
- Local: Agent rejects new tasks if overloaded.
4. Preemption
Failure Handling
- Master Fail: Raft leader election (<1s downtime). Followers replay Etcd.
- Agent Fail: Leases expire → tasks requeued.
- Task Fail: Exponential backoff retries (max 3), then mark failed.
- Consistency: Optimistic (assign → confirm). Conflicts resolved by task_id uniqueness.
- Monitoring: Prometheus metrics (utilization, queue depth, preempt rate).
Implementation Sketch (Go/Rust)
- Master: Actor model (e.g., Actix/Axon). Scheduler as hot loop.
- Agent: gRPC server + local executor (containerd).
- Storage: Etcd watches for state changes.
- Scalability: Shard jobs by user hash if >10k pending.
Evaluation Metrics
- Utilization: >90% (binpack + preemption).
- Fairness: Jain's index >0.95 on shares.
- Latency: P99 schedule <500ms.
- Throughput: 1k tasks/sec.
This design balances fairness, responsiveness, and resilience. Prototypable in ~2k LoC. Questions?