<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection">
<div dir="auto"><span style="font-family:Helvetica;font-size: 12px">Hey,</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px"> </span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">We are using NGINX as a proxy / caching layer for a backend application. Our backend has a relatively slow response time, ranging between the 100 to 300ms. We want the NGINX proxy to be as speedy as possible, to do this we have implemented the following logic:</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px"> </span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">- Cache all responses for 5 mins (based on cache control headers)</span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">- Use stale cache for error's on the backend</span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">- Do a background update for stale cache</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px"> </span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">The last part has an issue, namely if a first request reaches nginx, it will trigger a background request, but other requests for the same resource will be locked until this background request is finished instead of still returning the stale cache that is available. This is caused by the fact that there is a keepalive on the connection, which locks all subsequent requests until the background request is finished.</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px"> </span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">The issue that we are facing in this situation is that the locking is very long, namely 500ms hardcoded. I think it is caused by this:</span><span style="font-size: 12px"><br /></span><a style="font-family:Helvetica;font-size: 12px" href="https://github.com/nginx/nginx/blob/master/src/core/ngx_connection.c#L703" target="_blank">https://github.com/nginx/nginx/blob/master/src/core/ngx_connection.c#L703</a><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px"> </span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">This means that our relatively slow backend of 100-200ms actually gets worse than better.</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px"> </span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">Is it an option to make this 500ms a configurable setting instead of 500ms? Are there any downsides to making this 500ms lower? I'd be willing to see if we can contribute this.</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px"> </span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">Another option that I'd tried is to set the keepalive to 0, so that every request is a new connection. In small amounts of requests this actually seemed to solve the issue, but the moment that we went to a real life situation, this degraded the performance massively, so we had to revert this</span><span style="font-size: 12px"><br /></span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px"> </span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">Greets,</span><span style="font-size: 12px"><br /></span><span style="font-family:Helvetica;font-size: 12px">Roy</span><span style="font-size: 12px"><br /></span></div>
</div>
<div name="messageSignatureSection"><br />
<br /></div>
</body>
</html>