Get started in 5 minutes
This guide will have you running the load balancer with three test backend servers and making requests in under 5 minutes.Install Bun runtime
The load balancer requires the Bun runtime. Install it if you haven’t already:Verify installation:
Start test backend servers
Open three separate terminal windows and start simple HTTP servers on ports 3001, 3002, and 3003:
These are minimal test servers that return a simple text response. In production, these would be your actual application servers.
Start the load balancer
In a fourth terminal window, start the load balancer:You should see output like this:
Test with curl
Send requests to the load balancer and watch traffic distribute across backends:Expected output (round-robin distribution):The load balancer logs will show each request:
Understanding the code
Here’s how the components wire together insrc/index.ts:
src/index.ts
Key components explained
BackendPool — Manages the list of backend servers and their health status. Each backend has a URL and a boolean health flag.src/balancer/pool.ts
src/balancer/roundRobin.ts
src/balancer/loadBalancer.ts
Development mode
For development with automatic reload on code changes:--watch flag to restart the server when source files change.
Next steps
Installation guide
Learn about prerequisites and project structure
Architecture deep dive
Understand the internal design decisions
Configuration
Customize backend URLs and health check intervals
Extending strategies
Add new load balancing algorithms