Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output Caching .PHP files with IIS8 - Time To First Byte

I have a VPS with 4 cores and 5 GB of RAM with Windows Server 2012 and IIS8. I am running a WordPress site on it with MySQL as an InnoDB database, MySQL has been given roughly 70% of my available RAM to cache/buffer which works wonderful. I have Wincache enabled, Output Caching enabled for basically everything BUT .php files. My Time To First Byte is at 1400-1800 ms which is way too slow in my opinion.

I enabled Output Caching for all PHP files which decreased it to 8ms. Perfect! Or so I thought... Of course because it is now caching the entire PHP files it won't load any subsequent pages. This is because WordPress loads subsequent pages as a query, not as a new file.

I had to turn off Output Caching for PHP files completely but cannot seem to find any other solution in decreasing the Time To First Byte/Response Time. Does anyone here have any good suggestions using either Wincache or Output Caching to achieve the ~8ms results I had before?

Any out-of-the-box suggestions also highly appreciated!

like image 863
njs Avatar asked May 25 '14 17:05

njs


People also ask

What is output caching in IIS?

The IIS output caching feature targets semi-dynamic content. It lets you cache static responses for dynamic requests and increase scalability. Note that not every dynamic page can take advantage of the output cache.

How do I configure cache control in IIS?

Start the application named: IIS Manager. On the IIS Manager application, select your website. On the right part of the screen, access the option named: HTTP Response Headers.


1 Answers

After many headaches I have finally figured out where a large chunk of this slow TTFB came from. PHP/IIS8's MySQL driver MySQLnd is at fault. Since the AAAA (IPv6) record takes precedence over the A (IPv4) record, 'localhost' will return an IPv6 address. And as the MySQL driver (mysqlnd) in PHP cannot handle IPv6 addresses it first has to time out before it tries to connect over IPv4.

I cut my TTFB from 1400-1800 ms to a mere 200-400 ms. Already tons better. The rest I can probably attribute to .css/.js calls and other things.

To solve the above problem: instead of using 'localhost' use either 127.0.0.1 or gethostbyname('localhost') as this function will always return an IPv4 address. The latter being the better solution as you might want to connect to an external MySQL DB one day by using a hostname.

Hope this will help other people with TTFB issues!

like image 168
njs Avatar answered Sep 26 '22 01:09

njs