Storing sessions in Memcached: The pitfalls

Some projects have a lot of web servers, and they need a Memcached server to store the non-sticky sessions. So, the users can access the web servers without any session problems.

One fine day…

You decided to analyze your code right away. Everything is ok with your code.
Most probably, Memcached deletes your users’ session data.

How so?

Well, why Memcached deleted his session data?

When Memcached applies the LRU algorithm, X user’s session will be deleted from the Memcached server’s memory.

Another problem; Memcached doesn’t support persistent storage. Therefore, if you shut down and start Memcached for some reason (configuration changes, restart OS, hardware upgrade etc.) then you will lose the session data. So, your users will lose their sessions while they are using the website. It might be an unpleasant experience for your clients.

If you don’t want to lose the sessions then you will have to store the sessions in a database that supports persistent storage.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store