12 ms·
HAProxy vs. Nginx for load balancing (2016)
- rfraile 9y agoThanks Willy Tarreau and all the developers who make this excellent piece of sofware and share it for free.
- a012 9y agoThis is just a comparison of metrics monitoring capable between HAproxy vs. nginx. Nothing related to load balancing capability.
- user5994461 9y agoYes, they have the same features mostly. The difference is that HAProxy has a monitoring page to see what is configured and where connections are going. Nginx is a black box unless you pay them ~ $2000 for the pro edition. Monitoring is feature #1 of a load balancer.
- cjsuk 9y agoCouldn't agree more. Even apache status module was better than nginx. haproxy it is next time.
- vacri 9y agoI would have thought that effective load balancing would be feature #1 of a load balancer...
- qaq 9y agoand how will your determine effectiveness without metrics?
- fredsted 9y agoConsidering "it works" as a feature sets the bar really low.
- vacri 9y agoLucky I didn't say that, eh? A scooter can transport cargo. A truck can transport cargo effectively.
- blowski 9y agoGiven two load balancers, one can load balance 20K requests per second but has good monitoring, the other can load balance 500K requests per second but has bad monitoring. Which is the better load balancer? Note that I'm not implying these figures are true of either HAProxy or nginx, just pointing out that the OP is probably correct that the number one feature of a load balancer is its ability to load balance. Good monitoring makes it easier to use, though.
- gedrap 9y agoAll is good when it's working well, but when something's not right... well, you really wish that you had that monitoring. The truth is that most load balancing solutions are capable of handling _a lot_ of traffic, way way more than the majority of the people need. So performance is not a huge concern, typically. However, if you are at the level where it does actually matter and want to use nginx, chances are, you'll have no problem paying for the premium version.
- user5994461 9y agoThey both have the same performance for HTTP(S) traffic, both software are the same in their algorithms and performance critical aspects. The limit is set by the harware, they will either saturate the NIC or the CPU. HAProxy has a status page to see how many requests are processed and going where. nginx doesn't.
- StreamBright 9y agoYou have two airplanes, one can carry 100 passengers and you can see where you are flying; the other can carry 1000 but you are flying blind. Monitoring is essential to any infrastructure, regardless of its scale.
- busterarm 9y agoA monitoring page but no (usable) logs. we loadbalance nginx clusters with haproxy and we use filebeat & metricbeat to feed logs to an ELK stack. That monitoring page is useless to us now and your logging & monitoring should be centralized to one place anyway.
- user5994461 9y agocentralized logging is a separate concern to centralized monitoring, which is also a separate concern to having a status page.
- digitalbase 9y agoI was expecting this post to be some kind of benchmark/comparison between the two. A better title would be: "Don't use nginx unless you're paying for it".
- rndmio 9y agoWhile the OP is about metrics when it comes to load balancing IPVS from the LVS project http://www.linuxvirtualserver.org/software/ipvs.html http://www.linuxvirtualserver.org/software/ipvs.html is often overlooked. If you're trying to balance a lot of connections/traffic (and aren't/don't want to buy a hardware load balancer) you'll get much better performance with IPVS than HAProxy or nginx.
- _joel 9y agoI've not looked at LVS for a while, does it do SSL termination too? (Not sure as it's Layer4). We generally use haproxy for this reason in addition to using it for balancing.
- dsr_ 9y agoAnd to return to what the article thinks is most important: ipvsadm -L will get you metrics in a human-readable format, /proc/net/ip_vs* has stats for monitoring systems to parse.
- linsomniac 9y agoI've used IPVS a lot in the past and it worked well in a lot of ways. We had a number of problems with ldirectord getting wedged after months of uptime, and the tools are all a little primitive, but IPVS itself works quite well. The amount of traffic it can handle, especially in the "direct routing" configuration, is stunning. But, I can't imagine going back to it from haproxy. Haproxy allows so many other things including nice status page, agent and service checks, SSL termination, great monitoring (we use Influxdb/telegraf/grafana and Icinga2), and really advanced routing of requests (SNI, path, pretty much anything in the requests).
- bowersbros 9y agoHaproxy supporting `.map` functionality is the biggest selling point to me. It allows for SSL protected custom domains for a user of a SaaS app in a really easy to do way. We use it at Shopblocks extensively, and I've written a (brief) outline of roughly how we do it. https://zando.io/post/haproxy-saas-custom-domains/ https://zando.io/post/haproxy-saas-custom-domains/
- blowski 9y agoWhenever I see an article that says “you should NEVER do x” I typically treat it with a large pinch of salt. Plenty of people, myself included, use nginx as a load balancer and it’s absolutely fine. I’m sure there are lots of use cases where HAProxy would be a better choice, but there are also many use cases where nginx is at least as good a choice.
- mosselman 9y agoThis is a fair point. The title could have been 'I recommend HAProxy over nginx because nginx monitoring costs $2000/year'
- bfred_it 9y agoAnd The Great Gatsby should have been called “He dies at the end” Conclusions don’t have to be in the title
- aidos 9y agoNooooooooo! That was totally on my to-read list... :-(
- mosselman 9y agoWhile I get your point and don't really agree with spoiling books here to make one, firstly. Secondly, I don't really think it applies: this isn't literature; the title relevance to the content is more important than being creative.
- ohdrat 9y agoJay brushed the remnants of the Nginx sticker from his laptop. "I will not be enthusiastic for fake open source software" he said. " I must thank Hacker News commentators for saving me the time..."
- moreentropy 9y agoI'm pretty much in love with Nginx' simplicity and capabilities as a swiss army knife for all kinds of HTTP magic. I had some doubts about Nginx' direction and feature development, but most really great features (like stream proxy with SNI support) make their way into the open source release. Built in monitoring sucks though. There are options for better monitoring your requests with Prometheus. This project implements prometheus metrics using Lua scripting inside Nginx: https://github.com/knyar/nginx-lua-prometheus https://github.com/knyar/nginx-lua-prometheus This project is indended as a sidecar service to nginx and receives access logs and timings using syslog protocol (Nginx has a native syslog over UDP capabilities) so no scripting inside Nginx is necessary: https://github.com/markuslindenberg/nginx_request_exporter https://github.com/markuslindenberg/nginx_request_exporter (I'm the author of this exporter)
- unkown-unknowns 9y agoIs there any noteable difference in overhead between the two? Is there any difference in what data is able to be exported?
- moreentropy 9y agoI honestly don't know, I never made serious performance tests. The external exporter can make use of every variable that nginx is able to use in a log_format. I guess the internal Lua scripting has access to even more data. Performance wise I assume the external exporter has less impact on Nginx' operation because it doesn't keep shared state or run code inside Nginx at all (see https://github.com/knyar/nginx-lua-prometheus#caveats https://github.com/knyar/nginx-lua-prometheus#caveats), all Ngnix does is push out log records over syslog/UDP. If the exporter should overload that doesn't concern Nginx at all, what happens is that observations get dropped.
- SEJeff 9y agohaproxy tends to be lighter weight and higher performance. Also, haproxy gives (as this article mentions) a lot more information regarding status information, which can be used to monitor and manage the applications being load balanced. Also note that Willy Tarreau has been a Linux kernel guy for eons and does a lot of upstream kernel patches, some of which haproxy benefit from. Most recent example: https://github.com/torvalds/linux/commit/3979ad7e82dfe3fb94a51c3915e64ec64afa45c3 https://github.com/torvalds/linux/commit/3979ad7e82dfe3fb94a... Willy also was the long term maintainer of the 2.4 Linux kernel (yes... 2.4), and does a lot of maintenance with the long term 2.6 release. So many embedded companies rely on his trees for their devices it isn't even funny.
- merb 9y agowhat's sad about haproxy that it still does not support H2 (HTTP/2). I mean a load balancer should at least support it on the front end. Well haproxy is still great and one of the best load balancers out there. But it's still sad that it misses h2 on the frontend. (Well backend would be cool, but there aren't that many load balancers that can do that anyway). I'm pretty sure haproxy 1.8 will at least give experimental support! which would be awesome.
- spiderfarmer 9y agoThat was a deal breaker for me. HTTP/2 was a great performance boost for my projects and I'm not switching back.
- _joel 9y agoAre you sure? https://cbonte.github.io/haproxy-dconv/1.7/intro.html https://cbonte.github.io/haproxy-dconv/1.7/intro.html " - TLS NPN and ALPN extensions make it possible to reliably offload SPDY/HTTP2 connections and pass them in clear text to backend servers"
- merb 9y agoit supports the TLS ALPN extension which means tcp (haproxy) <-> backend server or nginx <-> backend server over h2
- meche123 9y agoTLDR; this whole article is a joke. The guy is not capable of extracting stats out of his nginx instance, so "Conclusion: Avoid nginx at all costs". End of article.
- c0l0 9y agoI must admit I'm not that huge of a fan of nginx myself, either - The "easy to read, easy to reason about, declarative configuration"-premise breaks down pretty hard when your application setup gets moderately complex. There's just too much obscure magic woven into innocent-looking configuration directives, and as a result of this, more than half of the world gets things wrong (by (ab)using "if" and the like). On top of that, as the article states, introspection into what is going on is laughable in the FOSS release. Still, we use nginx to terminate https at work, and it's doing a very fine job at that - much better than pound, which we used before. If it weren't for Varnish directly behind it, however, it would very often be supremely difficult to debug what's actually going on between our HTTP backends and UAs if the need arises.
- bazzargh 9y agoI hit this problem too - we had >100k lines of nginx config. My answer was to write a tool to simulate what happened to a request, logging the decisions made each step. It was limited in what config I'd programmed it to understand but did most of what we needed (rewrite rules, access control, proxy_pass ...). I also intended it to be used for regression testing - you could set up a yaml file with requests and specify eg, return this proxied response if the proxied request headers match X. Other efforts I'd seen at automated testing of nginx config relied on testing in a VM, but of necessity used different listen addresses etc. They looked complex to set up and weren't even testing the config as shipped...that's what I was aiming for. Since then we've moved away from that config, and I'm in a different role...keep meaning to see if we can open source that code. However, nginx being such a moving target, with lua config and such, I was never sure if it would find an audience.
- patmcguire 9y agoDocker with docker compose is good for this. Nginx can listen on ports 80 and 443, other containers on the same network can reach it on those ports. I have a setup which takes env vars, so it's not quite the same as shipped, but I can test the whole process of getting a cert from LetsEncrypt, for example.
- folex 9y agoAnyone knows if HAProxy can terminate Secure WebSocket? I've tried it about a year ago without any luck, maybe something changed?
- dpatriarche 9y agoSecure websockets (wss://...) works fine for my company's service that runs behind haproxy (version 1.6).
- SkyRocknRoll 9y agoWe moved away from nginx to envoy https://envoyproxy.github.io/ https://envoyproxy.github.io/ . With envoy you get best of both the world with hot reload. Envoy excels in metrics as well as remote configuration.
- pknopf 9y agoWebsite isn't clear, is Windows supported?
- mindcrash 9y agoRecently discovered Traefik (https://traefik.io/ https://traefik.io/) which is a highly performant reverse proxy written in Go. Supports just about every popular backend in existence (Docker, Kuber, Eureka, Consul and whatnot), supports metrics through statsd, Prometheus, Datadog or RYO via REST. Has a pretty good Admin UI but also comes with a REST API to integrate it in your own. Also it is entirely free and open source. Yes you are reading it correctly: Within your edge environment you are able to get pretty much all the features of a $2500+ per instance NGINGX Plus deployment with equal performance for nothing but a little investment in time (unless you need/want commercial support, ofcourse).
- trjordan 9y agoDo you know if it's been deployed at scale anywhere?
- mindcrash 9y agoNot widely. Cap Gemini UK apparently is using it within projects [1], GitLab is using it within their infrastructure [2] and Reevoo is using it aswell [3]. Katacoda [4] and Play-with-Docker [5] both have sandbox environments available which allow you to play with it. [1] https://hackernoon.com/kubernetes-ingress-controllers-and-traefik-a32648a4ae95 https://hackernoon.com/kubernetes-ingress-controllers-and-tr... [2] https://about.gitlab.com/2017/07/11/dockerizing-review-apps/ https://about.gitlab.com/2017/07/11/dockerizing-review-apps/ [3] https://www.youtube.com/watch?v=aFtpIShV60I https://www.youtube.com/watch?v=aFtpIShV60I [4] https://www.katacoda.com/courses/traefik/deploy-load-balancer https://www.katacoda.com/courses/traefik/deploy-load-balance... [5] http://training.play-with-docker.com/traefik-load-balancing http://training.play-with-docker.com/traefik-load-balancing
- sytse 9y ago[2] was a blog post by a guest author, as far as I know we don't use Traefik
- mschuster91 9y agonginx, while being a black box, can at least do logging. The haproxy logging in docker can't log to stdout since over three years without a workaround involving a linked rsyslogd (https://github.com/dockerfile/haproxy/issues/3 https://github.com/dockerfile/haproxy/issues/3). On the other hand, I like configuring a haproxy way, way more than configuring a nginx server. It's easier, and more concise to read.
- Thaxll 9y agoJust symlink your log file to /dev/stdout...
- mschuster91 9y agohaproxy is not able to log to any kind of file: https://unix.stackexchange.com/a/345716/29580 https://unix.stackexchange.com/a/345716/29580 And the official image does not provide for rsyslogd. I want an official image to at least support logging out of the box without having to resort to docker-compose or even weirder setups.
- user5994461 9y agoThey both can do logging. nginx has issues with formatting numbers/string and undefined values, which makes it harder to extract meaningful data from log. haproxy is easier to deal with. Docker abstract away the logging facilities and the filesystem. It's a general issue with Docker, not the software. See the 4th answer in your issue for a solution.
- mschuster91 9y ago> Docker abstract away the logging facilities and the filesystem. No it does not, it expects stdout and stderr to be used and stores its output in a logfile to be replayed with "docker logs" command. First solution in issue: mount /dev/log. Does not work when your host system is OS X or Windows, as you don't have access to the VM that Docker internally uses on non-linuxes. Also, on Linux it pollutes the syslog instead of docker log. Second (the one that refers to a gmane post from 2010) is essentially the same as #1. Third, rsyslog on host (or in a docker image): does not use docker log facility, and docker link is weird. Fourth, use the alpine image which has a syslogd: works, maybe, but is subject to changes in the Dockerfile (e.g. when the inner path of haproxy changes). Also I don't touch Alpine images because I can't use the standard Debian/Ubuntu tooling in case there are network/firewall issues that can best be debugged from inside the container by docker exec -it haproxy bash.
- bobfromhuddle 9y agoWe use nginx-vts (https://github.com/vozlt/nginx-module-vts https://github.com/vozlt/nginx-module-vts) to get metrics out of our nginx servers, and then we send them over to our Riemann stack with a Collectd module: https://github.com/bobthemighty/collectd-vts https://github.com/bobthemighty/collectd-vts The vts module has been updated in recent months to support more granular reporting of request times, but it's had everything we need for a while. It exposes metrics either as an html page or a json api.
- Plugawy 9y agoThis perfect - thanks for sharing. I looked at nginx alternatives like traefik, envoy and haproxy and none of them have an equivalent of auth module http://nginx.org/en/docs/http/ngx_http_auth_request_module.html http://nginx.org/en/docs/http/ngx_http_auth_request_module.h... So with vts I can finally replace last instance of haproxy which serves as a core LB between all services (because it has stats).
- INTPenis 9y agoAs with all things, it's not black & white. If I'm load balancing http or terminating https I'm not going to use HAproxy. HAproxy is very powerful but overkill.
- jsjohnst 9y agoWhy wouldn’t you for those two examples? Those are both great use cases for HAProxy and only require like a twenty line config to be up and running.
- INTPenis 9y ago* I'm not as experienced in HAproxy as I am with nginx * I have a perception of nginx being slimmer than HAproxy and more focused on http/https than HAproxy * Related to the aforementioned point; I have a perception of HAproxy being able to handle many OSI layers compared to nginx which only handles one.
- orthecreedence 9y ago> I have a perception of nginx being slimmer than HAproxy and more focused on http/https than HAproxy Oddly enough, I have the exact opposite perception. HAProxy doesn't even support logging to files because that would "block" the event loop, so it delegates to syslog. That seems fairly slim to me. The entire project is written with no compromises to support extreme numbers of connections efficiently. Nginx can do this as well, but has to be tuned a lot more to get to the same place, and I'm convinced if both were properly tuned and put in the same environment, HAProxy would come out ahead. That said, if you're at a place where you already need/use Nginx, it might not make sense to use HAProxy if you can re-use your existing Nginx instances. However if you're in the market for a load balancer and Nginx isn't being used already, definitely check out HAProxy. It's pretty simple to configure. EDIT: Another point for HAProxy: Recently, the job system we run (beanstalkd) was slowing to a crawl. I needed to find out where the slowdown was. The easiest way was to route the worker connections through HAProxy and enable logging and the HTTP stats page. I was able to determine the problem was in beanstalkd itself, not our workers (or a network issue). Sure there are a number of debugging proxies out there, but HAProxy was already installed and the logs it emits are the perfect balance of information so you can pick things apart without being overwhelmed. It really helped. Nginx wouldn't have been as useful, and would have taken a much longer time to configure.
- herf 9y agoRAM per connection is quite big with HAProxy, so if you're simply load-balancing lots of connections, something like nginx has a smaller footprint.
- sandGorgon 9y agohaproxy is able to preserve source ip by injecting proxy protocol. nginx is capable of reading proxy protocol but not injecting it. which is why haproxy is perfect as the ingress for your web application. Especially if you are using something like kubernetes that will use an overlay network. unfortunately, the momentum in kubernetes is to leverage nginx as an ingress.
- mbubb 9y ago"unfortunately, the momentum in kubernetes is to leverage nginx as an ingress." Moreso than the GCP loadbalancing or the relatively new AWS transit loadbalancers?
- sandGorgon 9y agoThat is even bigger a problem for those that do bare metal deployment. You are right - k8s is fairly built out as a post cloud load balancer system. However, in the limited work happening on lb increases, it's nginx rather than haproxy.
- manigandham 9y ago> the momentum in kubernetes is to leverage nginx as an ingress. There are many different groups, Nginx in fact has their own ingress controller [1] separate from K8S nginx ingress controller. HAProxy is older and harder to deal with for L7 ingress routing as compared to Nginx. The Istio project (using Envoy) [2] is gaining speed quickly though to become a much better ingress and internal proxy. 1. https://github.com/nginxinc/kubernetes-ingress https://github.com/nginxinc/kubernetes-ingress 2. https://istio.io https://istio.io
- jimjag 9y agoCould I also recommend Apache httpd? Dynamic config; failover; stats; RFC compliance; etc... https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
- rdsubhas 9y agoA load balancing comparison article that says nothing about load balancing. Weird.
- ryanqian 9y agoNone of them have http2 multiplex load balance support yet! None of them.
- manigandham 9y agohttps://envoyproxy.github.io/ https://envoyproxy.github.io/
- manigandham 9y agoBetter options now: Traefik: reverse proxy built in Go with dynamic backends and modern integrations, good replacement for most situations. https://traefik.io/ https://traefik.io/ Envoy: fast C++ L3/L7 proxy with some great features, http/2 support both ways, websockets, grpc, advanced health checking and load balancing, lots of metrics and integrations, my default recommendation now. https://envoyproxy.github.io/ https://envoyproxy.github.io/ + good comparison page: https://envoyproxy.github.io/envoy/intro/comparison.html https://envoyproxy.github.io/envoy/intro/comparison.html
- devmunchies 9y agoI can't take Traefik seriously with that silly gopher on their page. Seems as if they are limiting it to the Go community with that move.
- trjordan 9y agoNo kidding! I'm glad it exists, but Envoy seems like a much more legit / stable choice.
- manigandham 9y agoIt's just a logo, why does it matter? read the docs and see for yourself
- devmunchies 9y agoIts not just any logo. its the Go mascot! If they want to be community agnostic I would recommend a different logo. Its confusing.
- takeda 9y agoIt just screams: "we wrote it in Go because we wanted to write something in Go". You supposed pick the right tools for a project not a project for the tools.
- mdellabitta 9y ago
- AtticusTheGreat 9y agoI only skimmed the article but I agree with the conclusion. I evaluated both HAProxy and Nginx for a high-volume/low-latency load balancer cluster (500k requests/s) and HAProxy simply beat the pants off of Nginx. It has an extensive and well documented set of configuration options, tooling, and reporting, and it performed flawlessly on production (after much toiling). I couldn't ever quite get Nginx to handle the same load without falling apart at the seams.
- trjordan 9y agoI'm glad this has 2016 in the title! It's now 2017, and we have better options. Nginx has been the best routing and traffic serving system for a while now, but it's showing its age. In addition to hiding metrics, HTTP 2 support is difficult, there's no clear path to grpc, and (as mentioned in tfa) there is no way to get stats out of it. Envoy (envoyproxy.github.io) seems like the next thing. Nginx won because of the predominant patterns at the time, and it's still totally fine for serving lots of HTTP traffic. The problem is that if you're thinking microservices or rely on metrics to do your job or have elastic infrastructure, Envoy's feature set is the only real way to handle traffic routing in that world. Disclaimer: I work for turbinelabs.io, and we're moving from nginx to Envoy to take advantage of a lot of these features to power our product. We've put a lot of effort into that decision, and believe it's the right one.
- devmunchies 9y agoI haven't used Envoy yet, but i find it attractive that its made by a company that doesn't make money by selling enterprise features like Nginx+ does. Lyft doesn't care about that, so they have "Advanced Load Balancing" features baked in for free.
- kayoone 9y agoI thin nginx+ is handled really well, a lot of new features find their way into the open source version and for most people that version is all they ever need.
- nailer 9y agoHow can I manually switch an environment over from green to blue? I'm reading https://envoyproxy.github.io/envoy/operations/admin.html https://envoyproxy.github.io/envoy/operations/admin.html but I can't see it.
- trjordan 9y agoYou don't call Envoy to make changes. You either update the config file, or you implement an API server with a specific set of endpoints, then have it serve the updated values back. It's a bit tricky, but entirely doable. https://envoyproxy.github.io/envoy/intro/arch_overview/dynamic_configuration.html https://envoyproxy.github.io/envoy/intro/arch_overview/dynam... (You can do this in nginx, too, and it's similarly annoying. This is a big part of what we do at Turbine Labs -- a UI with a slider between blue and green, and a place to host all those stats, so you don't have to manage it!)
- arca_vorago 9y agoI feel like often a reverse proxy can do the majority of what a load balancer can do just fine. I have been using Hiawatha webserver (gplv2, like HAproxy) which has some great security features and works very well as a reverse proxy. If you haven't check it out, I really like Hugo's work. Security first, good looking code. https://github.com/hsleisink/hiawatha/blob/master/src/rproxy.c https://github.com/hsleisink/hiawatha/blob/master/src/rproxy...
- justaaron 9y agohaproxy is the real deal. - actual proxy server, not a 'freemium' webserver ($2500 for nginx commercial license) - massive capacity (300k/s without breaking a sweat on one process) - ability to config for 'high-availability' (hence the name) with something like keepalived and a 'floating' IP assignment - single config file for all: master it (it's frikkin' yaml, aka 2 spaces indentation) and you master the world...
- duozerk 9y agoIt's also amazingly simple to configure (though to be fair that applies to nginx as well, especially coming from apache) and well documented; its performances are great too.
- felipelemos 9y agoStats on HaProxy is still problematic when nbproc is bigger than 1 (multiprocessing).