Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

if request #1 takes 20 seconds (user authentication)
and request #2 takes 30 seconds (DB access)
and request #3 takes 25 seconds (external web service),
request #4 can get access to this session only 75 seconds after it came in.

...

iconfalse

Image Modified

What are the right things to do?

  • Close the session as early as possible - explicitly with session_write_close(). Don't wait for the session to close automatically at the end of the request. This way your request may still take 30 seconds, but the session will only be locked for several milliseconds, allowing the next request to proceed.

    Info
    iconfalse

    Image Modified


  • As strange as it sounds - avoid using session_start() and $_SESSION. This will automatically lead to healthy parallel programming practices. For example, you can replace session_start() with session_get():
Code Block
languagephp
themeEclipse
Note
iconfalse
   function session_get() {
      	global $SessionData;
      	session_start();
      	session_write_close();
      	$SessionData = $_SESSION;
   }


Even if you have to write to the session at the end of the request, you should close it early and reopen at the end of the request. This doesn't make sense in linear paradigm, but in parallel programming it looks like this:

...

For example, you can introduce session_set():

Code Block
languagephp
themeEclipse
Note
iconfalse
   function session_set() {
      	global $SessionData;
      	session_start();
      	$_SESSION = $SessionData;
      	session_write_close();
   }


  • Don't write to the session too often. In general, don't consider the session a variable that keeps information between requests. In many cases, it is cheaper to get the information again than to write it to the session.
  • To the previous point, but also to performance in general - make sure you include caching in your architecture.

    For example, if you go to an external web service to fetch the weather, there is no reason to do this for each user from Berlin - do this once every 30 minutes, when the cached information expires.

    In the same way, maybe you really don't need to save anything in the session, except the user's ID - the rest you can save in cache or in the database. If you use Zend Server, Zend Data Cache would be a sound choice, anyway there is no lack of caching solutions for PHP.

...