mercredi 9 mai 2018

Why using nginx as the load balancer for your production is really, really not a good idea.

As a freelance developer, I've stumbled upon this situation in various projects, and I want to publicly explain once and for all why the free version of nginx is not ok to use for production load balancing (LB). Here is why.

Let's first note that "health checks"(HC) are *not* available in the free version of nginx, only in the paid version. Using a LB without HC is really not ok for production, and I will now explain why.

Let's suppose you use nginx as a LB to distribute requests between 2 backends, with default parameters for the LB, typically max_fails=1 fail_timeout=10s.

Now let's suppose one of the backends becomes unresponsive. Typically nothing can connect to it, even your SSH client: trying to connect to it takes forever every time, and you eventually get a timeout somehow. This is not a fantasy, this happened to me yesterday in production. These things happen with servers. And suppose the unresponsive backend stays like this for several hours. What happens?

The LB nginx will consider the unresponsive backend as unavailable and stop sending requests to it. It will send all requests to the healthy backend only. Wait...  not really! Every 10s (fail_timeout) it will test whether the unresponsive backend is "back online" by sending a *real* request (from a real user of your app) to the unresponsive backend and see how it goes. It has to use a real request for this purpose, because HC, which are the exact tool for this job, are not available in the free version of nginx.

The LB nginx will wait 60 seconds (default value of proxy_connect_timeout) before deciding that the unresponsive backend will not be available to answer the real request, and then send this request to the healthy backend and eventually answer the user. From the point of view of your user, the page will load ok (HTTP code 200 OK) but will have taken 60 seconds to load!

They are ways to improve this, and I am working on them in my current project, but frankly it is is clumsy and just a bad solution to a real problem. A good solution would be to use the paying version of Nginx which offers HC, or just to use the right tool for the job which is IMHO HAProxy.

TL;DR If you are ok with having, every 10 seconds, one user wait 60 seconds for your app to load, during the several hours when one of your backend is unresponsive, be my guest and use the free version of nginx as a load balancer in production. Otherwise don't.

3 commentaires:

  1. What about https://github.com/yaoweibin/nginx_upstream_check_module ?

    RépondreSupprimer
  2. Very good point! Thanks for the link. We are using nginx 1.4.6-1ubuntu3.7 in production (Ubuntu 14.04 LTS) and unfortunately there seem to be no patch for this version.

    RépondreSupprimer
  3. And by the way, having to apply a custom patch and recompile nginx from source instead of using the regular distribution package just to get a decent load balancing is not a good sign... it really means nginx is not the right tool for the job. I would rather use Haproxy.

    By the way on my current project, I will give a last chance to nginx by tuning the variables [proxy_connect_timeout, proxy_send_timeout, proxy_read_timeout] down from 60s to 3 seconds or so, with the idea that while a backend is unresponsive, at least the unfortunate users will wait only 3s and not 60s for the page to load. IMHO the default value of 60s is shockingly high, there must be a practical reason for it but I do not see it.

    RépondreSupprimer