Current time: 12-16-2017, 09:22 AM Hello There, Guest! (LoginRegister)

Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimizing nginx Time to First Byte
04-06-2013, 11:41 PM
Post: #1
Optimizing nginx Time to First Byte
Hey guys,

Excellent resource this site is. I've used it to really optimize page loading times but have come across an issue and am a little bit stumped.

Serving static content with nginx can be improved, but I don't know where to start. Images are being served directly by nginx, yet I can't get TTFB below ~120ms. The small delay seems to affect both dynamic (php-fpm) content or static content (pure nginx).

Hardware is top notch with 24 cores (E5-2620) with 64GB of DDR3 served from an Intel SSD -- all brand new and in working order. mySQL databases are all in memory.

I'm convinced that the issue is with nginx or some OS-related issues nginx might be running into.

What I've tried with virtually no difference observed:
  • Disabling gzip compression
  • Changing gzip compression levels
  • Optimizing sysctl
  • Disabled logging, both error_log and access_log
  • worker_processes & worker_connections
  • Nginx microcaching
  • keepalive_timeout / keepalive_requests

I don't believe there should really be much delay in serving content, especially when keepalive is on.

Here's a test representing average load times for an example page: http://www.webpagetest.org/result/130406_H5_FNB/
Find all posts by this user
Quote this message in a reply
04-08-2013, 12:21 AM
Post: #2
RE: Optimizing nginx Time to First Byte
Hey,

I am on nginx also but on the lower end and with slower ttfb, (220ms). You have to consider how you built nginx, what did you include in there as well as version numbers.

Also you have a lot to play with regarding worker processes and connections. Everyone seems to have a different formula. Since you got a lot of room to test things out I would build a script that would try out a few thousand variations of numbers of processes and connections , change the number, save, restart nginx, connect somewhere you can measure ttfb a dozen times, save times along with the settings, and restart. By the morning you might actually have some data.

When you dealing with tiny fractions of a second, you have to check your linux installation as well. Anything removable, don't need? etc..

But the test script sounds like a winner to me. Especially since you can rerun the same numbers while changing another variable. I think its the easiest way to flush something out. Perhaps figuring out how close you can get to 0sec is a good idea too. Find some benchmarks, though I doubt they are out there, and too many braggarts to go by their word. lol

Good luck,

Turtle
Find all posts by this user
Quote this message in a reply
04-09-2013, 01:44 AM
Post: #3
RE: Optimizing nginx Time to First Byte
(04-06-2013 11:41 PM)Andem Wrote:  Hey guys,

Excellent resource this site is. I've used it to really optimize page loading times but have come across an issue and am a little bit stumped.

Serving static content with nginx can be improved, but I don't know where to start. Images are being served directly by nginx, yet I can't get TTFB below ~120ms. The small delay seems to affect both dynamic (php-fpm) content or static content (pure nginx).

Hardware is top notch with 24 cores (E5-2620) with 64GB of DDR3 served from an Intel SSD -- all brand new and in working order. mySQL databases are all in memory.

I'm convinced that the issue is with nginx or some OS-related issues nginx might be running into.

What I've tried with virtually no difference observed:
  • Disabling gzip compression
  • Changing gzip compression levels
  • Optimizing sysctl
  • Disabled logging, both error_log and access_log
  • worker_processes & worker_connections
  • Nginx microcaching
  • keepalive_timeout / keepalive_requests

I don't believe there should really be much delay in serving content, especially when keepalive is on.

Here's a test representing average load times for an example page: http://www.webpagetest.org/result/130406_H5_FNB/

Remember that the overall TTFB as measured from WebPagetest also includes the round-trip time on the Internet from the test machine to your server (and for the connection profile - in the test case Cable adds 28ms).

The socket connect time is usually a good baseline to use for estimating the network-level RTT and it looks like in the test you are seeing ~110ms of network time just getting from WPT to your server.

Your actual server-side latencies for the static requests are closer to 15-40ms.

The only way to reduce the network latencies is to distribute your static content closer to the users which is exactly why CDN's exist (real CDN's, not cdn.lo4d.com which just appears to be a static file CNAME that is served from the same server or location).
Visit this user's website Find all posts by this user
Quote this message in a reply
04-10-2013, 07:55 AM (This post was last modified: 04-10-2013 08:01 AM by Andem.)
Post: #4
RE: Optimizing nginx Time to First Byte
(04-08-2013 12:21 AM)Sleepy_Old_Turtle Wrote:  But the test script sounds like a winner to me. Especially since you can rerun the same numbers while changing another variable.

Thanks for your suggest Turtle. I've been taking the approach, but I suppose more automation might be in order.

(04-08-2013 12:21 AM)Sleepy_Old_Turtle Wrote:  When you dealing with tiny fractions of a second, you have to check your linux installation as well.

It's pretty bare bones with the exception of apf, nginx, mysql, etc. I'm going to give it a look other though.

(04-09-2013 01:44 AM)pmeenan Wrote:  Your actual server-side latencies for the static requests are closer to 15-40ms.

The only way to reduce the network latencies is to distribute your static content closer to the users which is exactly why CDN's exist (real CDN's, not cdn.lo4d.com which just appears to be a static file CNAME that is served from the same server or location).

Good point about the real latencies. That's exactly what I'm after improving and what I'd like to get down to. For image requests, I don't really see the point of using a CDN provider unless it can really make a difference. I didn't see any improvements in the past (hence the cdn.* addresses).
Find all posts by this user
Quote this message in a reply
04-16-2013, 03:22 AM
Post: #5
RE: Optimizing nginx Time to First Byte
If you have visitors from around the world, a CDN can get rid of the network latencies from traversing the globe. That's usually in the 100ms range from Europe to the US, 70ms between the US coasts and as much as 200-300ms between Asia and the US. That pretty much dwarfs your server-side latencies and it's not something you will be able to test without actually distributing the content.

That said, it only really matters if you have visitors from further away. If all of your visitors are from within a few thousand miles of your server then it's probably a wash.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)