Flaws in PHP sessions – session_start causes site to run slowly

PHP is a versatile language that gives the programmer many tools to get the job done. One such tool is sessions, but in some cases sessions can have a detrimental effect on your site’s performance.

Since switching from a Windows server to a UNIX server, I noticed a huge increase in overall site performance – but sometimes, with no noticeable pattern, a page would take a minute to load against the average which was under a second.

After messing with the scripts and painstakingly commenting out sections of code, I discovered that the problem lie quietly in the PHP sessions – specifically the session_start function. Perhaps it has something to do with disk operations and UNIX operating systems, whatever it was it was causing some frustrating performance problems.

I decided to alter rid of all session functions and create my own version that interacted with a MySQL HEAP table – that’s a table stored in the server’s memory instead of on disk. Instantly the site performance was noticeable better, pages loaded more instantaneously.

Looking into the PHP sessions further I discovered another flaw – if your visitor was to load several pages at once, each page would pause until the previous page had finished loading. This is because no two scripts can access the same session at any given time – session access is serial, not parallel. If a page takes one minute to process and another page takes a second to process, but the heavier page is loaded first, the reader will have to wait for heavier page to finished processing because they can see the page that would have taken a second to process. This increases the queues to the web server and doesn’t do much for the user’s experience.

It may however be helpful in some instances, for example if you were altering a virtual tree structure you wouldn’t want the functions to collide in operation. For what I had been using sessions for, it wasn’t helpful at all.

I’m going to make a modular version of the alternative session code and publish it here, so come back later if you want a copy!

2 Responses to “Flaws in PHP sessions – session_start causes site to run slowly”

  1. Mark Says:

    I think I will do some tests to see if it does the same thing with Windows. Personally, I think it should be an option in the php.ini file – so you can make it “wait” for the other process(es) first, before continuing!

    Marks last blog post..Windows Vista Start Menu updates not shown

    [Reply]

  2. Free Webmaster Tools Says:

    Thank you, I really need to know this. Can I request you post something on Aweber? I have problems using aweber and need some simple orientation. Hopefully I will be able to use the Aweber more effectively

    [Reply]

Leave a Reply