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.

Conclusion

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.

About the Author: Avinash

Experienced full stack freelance web developer with a track record of producing excellent results for 11+ years. Excellent communication and collaboration skills, managing with a team of 20+ people. I’m motivated to make a positive impact on your revenue through the customer web portal development project.

2 Comments

  1. Hood January 28, 2012 at 2:59 am - Reply

    Greate article. But how different if storing session in database level? I guess it should be another solution. Perhaps another article using memcache to solve SSO, due to capabilties of sharing session with another app. :)

  2. Hood December 20, 2019 at 2:54 pm - Reply

    Greate article. But how different if storing session in database level? I guess it should be another solution. Perhaps another article using memcache to solve SSO, due to capabilties of sharing session with another app. :)

  3. RizMe January 31, 2012 at 1:04 pm - Reply

    nice article!
    I also wonder how different from the way that stores sessions in a database in performance.
    I expect this article way is more fast than it.

  4. RizMe December 20, 2019 at 2:54 pm - Reply

    nice article!
    I also wonder how different from the way that stores sessions in a database in performance.
    I expect this article way is more fast than it.

Leave A Comment

FREE QUOTE

Do you need help with services? Request a free quote to get in touch.