Using new PECL Memcached extension for storing session data

24 thoughts on “Using new PECL Memcached extension for storing session data”

  1. Nice write up. There are a couple of points which should be clear though before using memcached to persist session data.

    Memcached stores all the data in memory so if the memcache daemon stops all the session data is lost. Moreover, specially if it’s used to cache other stuff besides session data, once the configured cache memory is filled “older” data is dropped from it to leave space for the new one. So it should be monitored to avoid session data lost on busy servers.

    Another issue to have in mind is that in a memcached cluster, the server where the data ends up stored is calculated by the client, so if we have multiple PHP servers they must have the same servers configured in session.save_path, in exactly the same order. Besides, if we add or remove a server from it, it might potentially trash all the current session data, since the client might end up asking the wrong server for the session data. These kind of changes should planned in advance and either warn the site users or dump and prime the sessions after the change.

    So I would advice against using session.save_handler for memcached support in servers with high loads. A custom session handler (using PHP code) which uses memcached to speed everything up but also persists the data to a database instance or a shared directory offers a more reliable solution while having a very small overhead, specially for small session data, over the memcached native save_handler.

    1. @DrSlump,
      Thanks for nice comment, much appreciated. The points you’ve mentioned above are valid, but shouldn’t be more of a problem if you give these session servers a decent amount of memory, plus if your sessions are not active infinitely.

      If you set expiry time for user session explicitly to 2 days, for example, it will expire the cache in 2 days. and for a decent memcached server with 4+ GB of allocated memory it shouldnt be a burden for even a higher traffic. And the beauty is that you can extend your node anytime.

      If you want to offload the data somewhere, for example, MySQL – the problem of using multiple DB or read/write load will persist. One good alternative could be using MySQL memory engine.

      Btw, yeah – it requires a well planned architecture first πŸ™‚ – This article is mainly for demonstrating the process.

      Have a good day.

    1. Memcached is a caching server actually, not a web server. It is used to store data temporarily for caching and offloading other business critical processes.

  2. If you are NOT on the “cloud” why not use memsession extension – it is way better for the session purpose & thread-safe … but even then Mohawksoft’s solution is preferable.

    memcached is just a multi-useless solution …


    1. Rhyno: Bolt, I can be a valuable addition to your team!

      Bolt; I’m listening …

      Rhyno: I am lightning quick, I’ve razor sharp reflexes, and I am a master of stealth. Plus, I’ll keep the cat in check

      Bolt : The road will be rough

      Rhyno : I have a ball

      Bolt : There’s no turning back

      Rhyno : Guess I’ll have to roll with the punches

      Bolt : Easy wont be part of the equation

      Rhyno: Promise?

      Bolt: gotta warn ya, going into the belly of the beast, danger at every turn,

      Rhyno: I eat danger for breakfast

      Bolt: you hungry?

      Rhyno: STARVING – mu ha ha ha ha

      Bolt: Welcome aboard …..

      @Martin Nikolaev – honestly, never heard of that extension. And I am listening ….

      And I would love to say “Welcome aboard…” too – have any benchmarking or feature comparison article around?

      πŸ™‚ – I just choose one of the very best solutions. May be it’s a multi-useless solution depending on how someone gonna use it, but it fits perfectly for me and million others out there.

      But, at the end, I am still listening…..


  3. memsession (or the mm session handler) are obviously faster than memcached, since they don’t have to use the networking stack to store/retrieve data. Problem with those is that they don’t solve the clustering of PHP servers without using an “sticky” load balancer. So, for those deployments where PHP is being run on a huge machine (like an IBM Power System) it’s going to perform way better than memcached.

    Be aware though that most literature online about optimizing PHP is based on “cloud” style scaling on commodity hardware, so documentation and even support for extensions like memsession is scarce at best, and usually require trial-and-error and going thru the extension’s source code to fine-tune them.

  4. thanks for the good article.

    am I wrong or doesn’t memchached need a lot of coding to do anything.

    the impression I get from your article is that it is “automatically” caching stuff. Is that really so?

    I tought you had to do like so in code:

    memcached::add($key, $val, $ttl)

    and then retrieve the object stored by using the key.

    Is memcached an “auto-cache” of sessions on top of what I just wrote?

    This is somewhat confusing πŸ™‚

  5. @Ritzo

    In this process, you dont need to write a single line of code if you follow the instructions available here. Session Data will be automatically handled by PHP+ Memcached Extension to store session data and successfully return it to the caller routines.

    You will use regular session code like



  6. How about session locks?

    With regular sessions you get a file lock on session file and two parallel scripts can’t mess with a session data. How that is managed in this case?

  7. I usually opt to store transient session data on a tmpfs partition – that way, you get the best of both worlds. It’s faster than memcached – and more stable than a 3rd party extension.

    Additionally, eAccelerator (and other op-code caching engines) has an in-memory session handler.

  8. @Ritzo Use of memcached for sessions is just one thing you can do with it. It’s one option along the way. Memcached really shines when you start to integrate it with your applications. For example – let’s say your web page parses an RSS feed for display as part of your web page. You are probably doing that parsing on every load. You can dramatically reduce the load on your server by doing that parsing only when a memcached get fails to populate your output variable.
    In pseudocode, it would look something like this:
    if (!$parsedresult = memcached.get()) {
    $parsedresult= LongExpensiveParsingProcess()
    echo $parsedresult

  9. Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum!
    .. ..
    ,’,”,.β•±β—₯β–ˆβ–ˆβ—£β€o’,”’,,’,.”.”,,’,.welcome to comeο½œη”°ο½œη”°η”°β”‚ ”,,’,.’,”’,,’,.”╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s