Search code examples
phpmysqlsymfonyconcurrencydoctrine-orm

Symfony not serving concurrent requests


My problem is that my Symfony application running on a remote machine with Apache/2.4.6 (CentOS) PHP/5.6.31 MySQL 5.7.19 does not handle concurrent requests. Meaning that when asking for two different pages at the same time. First one has to finish before second one can be rendered.

I have a another site on the same server written in plain Php which has no problem rendering as many pages as possible at the same time (it uses deprecated mysql connection not pdo like Doctrine).

That said I have done the following test: I have inserted a sleep(3); at my DefaultController. I requested that page and simultaneously requested a different one. See the two profilers below:

Page with sleep (called 1st): Page with sleep (called 1st) Page without sleep (called 2nd). Page without sleep (called 2nd)

  • Page 1 normal load time is 782ms
  • Page 2 normal load time is 108ms

As you can see Symfony's Http Firewall is taking all of the time of the second page to load. My guess (might be stupid) is that 1st action holds the database connection and only until it has finished with it does it let it go for other requests to use it. And especially something having to do with Doctrine's use of PDO connection.

By the way I have already read help and articles like: - What is the Symfony firewall doing that takes so long? - Why is constructing PDO connection slow? - https://www.drupal.org/node/1064342

P.S. I have tried using both app.php and app_dev.php in apache configs nothing changed. Sticked to app_dev.php so I can have the profiler. And local development using Symfony's build in server has the same result


Solution

  • You can not have 2 concurrent requests for the same open session in PHP. When you use a firewall, Symfony locks the user session and until you release it manually or the request is served.

    To release the session lock use this:

    $session->save();
    

    Note there will be some drawbacks and iimplications. After you save the session, you will not be able to update it (change attributes) until the next request arrives.

    Session management: https://symfony.com/doc/current/components/http_foundation/sessions.html

    Session interface: http://api.symfony.com/4.0/Symfony/Component/HttpFoundation/Session/SessionInterface.html#method_save

    Note 2. If you have multiple concurrent users with different sessions PHP will serve concurrently those requests.