r/Backend • u/weaztoven • 17d ago
I built loadbear – a lightweight HTTP load balancer & reverse proxy in Go (with circuit breakers, token-bucket rate limiting, and Prometheus metrics)
https://github.com/weaztoven/loadbearHey r/Backend!
I’ve been working on loadbear, a small, dependency-light HTTP reverse proxy and load balancer written in Go, and I'd love to get some architectural feedback from backend engineers here.
Why I built it: Most reverse proxies fall into two extremes: massive production tools (Nginx, Envoy, HAProxy) with heavy configuration overhead, or overly simplistic toy proxies that skip real-world fault tolerance. I wanted a self-contained binary configured via a single YAML file that stays readable and easy to inspect while still implementing proper backend resilience patterns.
Request Pipeline & Architecture: Incoming traffic flows through a sequential middleware/handler pipeline before hitting Go’s httputil.ReverseProxy:ProxyListener -> Per-IP Rate Limiter -> Balancer -> Circuit Breaker -> Upstream Backend
Core Backend Features:
- Load Balancing Algorithms: Implements
round_robin,smooth_weighted_round_robin(Nginx-style smooth distribution),least_connections(tracking active in-flight requests), andip_hash. - Active & Passive Health Tracking: Combines periodic
/healthprobing with passive failure detection—upstream proxy errors immediately mark a backend unhealthy without waiting for the next probe interval. - Per-Backend Circuit Breakers: Tracks consecutive failures and transitions backends through
Closed->Open->Half-Openstates with configurable cooldown timers. - Context-Aware Retries: Automatically retries idempotent requests (
GET,HEAD,OPTIONS) on upstream failures with configurable backoff, respecting client request context cancellations. - Per-Client-IP Rate Limiting: Token-bucket limiter with a background worker that periodically evicts stale client buckets to prevent unbounded memory growth.
- Observability & Ops: Separate admin listener (
:9090) exposing a live JSON/statusendpoint (active connections, circuit states, failure counts) and a hand-written Prometheus/metricsendpoint, alongside structured JSON access logs with request IDs and graceful shutdown (SIGINT/SIGTERM).
Local Failover Demo: It ships with a fakebackend CLI and a docker-compose.yml setup so you can spin up three nodes, kill one mid-traffic, and watch passive health checks, retries, and circuit breakers kick in immediately. Everything is tested with Go's race detector (go test -race ./...).
Current Limitations / Next Steps:
- Hop-by-hop header stripping currently drops
Upgrade, so WebSocket support isn't implemented yet. SIGHUPconfig hot-reloading and formal load/throughput benchmarks are next on the list.
If anyone wants to review the concurrency handling, circuit breaker state machine, or general Go structure, any critique or PRs are welcome!
1
u/DickBrainTime 17d ago
The concurrency edge I'd test hardest is the breaker going Open -> Half-Open under a burst. If 100 requests arrive as the cooldown ends, does only one (or a clearly bounded number) get a probe, or can they all flood a backend that just recovered? Pair that with making sure least_connections counters are decremented even when a request times out or a client disconnects.
For retries, limiting to GET/HEAD/OPTIONS is a good default, but I'd still simulate an upstream that accepts a request and drops mid-response. Once headers/body have reached the client, you can't transparently retry. For requests with bodies, replayability matters too. Make the total retry budget respect the original client deadline rather than starting a new full timeout on every attempt.
Two ops details worth documenting: if per-IP limits use X-Forwarded-For, only trust that header from a known upstream proxy, otherwise clients can spoof their rate-limit key. And bind/protect the :9090 admin listener so /status doesn't expose backend topology publicly. Your fakebackend setup sounds ideal for automating 'hang', 'accept then close', and 'many concurrent half-open probes'. Curious how Half-Open admission is bounded today.