Monday, November 12, 2012

Why does the Symfony 2 firewall take so long to load?

During the development of my current project, I noticed in the profiler that the Firewall took about 1 second to load, which was more than 50% of the total load time.


Since this is quite massive I decided to look into it. After 15 minutes of research I ended up figuring out that this was due to the PHP PDO constructor (my Firewall is the first to connect to the database as I use Entities as users). With this knowledge the issue was pretty quickly found ([1], [2]): as it turns out using a DNS name (like 'localhost') instead of an IP (like '127.0.0.1') causes this issue.

A simple edit of the parameters.yml file (changing localhost to 127.0.0.1) did the trick of reducing the Firewall load time to only a minimum.

12 comments:

  1. Great to know. Thank you for the share.

    ReplyDelete
  2. Does this apply to non-localhost database servers too?

    ReplyDelete
    Replies
    1. yes, the problem is with DNS translation, no matter what the hostname is

      Delete
  3. Sebastian,
    the issue is caused by a 'slow' DNS lookup. Since there are 2 entries for localhost on a 'common' machine (one for IPv6, one for IPv4) it might be a localhost only issue, where it first tries to connect over IPv6 and only when that fails over IPv4.

    A while ago I had a similar issue with curl, which was related to having IPv6 enabled. It seems like the currently implementation of the DNS lookup code tries to do the look-up via IPv6 first, but again, I haven't investigated this.

    As mentioned in http://drupal.org/node/1064342:
    "When using 'localhost' vs. '127.0.0.1' as the database host, connecting to the database takes over 1 second, probably by having the system try an IPv6 connect, then falling back to IPv4."

    So please give it a try with a non-localhost server and let us know if the issue occurs as well.

    ReplyDelete
  4. Thank you, It was very big problem for me. Now it is solved

    ReplyDelete
  5. Hi, I have the same problem but in my parameters.yml I had already put the IP address rather than the host. Some measurements:

    No auth:
    Total time: 137ms
    Initialization Time: 39ms
    Main Request: 98ms
    Firewall: ~

    In memory:
    Total time: 336ms
    Initialization Time: 41ms
    Main Request: 295ms
    Firewall: 249ms

    Db auth:
    Total time: 554ms
    Initialization Time: 65ms
    Main Request: 489ms
    Firewall: 453ms

    Some ideas?

    ReplyDelete
  6. Seems that I solved changing the encrypter by bcrypt to sha512. Now it is much faster

    ReplyDelete
  7. Hello,

    My connection to MySQL is already using 127.0.0.1 instead of a DNS and I have a delay of 1700ms for the firewall.

    Any idea/lead on what might be the issue?

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hey Anthony, do you use some kind of authentication? I had same problem and I used bcript. Then, after changing from bcript to sha512, the speed has improved greatly

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete