title: Symfony 3 - Firewall Listener Performance at Login
body: <p>Replied to my own question and posted it here for reference to others.</p>
  
  <hr>
  
  <h1>Setup</h1>
  
  <p>My application is really fast out of the box, running:</p>
  
  <ul>
  <li><code>Symfony 3</code> with <code>Doctrine2</code> </li>
  <li><code>PHP 5.6.*</code> with <code>CGI/FastCGI</code> as a PHP handler (not even php 7)</li>
  <li><code>mySQL 5.6.*</code></li>
  </ul>
  
  <p>Then, it is optimised further with:</p>
  
  <ul>
  <li><code>Zend OpCache</code> to get faster PHP execution through opcode caching and optimization</li>
  <li><code>Memcached</code> to store <code>user sessions</code> in memcached </li>
  <li><code>Memcached</code> to act as a <code>metadata cache driver</code> and a <code>query cache driver</code> for <code>doctrine 2</code></li>
  </ul>
  
  <h1>The bottleneck</h1>
  
  <p>However, one route is very slow and that is the <code>fos_user_security_check</code> route when I authenticate via the login form.</p>
  
  <p><a href="https://i.stack.imgur.com/kJKgM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kJKgM.png" alt="Debug Profile Symfony3"></a>
  It shows <code>Symfony\Bundle\SecurityBundle\EventListener\FirewallListener</code> as the culprit - though I am not sure why that is because this route lights up quickly on my local machine but doesn't on my production machine. </p>
  
  <h2>Things that I have tried</h2>
  
  <ul>
  <li>[x] To use Memcached to cache PHP sessions -> no difference</li>
  <li>[x] To use Memcached to cache Doctrine stuff -> no difference </li>
  <li>[x] To run mysql with <code>skip-name-resolve</code> -> no difference</li>
  </ul>
  
  <h2>Related posts I have seen</h2>
  
  <ul>
  <li><p><a href="http://symfony.com/doc/current/reference/configuration/security.html#using-the-bcrypt-password-encoder" rel="nofollow noreferrer">SecurityBundle Configuration ("security")</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/16111324/what-is-the-symfony-firewall-doing-that-takes-so-long">What is the Symfony Firewall doing that takes so long?</a></p></li>
  <li><p><a href="http://12wiki.blogspot.com.au/2012/11/why-does-symfony-2-firewall-take-so.html" rel="nofollow noreferrer">Why does the Symfony 2 firewall take so long to load?</a></p></li>
  </ul>