Why We Use Memcache for PHP Sessions?

Few Days back I have posted about storing the PHP Session in MemCache Server. Before this article I have also written about setting up PHP Session Path. With this article you should be able to instruct PHP to set your session on specific location defined by you.

On my article about storing session in MemCache, One of the reader had asked thay “Why should We store Session in Memcahce?“. So this is the article to answer this question.

How Session Works?

When ever you create a session using PHP, then session files are created on the application server on which PHP is running. So now that session will only available on that particular app. server only. Those session values can not be available on other application servers.

What is MemCache?

Memcache is the distributed cache server that can be accessible by any app. server. So cache value stored in memcache server can be available to any app. server with the valid credentials.

Definition of Memcache can be found at its own website which is as per below:

“Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.”

You can store your any types of values in Memcache in Key-Value pair. So after all session also one type of variable.

Store Session on MemCache

Now when you store your session in MemCache as per the technique mentioned in this article, all the session values will be stored in MemCache Server rather than application server.

You will just need server host and port on which MemCache server is running.

But Why We Store Session in MemCache?

Now imagine the small scale web application which has only one application server. So in this environment this technique for storing the session in MemCache will not provide any major benefits.

But now imagine the large scale application in which you have multiple application servers (say 2). This is the place where this methods works and helps to the developers.

Now once you have 2 application server you must needs a load balancer which maintain the load of the all app servers. So in this case if you have not used an sticky session then it is possible that first request served by the app server 1 and second can be served by the app server 2.

User’s Request without MemCache Session

Have a look at below image which shows the one user’s request to two different app servers. Now Imagine that when user comes to website and request is served by the App Server 1 and perform the login there. So any cookies and session created on that request will not be available on another application server.

Store Session in Application Server

Store Session in Application Server

Now when user make the second request to the server then it can be possible load balancer serve the request from the second app server. In that case no cookies or session are not available on that second app server. In that case application will be served as no one is logged in the application. That is because all authentication cookies and session are saved on the application server 1 and those are not shared so can not be available on application server 2.

User’s Request with MemCache Session

Now have a look at below image which shows the flow of storing the session in MemCache server and benefit for the same.

Store Session in Memcache Server

Store Session in Memcache Server

When user comes to the site and perform the login on the Application server then all the session are store in the separate server which is “MemCache Server“. These session are available to all application server.

So now when user makes a new request then load balancer will serve request from the second application server. But no problem at all as your application will still able to get all session created by the Application server 1. This is because all authentication data are stored in shared server which is MemCache Server.


If you are running on multi application server website then i strongly suggest to use this technique to save the session which are available to all your application server.

Also you can have more settings for memcache server to scale as per your caching needs. Also memcache is shared but faster cache system which we can use for the multi application server web sites.

If you would like to get updates for these types of articles from now on then subscribe to our RSS feed by your email address.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: