ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/loadbalancers/tasks/task-20-3-2-weighted-round-robin
TASK

Implementation

Why weighted round-robin?. . Problem: backends have different capacities. backend-1: 8 cores, 32GB RAM (high capacity). backend-2: 8 cores, 32GB RAM (high capacity). backend-3: 2 cores, 8GB RAM (low capacity). Standard round-robin sends 33% to each:. - backend-3 gets overwhelmed. - backend-1 and backend-2 underutilized. Weighted round-robin (weights: 4, 4, 2):. - backend-1: 40% of traffic. - backend-2: 40% of traffic. - backend-3: 20% of traffic. - Matches capacity ratios. . Smooth weighted round-robin algorithm:. typescript. name: string,. weight: number,. currentWeight: number. let maxBackend = null;. let maxWeight = -Infinity;. let totalWeight = 0;. // Add weight to each backend's current weight. backend.currentWeight += backend.weight;. totalWeight += backend.weight;. maxWeight = backend.currentWeight;. maxBackend = backend.name;. // Subtract total weight from selected backend. maxBackend.currentWeight -= totalWeight;. return maxBackend.name;. . Example weighted routing:. json. // Configuration:. "backends": [. ]. // Request sequence (first 10 requests):. Request: 1 → api-1. Request: 2 → api-2. Request: 3 → api-1. Request: 4 → api-2. Request: 5 → api-1. Request: 6 → api-2. Request: 7 → api-1. Request: 8 → api-2. Request: 9 → api-3 (20% traffic). Request: 10 → api-3. // Distribution: api-1: 40%, api-2: 40%, api-3: 20%.

Sample Test Cases

Weight-based distributionTimeout: 5000ms
Input
{"src":"client","dest":"lb","body":{"type":"init","msg_id":1,"backends":[{"name":"api-1","weight":4},{"name":"api-2","weight":4},{"name":"api-3","weight":2}],"algorithm":"weighted-round-robin"}}
{"src":"client","dest":"lb","body":{"type":"send_requests","msg_id":2,"count":10}}
Expected Output
{"src": "lb", "dest": "client", "body": {"type": "init_ok", "in_reply_to": 1}}
Unequal weightsTimeout: 5000ms
Input
{"src":"client","dest":"lb","body":{"type":"init","msg_id":1,"backends":[{"name":"large","weight":10},{"name":"medium","weight":3},{"name":"small","weight":1}],"algorithm":"weighted-round-robin"}}
{"src":"client","dest":"lb","body":{"type":"send_requests","msg_id":2,"count":14}}
Expected Output
{"src": "lb", "dest": "client", "body": {"type": "init_ok", "in_reply_to": 1}}

Hints

Hint 1
Assign weights to backends based on capacity (e.g., CPU, memory)
Hint 2
Higher weight = proportionally more requests
Hint 3
Weights 4, 4, 2 means 40%, 40%, 20% traffic distribution
Hint 4
Use a smooth weighted round-robin algorithm
Hint 5
Handle weight changes dynamically without disruption
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

weighted round-robincapacity-based routingbackend weightsheterogeneous clusterstraffic proportionality
main.py
python
Implement Weighted Round-Robin Load Balancing - Load Balancers | Build Distributed Systems