“HAProxy vs Nginx, which one should I choose??” – I get asked this question almost every day and while I’m not a fan of “vs” style posts, I’ll try to express my opinion on the topic that may or may not help you to select a product for your particular use case.
Let me start by saying both of the products are really good. Both support Level 4 and 7 load balancing, so it will come down to personal preferences and some specific features that I’ll try to describe below.
I’ll tell you even more – both could be used together to complement each other! How about that now?!
If you need webserver capabilities, meaning that you want to serve static files for your app or you planning to run dynamic apps that rely on fastcgi protocol (php-fpm for example), then you may want to stick with Nginx as HAProxy will not help you with that.
Nginx provides very flexible caching capabilities with lots of configuration options. It’s sort of like Varnish (but not as sophisticated). This point alone warrants separate post, so we will just move forward for now.
It used to be that Nginx didn’t have Level 4 loadbalancer capabilities, but both open source and commercial editions have it now.
There are two main disadvantages for the opensource version of Nginx compared with HAProxy:
- very limited stats – this is actually pretty big deal when it comes to the load balancers. There is only few metrics that Nginx exposes, that a barely usable.. There is no breakdown for each server. There is no way to get basic response code stats like number of 3xx, 4xx, 5xx (we had to send data from our logs aggregator and then to datadog to extract this information). It’s just a sad picture to be honest. It seems like Nginx does it intentionally to sell more commercial licenses.. 🙁
Don’t get too sad though, there is a light at the end of the tunnel for all us who wants better stats for the free Nginx edition. This light is called Nginx virtual host traffic status module by YoungJoo.Kim.
The project looks very promising and active at the time of writing, it aims at exposing same data as commercial version! I’ve tested it for some of my own projects and will start offering it to my clients very soon. Yay! (if you use it in prod setups, please share your thoughts in comments below, especially when it comes to the best ways of sending metrics to systems like Datadog).
no support for server
drainoption – WTF is server drain command?! Well, it may seem like some small feature but it’s actually quite useful or even critical for zero downtime deployments.
If we run PHP-FPM with opcache for example, we need to reload it after each release. Even though we sending soft reload signal PHP-FPM would abort currently running connections regardless of that ( the clients will see awesome 5xx responses, unless you have multiple servers with retries ).
There is neat way to avoid it by having multiple PHP-FPM processes running on the separate ports and then doing green / blue deployments, but this setup relies on the
draincommand as we want to start draining connection on one port while switching traffic to the new one. For this particular case I may use HAProxy behind Nginx just to have an option to drain connections for Free. Maybe I’ll write about this technique in the future if someone is interested.
Main advantage compared to Nginx is simplicity and single purpose of load balancing traffic which means stability. There is just less things to break or misconfigure. It just works.
HAProxy stats are great out of the box, easy to hook to monitoring services like Datadog. Plus there is our famous
drain option 🙂
Then again there is no webserver capabilities which could be either advantage or disadvantage, depending on the angle and particular situation. If you don’t need a webserver then it’s great! If you do – not so great and you need to add additional piece to your infrastructure.
So which one?
It’s really hard to pick HAProxy vs Nginx winner here to be honest. Yes, both products have similar load balancing features, but Nginx extends the reach a bit further which I personally like. Everything boils down to particular case and what you are most comfortable with.
If you you already have Nginx in your project, familiar with config syntax, then there might be no need to introduce HAProxy. If you just want to reliably balance traffic and have no need to run webservers, then HAProxy could be a good choice.
I also have to add that I commonly see both HAProxy and Nginx employed together (even on the same host). This allows for example to take one Nginx instance down, while HAProxy continues to proxy requests from other hosts.
Hope it helps a bit.. Feel free to add your own points in the section below.
Want faster website?
I'm Sergey Khaladzinski (@gansbrest). I regularly share tips and tricks to improve websites performance. My main focus at the moment is monitoring, servers and backend performance. If you liked this article, join my list to get updates from me in the future.