1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.20 out of 5)
Loading ... Loading ...

Yesterday I’ve read a post on The Developer’s Day blog. Then tested this thing in Yii. There is a jQuery in my application, so I created test controller and test action, that only outputted “Hello, world” and called it from the firebug console like this (I have url rewrite set up):

  1. $.ajax({url:‘/test/test.html’});
  2. $.ajax({url:‘/test/test.html’});
  3. $.ajax({url:‘/test/test.html’});
  4. $.ajax({url:‘/test/test.html’});
  5. $.ajax({url:‘/test/test.html’});
  6. $.ajax({url:‘/test/test.html’});
  7.  

Yes, really it matters. Each call starts after the previous one ends and it takes long to finish them all. But each web application handles the concurrent requests by nature. So I started a forum thread to clarify this. And sebas recommended me to use CDbHttpSession instead of standard one. Yes, I knew, that storing sessions in DB speeds up the application, but I thought that is because of more effective search. And it turns out, that besides more effective search it gives one more DB benefit – concurrent access to the session storage. That’s actually the reason #1 why we usually use databases instead of files :)
So here is a quick trick, that solves the problem:

  1.  
  2. // application components
  3.         ‘components’=>array(
  4. ……….
  5.                 ‘session’ => array(
  6.                         ‘class’ => ‘system.web.CDbHttpSession’,
  7.                         ‘connectionID’ => ‘db’,
  8.                 ),
  9.         ),
  10.  

It creates a new table YiiSession and uses it for session data. This time the JS test above runs much faster. Try it yourself!

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Share this post with a friend Share this post with a friend

12 Comments

  1. Jonah says:

    Nifty! Nice to know, and good to have if you have heavy Ajax features.

    (BTW I’m going to add you to my WordPress blogroll)

  2. Konstantin Mirin says:

    Not only AJAX features. The cause of this behaviour is blocking session storage. This slows down the concurrent requests for some portal, for example.

    (Thanks for adding me there!)

  3. Tomas says:

    Hi,

    found incorrect link to ‘forum thread’.

    Greatings, Tomas

  4. Konstantin Mirin says:

    Thanks! I corrected it.

  5. Kostik says:

    What about using RAMDRIVE?
    something like “mdmfs -p 777 -s 1024m md1 /usr/local/www/sessions”
    the only reason i see for storing sessions in DB is if u want to share sessions across multiple web boxes.

  6. Konstantin Mirin says:

    I haven’t heard of this. Any more info on the topic?

  7. php rounded corners says:

    that looks very nice, thank you for sharing!!!

  8. Derek says:

    Nice post, thanks.

    @Kostik: You might need the session across multiple servers if you do load-balancing… else user will have unexpected results.

  9. Konstantin Mirin says:

    @Derek
    Though I’m not using the load-balancing, I realize this is a possible issue. Maybe this can be handled with MySQL clustering. I’m not sure since I’ve never used load-balancing in my work…

  10. Lafriks says:

    Problem is that when storing session in DB and there are paralel ajax requests and they store values in session, only last requests session changes are stored :(

  11. Konstantin Mirin says:

    This issue has nothing to do with the session info storage. That’s the problem of the concurrent execution handling, I think.

Leave a Reply