Slow parallel requests

01/13/2012

Having a look at performance problems with parallel requests - maybe it's just the session-handling.

Ever wondered, why the "seems to be parallel" requests of your AJAX-based webapp seem to be loading slowly and in sequence instead of in parallel?
Well, it might be that you'll have to dive into the details of your applications architecture.

A first impression on how the requests are fired and served may be created by using the popular Firebug extension for Firefox. In the network-tab, you are able to filter and inspect the "XHR" requests. If the diagram rather shows a waterfall than a list of parallel threads, it's time to get ready for action.

The awesome xdebug extension for php provides a profiler creating a dump of each request handled by PHP (http://xdebug.org/docs/profiler). In combination with a visual analyzer like KCachegrind, analysing those traces is an easy one.

You may get something like the following picture:



What becomes clearly obvious: The generation-time consists of nearly 50% being spent in phps' session_start().
And this is, where your multi-request architecture gets relevant.
Whenever a session is opened by session_start, a lock is created on the session. This is necessary to avoid having multiple request writing to the same session. What value would you expect, of you read a value from the session? You can't be sure.
And that's why php keeps the session locked until either the request is finished or session_write_close() is called before. As soon as the session is closed the lock is released and another thread may take the up the session-data, lock it by its own and start processing.

As soon as session_write_close() was called, reading from the session will still be possible whereas saving values to the session will fail.

So, whenever you want to process threads aka. requests in parallel, make sure to close the session as early as possible. Only then it's possible for other threads to take up the session and start their own processing routines.

Filed under: TOP-News en TOP-News de Kajona News en Kajona News de
No comments made till now
Fork me on GitHub