A high-performance, concurrent Layer 7 Load Balancer built entirely from scratch using Go's standard library.
This project was developed to demonstrate a deep understanding of system architecture, network programming, and safe concurrency in Go. Rather than relying on external frameworks, it leverages core Go primitives to efficiently distribute HTTP traffic across multiple backend servers while actively monitoring their health.
- Round-Robin Traffic Distribution: Evenly routes incoming requests across a pool of available backend servers.
- Active Health Checks: Runs asynchronous background goroutines to periodically ping backends via TCP. Automatically removes dead nodes from the rotation and reinstates them when they recover.
- Thread-Safe State Management: Utilizes Go's
sync/atomicpackage for lock-free, high-speed routing index calculation, andsync.RWMutexto protect server health states across thousands of concurrent requests. - Zero-Dependency Core: Built exclusively using standard library packages (
net/http,net/http/httputil,sync,atomic) to showcase fundamental language proficiency. - Production-Ready Deployment: Fully containerized using multi-stage Docker builds and orchestrated via Docker Compose.
In Go, http.ListenAndServe spawns a new Goroutine for every incoming request. Under heavy load, using a standard sync.Mutex to lock the server pool array just to increment the routing counter would create a massive bottleneck. Instead, this project uses atomic.AddUint64 to increment the index directly at the hardware level, ensuring lock-free thread safety and maximum throughput.
Traffic tunneling is handled by httputil.NewSingleHostReverseProxy. This acts as the core engine, transparently forwarding client headers, handling TCP connection pooling to the backends, and streaming responses back to the client without buffering large payloads in memory.
- Docker & Docker Compose installed.
- (Optional) Go 1.24+ if running locally without Docker.
The included docker-compose.yml file spins up the load balancer alongside three dummy Python web servers for immediate testing.
# Clone the repository
git clone https://github.com/yourusername/go-load-balancer.git
cd go-load-balancer
# Build and launch the cluster
docker-compose up --build- Open your browser or use
curlto hit the load balancer:curl http://localhost:8080
- You will see responses alternating sequentially between Backend A, B, and C.
- Simulate a Server Crash: In a new terminal, stop one of the backend containers:
docker stop go-load-balancer-backend2-1
- Check the load balancer logs. Within 10 seconds, it will detect the failure (
[backend2:80] Server is down). Subsequentcurlrequests will seamlessly route only between the remaining healthy nodes.
This codebase is built with reliability in mind. Unit tests isolate the core NextPeer routing logic from the HTTP server to verify the algorithm under various states (happy path, recovering nodes, total outage).
To run the tests with Go's built-in Race Detector (proving memory safety across concurrent goroutines):
go test -v -race ./...While fully functional, this project lays the groundwork for advanced load-balancing techniques:
- Weighted Round Robin: Allowing configuration of backend capacities.
- Least Connections Algorithm: Routing traffic to the server with the fewest active requests.
- Dynamic Configuration: Reloading the backend server pool from a config file without dropping active connections.
Designed and built to showcase backend engineering capabilities.