Is it recommended to store PHP Sessions in MemCache?

I'm working with a couple of Web Servers behind a Load Balancer and I can enable Sticky Sessions to hold a user to the one specific Web Servers - this will work.

I have been reading about PHP Sessions & MemCache. I must say what I've read is a touch confusing as some pages say its a good idea and others the opposite.

Questions:

  • is it possible to keep php sessions in memcache?
  • is it better to use sticky sessions over memcache?
  • what are the problems with php sessions in memcache - note: I can get enough cache (amazon so its expandable).

  • 1: YES. And I strongly recommend storing PHP sessions in Memcached. Here's why:

    Memcached is great for storing small chunks of data that are frequently accessed by the database and filesystem.

    Memcached was designed specifically for sessions. It was originally the brainchild of the lead developer of livejournal.com, and later used to also cache the content of users' posts. The benefit was immediate: most of the action was taking place in memory. Page load times greatly improved.

    Thankfully, PHP and Apache have an easy implementation to handle sessions with Memcached. Simply install with a few shell commands

    example for debian:

    sudo apt-get -t stable install php5-memcached
    

    and

    change your php.ini settings to something similar to:

    (taken from http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/)

     session.save_handler = memcache
     ; change server:port to fit your needs...
     session.save_path="tcp://server:port?persistent=1&weight=1&
    timeout=1&retry_interval=15"
    

    The key is the session.save_path

    It will no longer point to a relative file path on your server. APC was mentioned- APC for the caching of .php files used by the program. APC and Memcached will reduce IO signicantly and leave Apache free to serve resources,such as images, faster.

    2: No

    3: The fundamental disadvantage of using Memcached is data volatility

    Session data is not persistent in Memcached. So if and when the server crashes, all data in memory is lost. Everyone will have to log in again.

    And then you have memory consumption...

    Remember: the sessions are stored in the memory. If your website handles a large amount of concurrent users, you may have to shell out a little extra money for a larger memory allocation.

    Lastly, the latency (not stupid)

    This puts it all into perspective Unless you plan to keep all the action on one machine, you will eventually scale your memcached cluster over a network of servers. (This is scaling horizontally) Latency can have a dramatic effect on performance and throughput, when not addressed with the appropriate amount of hardware to handle the load requirements.

    My Amazon EC2 instances, for example, have a latency of about 80 ms, and the most expensive operation in my application takes about 70 ms to execute & cache a single page. Some of the fastest pages used to take 1-2 ms, but now take 81 ms with the 80 ms penalty. So instead of thinking that adding 2 servers will 'triple' my capacity, I would be wrong. 5-10 servers, maybe? There is no formula to determine the optimal number after the upgrade, as the only way to know is to measure the traffic as it comes, and spin-up new servers when the need arrives. Another $$$ consideration, but you can scale to infinity on the cheap, because getting more bandwidth with more hardware has always been the easy part!


    1. Yes, it is possible to keep PHP sessions in memcached.

    The memcache extension even comes with a session handler that takes very little configuration to get up and running. http://php.net/manual/en/memcached.sessions.php

    2. Memcache/Sticky Sessions

    I don't really know which is "better". I feel this is going to be one of those "it depends" answers. It likely depends on your reasons for load balancing. If a small number of users cause lots of load each, or if it's a large number causing a small load each.

    3. Cons of Memcache

    There are probably 2 main cons to using memcache for sessions storage.

    Firstly, it is volatile. This means, if one of your memcached instances is restarted/crashes etc. any sessions stored in that instance are lost. While if they were using traditional file based sessions, they will be still there when the server returns.

    Secondly and probably more relevant, memcached doesn't guarantee persistance, it is only meant to be a cache. Data can be purged from memcached at any time, for any reason. While, in reality, the only reasons data should be purged is if the cache is nearing its size limits. The least recently accessed data will be expelled. Again, this might not be an issue, as the user is probably gone if their session is stale, but it depends on your needs.


    作为我的观点,不建议将会话存储在Memcached中。如果会话消失,用户通常会注销,如果缓存的一部分消失或者由于硬件崩溃,它不应该导致用户的痛苦。根据memcached站点“,memcached是一个高性能,分布式内存对象缓存系统,通常属于本质,但旨在通过减轻数据库负载来加速动态Web应用程序。”因此,在开发应用程序时,请记住您必须拥有一旦在Memcached服务器中找不到数据,回退机制即可检索数据。

    链接地址: http://www.djcxy.com/p/68280.html

    上一篇: PHP会话锁定并使用Memcache存储会话

    下一篇: 建议将PHP会话存储在MemCache中吗?