<div dir="auto">Hi Maxim,<div dir="auto"><br></div><div dir="auto">Thank you for that long explanation. It is quite clear and helps me a lot.</div><div dir="auto"><br></div><div dir="auto">In my case upstream servers are vert.x servers, using event loop pattern so i won't have the pb induced by thread-per-request servers like Apache you mentionned.</div><div dir="auto"><br></div><div dir="auto">I understood that all keepalive connections could end up being assigned to the same server of the group occasionnally, no even distribution guarantee. In my case I think that I can increase much the number of keepalive then. I have 12 vertx servers in the group and implemented the *2 rule so it means max 24 keepalive connections maintained to the same server, which is quite low.</div><div dir="auto"><br></div><div dir="auto">Thanks agin, have a great day.</div><div dir="auto"><br></div><div dir="auto">Sébastien.</div><div dir="auto"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le lun. 18 mars 2024, 23:50, Maxim Dounin <<a href="mailto:mdounin@mdounin.ru">mdounin@mdounin.ru</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello!<br>
<br>
On Mon, Mar 18, 2024 at 09:08:18PM +0100, Sébastien Rebecchi wrote:<br>
<br>
> Hello,<br>
> <br>
> What is the good rule of thumbs for setting the number of keepalive<br>
> connections to an upstream group?<br>
> <br>
> 1. <a href="https://www.nginx.com/blog/performance-tuning-tips-tricks/" rel="noreferrer noreferrer" target="_blank">https://www.nginx.com/blog/performance-tuning-tips-tricks/</a><br>
> in this blog, the writer seems to recommend a constant value of 128, no<br>
> real explanation why it would fit whatever the number of servers in the<br>
> upstream<br>
> <br>
> 2. <a href="https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive" rel="noreferrer noreferrer" target="_blank">https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive</a><br>
> the upstream module doc seems to recommend a rule like 16 times the number<br>
> of servers in the upstream, as we have two examples with respectively<br>
> keepalive 32 for 2 upstream servers and keepalive 16 for 1 upstream server<br>
> <br>
> 3.<br>
> <a href="https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#no-keepalives" rel="noreferrer noreferrer" target="_blank">https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#no-keepalives</a><br>
> in this blog, the writer recommends a rule of 2 times the number of servers<br>
> in the upstream<br>
> <br>
> I used to follow rule of item 3 as it comes with a somewhat good<br>
> explanation, but it does not seem to be widely accepted.<br>
> <br>
> What could explain such a divergence between several sources? What would<br>
> you recommend please?<br>
<br>
There is no strict rule, and actual numbers to use depend heavily <br>
on the particular configuration, expected load, the number of <br>
upstream servers, and the maximum number of connections each of <br>
the upstream servers can maintain.<br>
<br>
The most notable limitation to keep in mind is that when you are <br>
configuring keepalive connections with upstream servers using <br>
process-per-connection model, it might be important to keep total <br>
number of cached connections below the maximum number of <br>
keepalive connections the upstream server can maintain, or you <br>
might end up with non-responsive upstream server.<br>
<br>
For example, consider an upstream server, such as Apache with <br>
prefork MPM, which is configured to run 256 worker processes <br>
(MaxRequestWorkers 256, which is the default and will typically <br>
require more than 8G of memory).<br>
<br>
As long as nginx is configured with "keepalive 32;" and 16 worker <br>
processes, this can easily consume all the Apache workers for <br>
keepalive connections, and you'll end up with occasional slow <br>
upstream responses simply because all the upstream worker <br>
processes are occupied by cached connections from other nginx <br>
worker processes, even without any real load.<br>
<br>
That is, it is a good idea to keep (worker_processes * keepalive) <br>
below the maximum number of connections the upstream server can <br>
maintain.<br>
<br>
Note well that keepalive cache size is configured for all servers, <br>
but it might end up keeping connection to just one upstream <br>
server. As such, it might be important to choose the keepalive <br>
connections cache size based on the capacity of each upstream <br>
server individually, and not their total capacity.<br>
<br>
On the other hand, configuring cache which cannot hold at least 1 <br>
connection to each upstream server hardly makes sense, as it can <br>
easily end up in cached connections being closed after each <br>
request and never reused.<br>
<br>
Hope this helps.<br>
<br>
-- <br>
Maxim Dounin<br>
<a href="http://mdounin.ru/" rel="noreferrer noreferrer" target="_blank">http://mdounin.ru/</a><br>
-- <br>
nginx mailing list<br>
<a href="mailto:nginx@freenginx.org" target="_blank" rel="noreferrer">nginx@freenginx.org</a><br>
<a href="https://freenginx.org/mailman/listinfo/nginx" rel="noreferrer noreferrer" target="_blank">https://freenginx.org/mailman/listinfo/nginx</a><br>
</blockquote></div>