number of keepalive connections to an upstream

Igor Ippolitov iippolitov at nginx.com
Wed Mar 20 12:26:30 UTC 2024


Sébastien,

Keepalive in an upstream defines a pool of connections attached to that 
upstream.
The main purpose of the pool is to reduce the amount of new TCP 
connections: the fewer new connections you open the less load you have.

Any specific recommendation will fail in some case. So the real value is 
dictated by your load and your upstream applications. Consider the 
following when choosing a value:

If the pool is smaller than the number of servers in an upstream group - 
nginx may end up closing connections to an upstream every time.
So the common sense is to have keepalive pool at least as big as there 
are servers in a group (10 servers dictate having a pool of at least 10 
connections, 1 per server).

If you have a low count of lightweight upstream processes (say, it's 
another nginx) and a high count of concurrent requests - the value for 
keepalive can easily be in thousands.
On the other hand, if you have 10 concurrent connections and 5 servers 
in an upstream something like "15" would be a good choice.

Be careful setting high values though: in opensource version keepalive 
is set per worker. So if you have 'keepalive 10' and 16 workers you will 
end up with 160 connections from nginx to an upstream.

I hope this answers your question.

Kind regards,
Igor

On 18/03/2024 13:41, Sébastien Rebecchi wrote:
> Hello,
>
> What is the good rule of thumbs for setting the number of keepalive 
> connections to an upstream group?
>
> 1. https://www.nginx.com/blog/performance-tuning-tips-tricks/
> in this blog, the writer seems to recommend a constant value of 128, 
> no real explanation why it would fit whatever the number of servers in 
> the upstream
>
> 2. https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
> the upstream module doc seems to recommend a rule like 16 times the 
> number of servers in the upstream, as we have two examples with 
> respectively keepalive 32 for 2 upstream servers and keepalive 16 for 
> 1 upstream server
>
> 3. 
> https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#no-keepalives
> in this blog, the writer recommends a rule of 2 times the number of 
> servers in the upstream
>
> I used to follow rule of item 3 as it comes with a somewhat good 
> explanation, but it does not seem to be largely accepted.
>
> What could explain such a divergence between several sources? What 
> would you recommend please?
>
> Regards,
>
> Sébastien.
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> https://mailman.nginx.org/mailman/listinfo/nginx



More information about the nginx mailing list