A Algo3DVerse
05 — System Design
System Design — Lesson 5 of 6

Load
Balancing

Requests rain onto a load balancer and distribute across servers. Add servers, watch the traffic spread — round-robin in 3D.

A single server has limits. When traffic exceeds its capacity, you don't upgrade to a bigger server (vertical scaling has a ceiling) — you add more servers and distribute the load. A load balancer is the traffic director that makes this invisible to clients.

Round-Robin Distribution

Press Send Requests to fire traffic onto the load balancer. Watch requests distribute evenly across servers in round-robin order.

drag — scroll to zoom

Code example

A round-robin load balancer class and health check logic in JavaScript.

LOAD BALANCER — ROUND ROBIN
// 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

traffic director — single entry

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

1?2?3?1?2?3

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

more servers — not bigger

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

liveness — readiness

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.

→ Caching System Design — Algo3DVerse Message Queues ?
Press ? to begin the narration walkthrough.
0 / 8