Help me implement a round-robin load balancer in raw Node.js without any proxy…
- User
Help me implement a round-robin load balancer in raw Node.js without any proxy libraries. It should spawn child processes for workers and have a health check that removes any child from rotation if it doesn't respond to a ping within 200ms.
- Assistant
Designing round-robin load balancer with health checks
Outlined a Node.js load balancer spawning child processes as workers on distinct ports, proxying requests manually with http.request, and performing periodic 200ms-timeout health pings that remove unhealthy workers from rotation to maintain accurate round-robin dispatch. Next, the plan is to produce clear split code for master and worker processes implementing this logic. You don’t need any third‑party proxy library to do this. The pattern below uses only core modules: a master process that listens on a public port, spawns child processes (workers) that each listen on their own port, and a very simple round‑robin scheduler in the master. A periodic health check pings each worker; if a worker doesn’t answer within 200 ms, it’s removed from the rotation.
worker.js– a simple HTTP server with a/pingendpoint and some example work:// worker.js const http = require('http'); const port = process.env.PORT || 0; // passed in by parent const server = http.createServer((req, res) => { if (req.url === '/ping') { // Fast health response res.writeHead(200); return res.end('pong'); } // Simulate doing some work setTimeout(() => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(`Hello from worker ${process.pid}\n`); }, Math.random() * 100); // random delay just to show round-robin }); server.listen(port, () => { console.log(`Worker ${process.pid} listening on ${port}`); });lb.js– the load balancer/master process:// lb.js const http = require('http'); const { fork } = require('child_process'); const NUM_WORKERS = 4; const START_PORT = 4001; // ports for workers to listen on const HEALTH_PATH = '/ping'; const HEALTH_TIMEOUT_MS = 200; const HEALTH_INTERVAL_MS = 1000; let workers = []; let rrIndex = 0; // Spawn initial workers for (let i = 0; i < NUM_WORKERS; i++) { spawnWorker(START_PORT + i); } function spawnWorker(port) { const child = fork('./worker.js', [], { env: { PORT: port } }); const entry = { pid: child.pid, port, child, healthy: true }; child.on('exit', (code, signal) => { console.warn(`Worker ${child.pid} exited (${signal || code}), removing from pool`); workers = workers.filter(w => w.pid !== child.pid); // Optionally respawn: // spawnWorker(port); }); workers.push(entry); } // Round robin selector function nextWorker() { if (workers.length === 0) return null; // Ensure index is within bounds rrIndex = rrIndex % workers.length; const w = workers[rrIndex]; rrIndex = (rrIndex + 1) % workers.length; return w; } // Health check loop setInterval(() => { workers.forEach(w => { const req = http.get( { host: '127.0.0.1', port: w.port, path: HEALTH_PATH, timeout: HEALTH_TIMEOUT_MS }, res => { // If it responds with 200, consider healthy if (res.statusCode !== 200) { markUnhealthy(w, `bad status ${res.statusCode}`); } res.resume(); // discard body } ); req.on('timeout', () => { req.destroy(); markUnhealthy(w, 'timeout'); }); req.on('error', err => { markUnhealthy(w, err.message); }); }); }, HEALTH_INTERVAL_MS); function markUnhealthy(worker, reason) { if (worker.healthy) { console.warn(`Worker ${worker.pid} unhealthy (${reason}), removing from rotation`); worker.healthy = false; workers = workers.filter(w => w.pid !== worker.pid); // Optionally kill the child: // worker.child.kill(); } } // Load balancer server const server = http.createServer((req, res) => { const target = nextWorker(); if (!target) { res.writeHead(503); return res.end('No healthy workers available'); } // Forward the request to the worker const proxyReq = http.request( { host: '127.0.0.1', port: target.port, path: req.url, method: req.method, headers: req.headers, }, proxyRes => { res.writeHead(proxyRes.statusCode, proxyRes.headers); proxyRes.pipe(res); } ); proxyReq.on('error', err => { console.error('Proxy error:', err.message); res.writeHead(502); res.end('Bad gateway'); }); // Pipe request body to worker req.pipe(proxyReq); }); const BALANCER_PORT = 3000; server.listen(BALANCER_PORT, () => { console.log(`Load balancer listening on port ${BALANCER_PORT}`); });Run the balancer with
node lb.jsand point your client athttp://localhost:3000/…. The balancer will forward each incoming request to the next healthy worker in round‑robin order. Each second it sends a GET/pingto each worker; if a worker doesn’t respond within 200 ms or returns a non‑200, it’s removed from the pool.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.