Prerequisites
Before installing the load balancer, ensure you have the Bun runtime installed.Install Bun
Bun is a fast JavaScript runtime and package manager that powers this load balancer.Why Bun? This project uses Bun for its exceptional performance, built-in TypeScript support without compilation, and fast package installation. The load balancer can handle high request throughput thanks to Bun’s optimized HTTP primitives.
Installation steps
Install dependencies
Install all required packages using Bun:This installs:
express- HTTP server frameworkexpress-http-proxy- Reverse proxy middlewaredotenv- Environment variable management- TypeScript types for development
Project structure
Here’s the complete directory layout:Key directories explained
src/balancer/ — Contains the core load balancing logic. The LoadBalancer class orchestrates the BackendPool and routing RoundRobin strategy to select backends for incoming requests.
src/healthchecker/ — Implements periodic health checks using fetch() with timeout controllers. Runs checks in parallel and updates the backend pool’s health status.
src/proxy/ — Contains the Express middleware that forwards requests to selected backends using express-http-proxy. Handles errors and marks backends as unhealthy on failures.
src/types/ — Defines TypeScript interfaces used across the application. Currently contains the Backend interface with url and health properties.
src/utils/ — Utility modules including the structured logger that provides color-coded, categorized logging for requests, responses, health checks, and errors.
Configuration
The load balancer is configured directly insrc/index.ts. You can customize:
Backend servers
Edit thebackendUrls array to add or remove backend servers:
src/index.ts
Health check interval
Change the health check frequency by modifying the second parameter (in milliseconds):src/index.ts
Load balancer port
Change the port the load balancer listens on:src/index.ts
Available scripts
Thepackage.json defines these npm scripts:
| Script | Command | Description |
|---|---|---|
start | bun run src/index.ts | Start the load balancer in production mode |
dev | bun --watch src/index.ts | Start with auto-reload on file changes |
build | tsc -b | Compile TypeScript to JavaScript |
Running in development mode
For development with automatic reload:--watch flag to restart the server whenever source files change.
Building for production
While Bun can run TypeScript directly, you can compile to JavaScript:tsconfig.json settings.
Environment setup
The project includesdotenv for environment variable management. Create a .env file in the project root for environment-specific configuration:
.env
The current implementation uses hardcoded values in
src/index.ts. To use environment variables, you’ll need to load them with dotenv and update the configuration accordingly.TypeScript configuration
The project uses strict TypeScript settings defined intsconfig.json:
tsconfig.json
Troubleshooting
Bun command not found
If you get “command not found” after installing Bun, restart your terminal or add Bun to your PATH:~/.bashrc or ~/.zshrc to make it permanent.
Port already in use
If port 3000 is already in use, either:- Stop the process using that port
- Change the
PORTconstant insrc/index.tsto a different value
Health checks failing
If you see continuous health check failures, ensure:- Backend servers are running on the configured ports
- The backend URLs in
backendUrlsare correct - No firewall is blocking connections to localhost
Next steps
Quickstart guide
Get up and running with test backends in 5 minutes
Architecture overview
Learn how the components work together
API reference
Explore the core classes and methods
Extending strategies
Add custom load balancing algorithms