Last Updated: May 11, 2017
·
16.98K
· mailmevenkat25

Memcached and PHP Sessions

The Traditional way

Basically when we write

session_start();

PHP assigns a new id

session_id(); //just a random tweeks of letters and numbers...

and then creates a new file , say sess_7s6fko70u9d5gse4kp8a9a10q4 (i.e, sess_{SESSIONID}) is created which contains our session data.

PHP manages sessions for us, which cleans old/expired sessions automatically using some of it's own mechanisms.

But using file system for storing sessions are pretty slow and it's a bottleneck to handle say thousands of users at time. But what if we want to have much faster access? here comes our Supeman "Memcached" to help us.

What is Memcached

Memcached as the name suggests, offers a way to store data's in system's cache memory.

Lets Get Started

PHP offers a easy way unlike other languages to manage our sessions with memcache

All we need is to install memcached on our server and install a php driver for it.

How to configure?

To use memcached to store the php sessions we will have to edit php.ini and replace the default file handler settings with something like:

; Use memcache as a session handler
session.save_handler = memcache
; Use a comma separated list of server urls to use for storage:
session.save_path="tcp://<memcache_server>:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

We can also configure it on runtime using the following code:

ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15');

Now all our session data's are stored directly in Memcached, that sound's great :) But what if your server gets restarted or something goes wrong? Would you prefer to log out all your users?So how to backup these data's? Since cache are volatile we need something that is reliable on failure. Will deal with this on how to backup session data's with MongoDB.

Now let's gets started with sessions,

session_start(); //starts a new session

//setting some session data's
$_SESSION['u_id'] = 123; 
$_SESSION['u_name'] = "Some name";

This will be stored somewhat similar like below,

7s6fko70u9d5gse4kp8a9a10q4 : u_id|i:123;u_name|s:9:"Some name";

What are they actually?

7s6fko70u9d5gse4kp8a9a10q4 : Session ID and/or Cache Key

u_id|i:123: 
u_id : Session key , i: Integer data type , 123: Value

That's great our sessions are perfectly stored :) But where does the problem occurs? Each sessions by default are stored only for 1440 seconds.

Oops! we are in trouble :/ But don't worry when we can extend it to another 1440 seconds using

session_regenerate_id(); //It regenerates new ID and resets expiry time

Yea, that's good, but what if your user stays idle for more than 1440 seconds? Their sessions will be cleared and they will be logged out (this is really bad :/). How to tackle these issues?

We can, by backing up our session data's to database (faster compared to file system) and then query in such a way that if Memcached fails, then fallback to Database for session details.

I will discuss about backing up session data's in my next pro tip.

Enjoy!

2 Responses
Add your response

" But what if your server gets restarted or something goes wrong? "
I think Redis is the best answer :)

over 1 year ago ·

Nicely explained. You can also configure memcached on PHP using this tutorial here: https://www.cloudways.com/blog/memcached-with-php/

over 1 year ago ·