- Jun 26, 2013
- by Brad Litwin
With many websites there will come a point where optimizing PHP code, adding memory to a server and tweaking configuration files can’t provide enough optimization to keep the site running smoothly. Even though server technologies are improving rapidly, all of the code processing, compiling and database queries that happen when serving up web content can take their toll on the available resources of the server. Eventually with a growing audience and without the proper solutions in place, the speed at which the server is able to deliver content begins to slow down.
This article is Part 1 of a multi-part series sponsored by A2 Hosting, highlighting different web server optimization tools and techniques. These articles are geared towards users of VPS, Cloud and Dedicated servers.
Part 1 – What’s the Cache?
In the endless pursuit of a speedy website, there are many tools that can be used to gain a boost in the performance department. Caching can help speed things up by eliminating some of the duplicate processes that occur when serving up web pages multiple times over a period of time.
Whenever a user types a web address into their browser, it sets off a chain reaction of events that process and put all of the data together to show the website in the viewer’s browser window.
The first thing that happens is the web server has to interpret all of the PHP code for that particular web page. If the site is hosting using a framework such as WordPress, the web server will load many different files into memory to gather all the code it needs to display the page. These files will include a configuration file, library files, templates, plugins, etc. A single web page may require resources from hundreds of files on the web server. The server must read the PHP in it’s human-readable code from each of those files, and then compile it into a machine-readable form of code. Once that process is complete, the server executes the compiled code.
In the course of running that compiled code, the web server may be directed to go and grab some information from the MySQL server. That could mean a handful or even a few hundred queries to the MySQL server to gather all of the data needed to display the web page. Once all the data is collected, then the web server takes that data and processes it with the PHP code to output the final HTML code of the page and send it off to the web browser..
Here’s where caching makes a huge difference. When caching is enabled, once all of those processes are complete the web server adds an extra step – it takes the finished output after all of the queries and compiling, and stores it. The next time a request for the same page is received by the web server, instead of going through the process of loading, compiling, querying the database, and interpreting all of the data to create the output, the server used the saved, completed output from the last time the page was requested, and sends that to the web browser instead.
Three Types of Caching Engines
Caching can happen in many different ways. The most common is an opcode caching, also known as a PHP Accelerator. This type of caching saves all of the compiled code for the requested PHP files. Instead of going through the process of compiling each of the PHP scripts for every request, opcode caching engines compile the scripts once, and then save the compiled versions of the scripts and use those for future requests.
The benefit of opcode caching is that it still allows highly-dynamic websites to maintain their ability to be dynamic. That is because the scripts are still pulling data from the database every time they run, giving the end user the most recent data available. That is also the downside to these types of caching engines. The database (and other resources) are still be accesses for every page view, so there is still a time and resource hit to the server while those queries are being conducted.
The next type of caching is object/data caching. These caches look at the specific bits of data that a PHP script uses, and caches just that data – preventing the script from having to query the database every time the page is loaded. For single-server instances, APC, Zend and other opcode caches may also handle object/data caching alongside their ability to opcode cache.
The benefit of object/data caching, is that is prevents the numerous database calls for a particular page from happening every time the page is requested. The downside is when a site is fairly dynamic, the data stored in the cache may be old or outdated. Caching engines such as these typically have a specific timeframe set in its configuration that dictates exactly how long data will be stored in the cache before it is flushed and refreshed.
A proxy cache is an intermediary caching engine. It sits between the user and the web server and intercepts the request for a web page and determines whether or not it has a stored version of the page available to offer the user. This saves even more load from the web server in that if the proxy has a cached copy of the requested page, it sends it back to the user immediately and the request is never even presented to the web server.
The benefit of a proxy cache is that is removes a great deal of burden from the web server itself. It can also be set up as a completely separate server from the web server, adding an additional layer of security and preventing hacks and malicious users from gaining access to the web server itself.
The downside of a proxy cache is also that it adds an additional layer between the user and the web server. If not setup properly, proxy caches can severely limit the logging and data capture abilities of the the web server. Additionally for highly-dynamic sites, and sites where users are required to login, an improperly set up proxy cache could inadvertently expose private user data to other users.
In the next installment, we’ll take a look at Content Delivery Networks and how they both speed up your site as well as add a layer of security against hacks.