Round-Robin Distribution
Press Send Requests to fire traffic onto the load balancer. Watch requests distribute evenly across servers in round-robin order.
Code example
A round-robin load balancer class and health check logic in JavaScript.
// Round-robin load balancer
class LoadBalancer {
constructor(servers) {
this.servers = servers; // array of server URLs
this.idx = 0;
}
next() {
// Skip unhealthy servers
const healthy = this.servers.filter(s => s.healthy);
if (healthy.length === 0) throw new Error('No servers');
const server = healthy[this.idx % healthy.length];
this.idx++;
return server;
}
forward(req) {
return fetch(this.next().url + req.path, {
method: req.method,
headers: req.headers,
body: req.body
});
}
}
const lb = new LoadBalancer([
{ url: 'http://server1:3000', healthy: true },
{ url: 'http://server2:3000', healthy: true },
{ url: 'http://server3:3000', healthy: true },
]);
// Health check — mark servers up/down
async function healthCheck(lb) {
for (const server of lb.servers) {
try {
const res = await fetch(server.url + '/health', {
signal: AbortSignal.timeout(2000) // 2s timeout
});
server.healthy = res.ok;
} catch {
server.healthy = false; // unreachable
}
}
}
// Run health checks every 10 seconds
setInterval(() => healthCheck(lb), 10_000);
// Sticky sessions: same client ? same server
// Use consistent hashing on client IP or session ID
function sticky(clientId) {
const hash = clientId.charCodeAt(0) % lb.servers.length;
return lb.servers[hash];
}
Key concepts
Load Balancer
A reverse proxy that distributes incoming requests across a pool of servers. Clients see one address; the load balancer decides which backend gets each request.
Round Robin
The simplest strategy: send request 1 to server 1, request 2 to server 2, request 3 to server 3, then start over. Equal distribution when servers are identical.
Horizontal Scaling
Add more servers instead of upgrading one server. Horizontal scaling (scale out) is more cost-effective and more resilient than vertical scaling (scale up) for web workloads.
Health Check
Load balancers periodically probe each server at /health. If a server fails to respond, it's removed from rotation automatically. New requests go only to healthy servers.