Home » Articles

PHP web based application performance tuning

paan 29 May 2008 Articles 1,331 views No CommentPrint This Post Print This Post Email This Post Email This Post

Running FastI’ve been doing a lot of research on this lately, both for my personal sites and also for work. And by using some of the techniques here. You can increase the responsiveness of you web application.

I try to make this as general as possible but mostly they apply to php/mysql development as that is what I have the most experience in.

Databases

First of all. The most common bottlenect of a web application is not the application itself but the database.

Query optimization

Database queries are typically very expensive. But a poorly designed query can take even the best servers to its knees. Which is another reason to hire good programmer (But that’s a rant I’ll save for another time). A profile is an important tool in query optimization. For MySQL there a handy guide to Using the New MySQL Query Profiler MySQL profiler at mysql.com.If you are using postgres then there’s the EXPLAIN command for you to use.

Caching

MySql have a query cache system that you can use. This is particularly usefull for if your application keeps asking for the same data over and over again. For example requesting application settings from database on each page load.  If you are using a language that, unlike PHP, supports application wide variables, for example Java. These queries are better cached into the application memory during application start instead. Just make sure you update the settings if you update the settings in the database. This frees up the cpu from doing sql queries at the expense of some memory overhead. On a busy site this is very much worth it.

memcached is a general purpose memory cache engine but is frequently used as a database cache. Running a big site without memcache is like running up a hill with a backpack full of bricks. You can do it, but you really don’t want to. A lot of people use memcached In conjuction with all any database cache that they already have in the database. memcached is created by the guys behind LiveJournal to help cope with their 20million+ page view, so you don’t need to worry. memcached is one of the big boys here.

Application

These deal with mostly application performance tuning. And not programming for performance.

Caching

On the application front, you can attack the issue of caching from multiple angle. There opcode caching, output caching.The great thing about opensource is that a lot of very smart people that face the problems that you face today already created a solution for it. PHP opcode caching

PHP is an scripting language. Meaning that it is not compiled. When a page is requested. The source is process into machine language and then the instructions are ran. This is done each time the page is requested. You can save a lot of CPU power by caching the generated bytecode that so that the same page need not to compile each time it is access. This is called opcode caching. For this there are a lot of solution but I recommend APC(wiki link). It’s one of the few that still remain active. Opcode caching was popular around PHP 4.1 but somehow most of them fell off the bandwagon and no longer being actively developped, some evolve as other opcode caching projects but just stay dead. Most of the old ones still works with PHP4 but most can’t  even work with PHP5(I think no one should use PHP 4 anyway, a 10 years old language but that is also a rant for another day).

Template Caching

Opcode caching is often accompanied with the used of a tempalate engine to do template caching. Template engine like Smarty can cache all or parts of a rendered web page, or leaving parts uncached. A cached page use more or less the same amount of processing power as serving a plain HTML page. This is really usefull  for a site for data that don’t change to often after they are updated, for example, blog posts. With Smarty your blog posts can be cached while your sidebar with the your widgets that needs fresh up to date data can get the updated data.

Segragate dynamic and static data

Apache is a good webserver. But it is geared towards dynamic content. lighthttpd on the other hand is a webserver that is made for speed. It can’t do many of the fancy things that apache can do. But it can serve static page really really fast. phpClasses used lighthttpd for all their static pages (graphics, javascripts,css, file downloads) to handle the huge amount of traffic for their site. On a smaller scale you can offload all you static content to services like Amazon S3 to host all your static content. That’s the speed of Amazon Enterprise Datacenter at your disposal what else can you ask for.

Summary

Basicly I touched a little on performance for traditional Web application. There are many other thing that you can do to optimize your web application. Newer AJAX heavy websites require the same basic concept but a different approach towards the subject. There are also many other areas that I haven’t touched yet. Like replications of database to spread your database load database and web server clustering. We also haven’t touched on programming techniques to maximize performance.

Application performance is a big subject and hopefully this will give you a small start toward designing high performance PHP web application

Digg!
Rate this:
2.5

Have your say!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>