Symfony2: Intermittent High Response Time/Slow Ses

2019-06-23 22:45发布

问题:

I'm seeing very strange behavior coming from the Symfony2 Session manager component. In particular, the SessionHandlerProxy::read() function is occasionally very slow in my production environment.

Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy::read

I'm using Apache2, on an Amazon EC2 running Ubuntu, with default Symfony2 session storage (not Redis, or something similar), though I'm wondering if I should be. I have NewRelic installed to trace my transactions which reports the following:

The slow responses are intermittent and I haven't noticed any marked correlation between requests/min and slow session read times. I'm stumped, any ideas what I might try?

回答1:

I ran into something similar on a project. I identified it when we switched to redis for session handling (we've switched back to the default file system handler for PHP, problem still occurs intermittently).

What's likely happening is the SessionHandlerProxy::read() method is locked out of the session and the process is waiting for the session to unlock.

Session locking is a good thing, as it prevents race conditions in php. So what is likely happening is that another request is currently accessing the session and not releasing it as immediate as it can. The solution I found via my google fu skills calls the $session->save() handler as soon as you are finished with the session, which intern calls session_write_close() (this unlocks the session for the next request).

I hope this helps out!

Symfony API Documentation for Session:Save()

Here is the original stack overflow question (albeit 5 years ago).