How health checks work
Health checking is a critical mechanism that ensures traffic is only routed to operational backend servers. TheHealthChecker class periodically polls each backend to verify it’s responding correctly.
By default, health checks run every 5 seconds with a 3-second timeout per check.
Health check implementation
The health checker uses parallel HTTP requests to monitor all backends simultaneously:src/healthchecker/healthChecker.ts
Key features
Parallel health checks
The implementation usesPromise.all() to check all backends simultaneously rather than sequentially. This ensures:
- Fast health checks - All servers are checked at once
- Consistent timing - The 5-second interval applies to all backends together
- Efficient resource usage - No waiting for slow servers to timeout before checking others
Using
Promise.all() means if you have 10 backends, they’re all checked in parallel, not one after another.Timeout handling with AbortController
Each health check has a 3-second timeout enforced usingAbortController:
- If a server doesn’t respond within 3 seconds, the request is aborted
- The backend is marked as unhealthy
- The health checker moves on without waiting
Health check interval
The default interval is 5 seconds, meaning:- Every 5 seconds, all backends are checked
- Healthy servers respond quickly and stay in rotation
- Failed servers are detected within 5 seconds maximum
src/healthchecker/healthChecker.ts
Recovery mechanism
When a backend recovers from failure, it’s automatically added back to the pool:Backend fails health check
The server is marked as unhealthy and removed from the rotation. No traffic is sent to it.
Server recovers
When the backend starts responding with
res.ok status codes (2xx), it’s marked healthy again.Recovery is automatic - no manual intervention needed. Failed servers rejoin the pool as soon as they’re healthy.
Starting and stopping health checks
The health checker lifecycle is managed withstart() and stop() methods:
src/healthchecker/healthChecker.ts
start()runs an immediate health check, then begins the polling loopstop()halts the loop gracefully- The
isRunningflag prevents duplicate loops
Health checks start immediately when
start() is called - you don’t wait 5 seconds for the first check.