TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Let's Create a Simple Load Balancer with Go

366 pointsby UkiahSmithover 5 years ago

12 comments

mholtover 5 years ago
Fun stuff!<p>If you like this kind of thing, we are developing a very powerful and flexible reverse proxy with load balancing into Caddy 2: <a href="https:&#x2F;&#x2F;github.com&#x2F;caddyserver&#x2F;caddy&#x2F;wiki&#x2F;v2:-Documentation#httphandlersreverse_proxy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;caddyserver&#x2F;caddy&#x2F;wiki&#x2F;v2:-Documentation#...</a><p>It&#x27;s mostly &quot;done&quot; actually. It&#x27;s already looking really promising, especially considering that it can do things that other servers keep proprietary, if they do it at all (for example, NTLM proxying, or coordinated automation of TLS certs in a cluster).<p>If you want to get involved, now&#x27;s a great time while we&#x27;re still in beta! It&#x27;s a fun project that the community is really coming together to help build.
评论 #21498525 未加载
manigandhamover 5 years ago
Seems like most of the work is done by the `ReverseProxy` package and this code is more about health checking.<p>Nice to see how simple it is now though. Go is definitely a great choice for low-level networking, and .NET Core has recently become a great option as well.
评论 #21497335 未加载
sciurusover 5 years ago
It&#x27;s nice to see a walkthrough of what goes into a load balancer and how simple it is to build on in Go.<p>One nitpick is that the autho reversed the meaning of active and passive health checks. Active generates new traffic to the backends just to determine their healthiness, passive judges this based on the responses to normal traffic.
arberavdullahuover 5 years ago
I found it more convenient to keep the field unnamed when using mutex in a struct. So in the example that would be<p>type Backend struct {<p><pre><code> URL *url.URL Alive bool sync.RWMutex ReverseProxy *httputil.ReverseProxy }</code></pre>
评论 #21497829 未加载
knicknicover 5 years ago
With talk of proxy and go. I am surprised gobetween hasn’t been mentioned yet <a href="https:&#x2F;&#x2F;github.com&#x2F;yyyar&#x2F;gobetween" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;yyyar&#x2F;gobetween</a> . Last time I looked at the source it was very approachable.
hinkleyover 5 years ago
Load balancer seem like one of those problems that engineers should be cutting their teeth on.<p>And yet we have only a handful and one of the most popular charges money for cool features and does not appear to have an ABI for addons.
评论 #21495762 未加载
评论 #21495939 未加载
评论 #21496173 未加载
评论 #21498507 未加载
kitdover 5 years ago
This is pretty cool. But I think an implementation that avoids the mutexes (mutices?) when allocating the backends and uses channels instead would probably perform better.<p>2 channels needed, 1 for available backends and 1 for broken ones.<p>On incoming request, the front end selects an available backend from channel 1. On completion, the backend itself puts itself either back onto channel 1 on success, or channel 2 on error.<p>Channel 2 is periodically drained to test the previously failed backends to see if they&#x27;re ready to go back onto channel 1.
评论 #21497320 未加载
评论 #21497349 未加载
评论 #21497588 未加载
评论 #21497404 未加载
westoqueover 5 years ago
&gt; Multiple clients will connect to the load balancer and when each of them requests a next peer to pass the traffic on race conditions could occur.<p>I quite don&#x27;t understand what this means? What race conditions? Can anybody explain? Thanks.
评论 #21499092 未加载
评论 #21498935 未加载
noah-kunover 5 years ago
Great tool here: <a href="https:&#x2F;&#x2F;github.com&#x2F;superfly&#x2F;wormhole" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;superfly&#x2F;wormhole</a>
rndmioover 5 years ago
If you don&#x27;t need http smarts on the load balancer LVS is a great option that will give you fantastic performance.
kasvithover 5 years ago
Thanks for sharing the article and thanks all for the feedbacks :)
AdieuToLogicover 5 years ago
The author states:<p>&gt; After playing with professional Load Balancers like NGINX I tried creating a simple Load Balancer for fun.<p>And while nginx[0] certainly can perform in this role, another production quality load balancer is HAProxy[1]. Both can do more than this, of course.<p>Reinventing solutions &quot;for fun&quot; certainly can be educational and help others learn key concepts, but the author should clearly state what they are doing is not meant to replace production quality solutions.<p>0 - <a href="https:&#x2F;&#x2F;www.nginx.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.nginx.com&#x2F;</a><p>1 - <a href="https:&#x2F;&#x2F;www.haproxy.com&#x2F;solutions&#x2F;load-balancing&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.haproxy.com&#x2F;solutions&#x2F;load-balancing&#x2F;</a>
评论 #21495955 未加载
评论 #21495899 未加载