Benjamin Benjamin 06.02.2015

Detecting Bottlenecks: Blocking Session

PHP uses a file-based session store by default that conveniently works out of the box. However file-based sessions can introduce a serious bottleneck when used with parallel (AJAX-) requests. PHP locks each session file during session_start() until the request ends, preventing concurrent access to the same session at the same time. A blocked request waits at session_start() until previous requests release the lock.

This leads to linear AJAX-requests execution instead of in parallel.

We improved the bottleneck detection to scan for this problem automatically, as you can see in this screenshot that shows an transaction that is called via AJAX:

Session BottleneckSession Bottleneck

No changes are necessary to your Profiler setup, this can be extraced on our side already. If your application is suffering from this problem, you will see the warning label popup automatically with the next affected trace.

There are multiple different fixes to this problem:

  1. If you want to keep file based sessions or use any other backend with locking support such as Memcached: Call session_write_close() when you are sure that you are not writting to the session anymore later. This allows other request to start reading the session until again session_write_close() is called or the request end.

  2. You can switch to a custom session backend that don't implement locks on the session. Careful though, you can theoretically suffer from lost updates when two parallel requests read and then write to the same session. The PHP manuel describes how to create a custom session backend in detail.

See the Session reference in the PHP documentation for more information.