Deviopath › Node.js

Load Balancer with Health Checks

Hard · Networking

A load balancer (nginx, HAProxy, AWS ALB) spreads requests over several servers and stops sending traffic to servers that fail their health checks. Write simulateLoadBalancer(config, events) that returns a log of what happened.

config is { servers, strategy, unhealthyAfter, healthyAfter }. All servers start healthy with 0 open connections. Process the events in order:

- ["request", id]: pick a healthy server, log "<id> -> <server>" and open a connection on it (it stays open until it is closed). If no server is healthy, log "<id> -> 503".

- ["close", id]: close the connection of request id. Ignore unknown ids, ids that got a 503 and ids that are already closed. Nothing is logged.

- ["health", server, ok]: a health check result (ok is a boolean). A success resets the failure streak and a failure resets the success streak. A healthy server that reaches unhealthyAfter failures in a row becomes unhealthy: log "<server> down". An unhealthy server that reaches healthyAfter successes in a row becomes healthy again: log "<server> up". Connections already open on a server that goes down stay open and still count.

Strategies:

- "round-robin": keep a pointer into servers, starting at index 0. From the pointer, walk forward (wrapping around) to the first healthy server, choose it, and move the pointer to the server right after it. The pointer does not move on a 503.

- "least-connections": choose the healthy server with the fewest open connections. On a tie choose the one that comes first in servers.

Next: Circuit Breaker