Speed Up PHP

APC-Cache

Zend engine needs 4 steps to run a PHP script
1. Read PHP code from file into memory
2. Lexing : convert to lexicons that can form syntax
3. Parsing and compiling : parsing lexicons into opcodes and validate language syntax
4. Executing: execute opcodes

APC mainly hijacks step 3. Instead of having Zend to do step 1,2,3 again and again, APC actually stores the opcodes into shared memory, then copies opcodes into execution process so Zend can actually execute the opcodes. Classes and functions tables are also stored into shared memory because that’s what zend_compile_file generated. Link

PROXY CACHING

A common tool employed by site owners and administrators is a ‘reverse proxy cache’ that sits between page requests made by a web browser and the web application. It intercepts requests and renders copies of pages straight from the cache, thus providing a noticeable speed boost.
There are several major proxy cache options available for self-install or as ‘Software as a Service’. (We are ignoring cloud hosting providers who typically package everything you might need into a self-contained web stack.)
Popular proxy cache options include:
Varnish (see below)
Squid;
Nginx (a combined web server and proxy cache).

By default Varnish will not cache content from the backend that sets cookies. Similarly, if the client sends a cookie, it will bypass Varnish straight to the backend.  We don’t want varnish to cache dynamic content. We define dynamic  as:

  • if the content depends on the user (through Cookies for example), it should not be cached as you’ll have lots of different contents and your HIT/MISS ration will not be high since every user has a different content.
  • if the content changes in time, you can always cache the content a little, for example a few seconds.
  • if the content changes in time, a better option is to separate the “static content” from the dynamic one. You may cache the page template and do ajax calls to refresh the content. You may also use esi, it’s an old technology but it lets you specify differents “zones” in your pages, each having its cache duration.
  • you can benefit from IMS requests. Telling the backend to send the response body only if it has changed since the last request can save you lots of processing time. I think varnish does this from version 4

 

Squid – Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-requested web pages. Squid has extensive access controls and makes a great server accelerator. It runs on most available operating systems, including Windows and is licensed under the GNU GPL.

The most fundamental difference between Squid and Varnish is that Squid is forward proxy that can be a configured as a reverse proxy whereas Varnish is built from the ground up to be a reverse proxy.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s