12

This is an issue I've started to experiment a few months ago and I've been trying to fix without success since.

Symptoms: At random intervals of time symfony loses the session info and logs out the users. It seems somehow to be connected with the load of the site. When the load is higher it seems that the users are logged out more often, could happen even fast as 30 seconds.

Environment: Since this started I've changed a lot of the setup, including the php version, web server, session storage, symfony version. Here is the current setup: Ubuntu 10.04, php 5.4.0, symfony 1.4.17, nginx 1.0.15 with FPM. Here is how the session storage is configured in factories.yml:

  user:
    class: myUser
    param:
      timeout:         86400
      use_flash:       true
  storage:
    class: sfCacheSessionStorage
    param:
      cache:
        class: sfMemcacheCache
        param:
          lifetime:  86400
          host:      192.168.1.3
          serializer:  IGBINARY
          mode:        compiled
          port:        11211

I got to mention that I've also used redis for session storage and still had the problem. I really have no idea what to try next. Anybody else experienced something similar? At this stage any hint would be much appreciated.

Update: Ater months of searches and countless trials and errors I think it could be a concurrency problem. Our site is quite heavy on AJAX requests and I have learned that it can lead to issues with the sessions unless proper locking mechanisms are implemented in the session handler. First I have eliminated symfony from the equation, I've set it to use php sessions. With the default file session storage I never lose any sessions. Then I have configured php to use the memcache session storage. Surely enough we have started to see lost sessions. I am 100% positive that memcached is not running out of memory, I have installed an admin tool and the memcached server is barely using 2% of the 8GB allocated to it (no waste, the memory is allocated as needed). Then I have added a second memcached server and configured the session handler to use redundancy. This helped a lot, I rarely have any lost sessions. For now this is an acceptable compromise.

dcb
  • 531
  • 3
  • 15
  • Are you using memcache as your session storage mechanism? If so, I should imagine memcache is throwing away a session as a new one comes in - memcache storage is not guaranteed, by design. Either increase the available memory to memcache, or better yet, use another persistence mechanism. – halfer May 06 '12 at 14:51
  • As I mentioned I did use other session storage engines, as redis which doesn't throw away data. Besides I have never seen memcache to use more than 1GB, although is configured to use up to 8GB. – dcb May 06 '12 at 15:47
  • Ah, I may have missed that - unless that detail was added in a subsequent edit. Is the behaviour exhibited on a dev server under test, perhaps using `ab`? Do you need PHP 5.4? If not, drop back done to 5.3.x, or even 5.2.x, and see if that makes a difference? – halfer May 06 '12 at 16:02
  • It's a live server, the php version makes no difference, I only upgraded to 5.4 recently. I've also changed the web server recently. I was running litespeed with lsapi, but just switched to nginx with FPM. Nothing made any difference. The only thing that seems to affect the frequency of the session drop is the load. During the weekends when we have low traffic rarely ever loses the sessions, but during the week it can happen after an hour or even after 5 mins. – dcb May 06 '12 at 17:22
  • 1
    I'm not sure what else to suggest. Can you replicate that loss of session with `ab` in a dev environment? – halfer May 07 '12 at 00:34
  • 1
    Have you tried switching to a traditional session managing method to positively identify memcache as the problem? – Jestep May 07 '12 at 21:40
  • As I mentioned already I've used redis for session storage and had the same problem. But, for the sake of it, I've configured symfony now to use the plain sfSessionStorage and php is set to file session storage. I'll know in a few hours how it went. – dcb May 08 '12 at 01:48
  • Switching to sfSessionStorage and configuring php to use files for sessions did solve my problem. I also made a small change so that sessions are not started for search engine bots to avoid having tens of thousands of session files created every day. Next I will try to configure PHP session handler to use memcached, see how that goes. – dcb May 09 '12 at 10:17
  • Check how many space do you have for the session. If changing to files solved the problems than it may have to do with size memcache or redis can use to store its data and if the traffic increase it will start removing last recently used data. – Zefiryn May 20 '12 at 23:23
  • Please, give your configuration value for session.gc_maxlifetime option. – lisachenko May 28 '12 at 10:25
  • How big are your session objects? Do you store any non standard characters? – StackKrish Jun 01 '12 at 11:18
  • How does your application behave if it fails to connect to memcache/redis etc? If you are running a high load site protected by with linux/iptables, its possible the connections are failing due to running out of conntrack table space. Im making a lot of assumptions here, but by default even local->local tcp connections are conntrack'd, I had similar problems (with memcache as it happens), and it was very difficult to diagnose so I thought Id suggest. – carpii Jun 11 '12 at 22:23
  • @carpii it might be possible to be something like what you suggest, because both memcache and redis were failing and yes both machines are protected with iptables. But on the other hand our load is really not that big, maybe 20-30 hits per second at the highest. Anyway I have switched to file storage and it's been running without a problem for the last month. Now this is probably a separated issue, but I can't seem to be able to get PHP to work with memcache sessions directly. It just won't save any sessions. – dcb Jun 12 '12 at 09:09
  • Thanks for coming back to explain what you did. Could you post an answer instead of updating your question (and accept it)? – j0k Oct 16 '12 at 12:37
  • I had a similar issue and your fix of switching it back to the traditional file-based session storage fixed the problem. I was using symfony 1.3 and using the latest redis server. In my case, it was a session variable whose value was not being set in redis properly. Even though I called setAttribute(), when i looked up the session value in redis, the value was not being set. This did not happen always though. Anyways, thanks for the suggestion. – Ajoy S Dec 29 '12 at 20:41
  • @dcb : how u used redis for session storage, i want to see any sample program – sanjeev51 Sep 30 '15 at 13:50

2 Answers2

1

For some reason, memcache seems to miss every now and again and create a new session which causes the user to be logged out.

As suggested by Jestep, you should prove this by taking memcache out of the equation to see if the problem goes away.

If so, then the problem is either the way you are talking to memcache or memcache itself.

Lloyd Moore
  • 3,117
  • 1
  • 32
  • 32
0

Actually the setup we have been using for the last few months is symfony configured to use the normal php sessions (not any of the cache classes) and then php is setup to use the memcache extention (there is another one called memcached) to store the session on 2 redundant memcache servers. If I take out one of the memcache servers we immediately start to see lost sessions.

This was the only setup that really did the job.

dcb
  • 531
  • 3
  • 15