Jaslabs: High performance Software

High Performance Software

5 tips for creating high performance web apps

By Justin Silverton

The following are five tips that can help with peformance when writing php (some can be applied to other languages) applications.

1) use multi resultset queries to your database rather than many small ones

Look through your database code to see if you have requests that go to the database more than once. Each of these will decrease the number of requests per second your application can serve. By returning multiple resultsets in a single database request, you can not only cut the total time spent communicating with the database but also make your app more scalable by cutting down on the work the database server is doing to manage requests.

2) page/object caching

Templates caching (a previous article I wrote describes some template engines here)

PHP Object caching

  • ion cube (commercial) - This one is unique because you don’t have to have server extensions installed.
  • Alternative PHP cache (free) - will be included with PHP 6.
  • Turck MMCache (free) - includes an encoder and loader, so you can distribute your scripts without the source

Database object caching

memcached - used by livejournal and slashdot.org.

3) gzip compression

Enabling this may increase CPU utilization (because it takes more processing power to gzip a file) but it will decrease the number bytes sent from you server, save your bandwidth, and generally make you site faster to your visitors.

to enable gzip compression, add the following to your php.ini:

zlib.output_compression = 1 (requires php 4.0.5 or above)
zlib.output_compression_level = X (X=0 through 7. The higher the number, the more the output will be compressed. Be careful when choosing higher numbers as it will take much more processing power) (requires PHP 4.3.0 or above)

4) tune your web server

A large list of apache (version 2.0) performance tips can be found here

5) Don’t save performance testing for the end of the project

If you save performance testing until the end of the project, it may already be too late and take too much time to make the necessary architectural changes. Tests can be performed on individual pieces of your application or the application as a whole.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati

10 Comments so far

  1. Oscar February 8th, 2007 4:58 pm

    TurkMMCache hasn’t been in development for years, it’s been replaced by eAccelerator.

  2. Oscar February 8th, 2007 4:59 pm

    If you’re on apache2, use mod_deflate instead and you can also compress css/js/regular html files

  3. Arikon February 8th, 2007 5:13 pm

    Can you explain, what do you mean about queries, returning multiple resultsets? Can you show an example?

  4. Justin Silverton February 8th, 2007 5:43 pm

    Arikon,

    Here is an example:

    foreach ($users as $user)
    {
    $oUser = select * from user_table where userID = $users->ID;
    }

    instead of doing the above, you could build one large select statement grabbing all the users info that you need, then looking through the data.

    iE: SELECT * from user_table WHERE userid=’1′ AND userID=’2′

  5. Mitchell February 8th, 2007 6:12 pm

    When you say “Database object caching” what exactly do you mean? We’ve been using memcached for a years now, but whenever I try to cache a database object with the connection Id, it fails because the resource Id it had previously connected with no longer exists on the next page load. Have you found a way around this?

  6. […] Jaslabs: High performance php » 5 tips for creating high performance web apps (tags: php tips optimization) […]

  7. […] Sveiki, šią savaitę sulaukėm 4 straipsnių, du iš jų vėl parašė ernetas: “Instaliuojame CUPS ir HP LaserJet 10xx spausdintuvą Gentoo sistemoje” ir “Instaliuojame SAMBA Gentoo sistemoje“. Taip pat MaxMeOut parašė pirmąjį straipsnį Java tema “float ir double tipo kintamųjų formatavimas Java kalboje“. Na ir sulaukėme medučio “PHP saugumo nustatymai - 1 dalis” tęsinio “PHP saugumo nustatymai - 2 dalis“. Dėkui jums visiems Skaitiniai šiam savaitgaliui: For the Love of the Web, Please Use Full Content Feeds! 5 tips for creating high performance web apps (tikriausiai jau daugeliui labai žinomi tips`ai, bet prisiminti kai kuriems verta ) Parse html with preg_match_all Pure CSS Popups Boost Ajax performance using local storage […]

  8. Andrew Warner February 10th, 2007 12:42 pm

    SELECT * from user_table WHERE userid=’1′ AND userID=’2′

    ….will get you a zero result set every time.

    try:

    SELECT * from user_table WHERE userid=’1′ OR userID=’2′
    …or:
    SELECT * from user_table WHERE userid in (’1′,’2′)

  9. Justin Silverton February 10th, 2007 1:20 pm

    Andrew is correct. I wrote the statement with AND while I was working on one of my own projects (which involves many sql statements).

  10. John Cage August 30th, 2007 7:32 am

    Just spotted with this nice article and I have one comment regarding encoding - ion cube is not unique in its features
    worth to take a look here:

    http://www.sourceguardian.com

    Also much cheaper encoder with encoding on byte code level:

    http://www.phpshield.com

    I’m considering to purchase one of them.

Leave a reply