Skip to content

nstoqnov/loadbalancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚦 Layer 7 Reverse Proxy & Load Balancer

Go Version Docker Status

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.


✨ Key Features

  • 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/atomic package for lock-free, high-speed routing index calculation, and sync.RWMutex to 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.

🧠 Technical Deep Dive (For Engineers)

Why sync/atomic over Mutexes for Routing?

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.

Reverse Proxy Implementation

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.


🚀 Getting Started

Prerequisites

  • Docker & Docker Compose installed.
  • (Optional) Go 1.24+ if running locally without Docker.

Run the Cluster

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

See It in Action

  1. Open your browser or use curl to hit the load balancer:
    curl http://localhost:8080
  2. You will see responses alternating sequentially between Backend A, B, and C.
  3. Simulate a Server Crash: In a new terminal, stop one of the backend containers:
    docker stop go-load-balancer-backend2-1
  4. Check the load balancer logs. Within 10 seconds, it will detect the failure ([backend2:80] Server is down). Subsequent curl requests will seamlessly route only between the remaining healthy nodes.

🧪 Testing & Quality Assurance

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 ./...

🔮 Future Enhancements

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors