2

Say I have a caching system that i can use for storing sessions:

// example i have a cache class
$memcached->add('key','value');
// then i can get the session from
$memcached->get('key');

What are the advantages and disadvantages of using caching (e.g. memcached, maybe Redis can fit in, or other things) for sessions rather than using PHP's built-in sessions?

2 Answers 2

4

I want to start by clarifying. When you're using PHP session storage, the sessions are being stored in a file (usually in /tmp). Each session becomes it's own file.

When you use memcached or redis to store sessions, nothing in your PHP code changes.

You can simply configure php to use redis or memcache to store the sessions instead (configured via session save handler in php.ini).

Pros:

  • session storage/retrieval becomes much much faster

cons:

  • you need to configure php.ini (or implement the session save handler yourself)

now, I would actually recommend using redis instead of memcache, simply because the sessions will become permanent, so if you need to reboot the server, all your users wont get logged out.

For redis: https://github.com/owlient/phpredis (see "session handler" lower on the page)

3

You should be able to have PHP store session data in Apache's runtime memory by setting session.save_handler to mm. However, to do this you need to compile PHP with the memory management module (--with-mm), which I don't think is available for Windows.

If you want to use memcached or some other caching mechanism, then it'd probably be best to implement user-defined storage handlers using session_set_save_handler so you don't have to rewrite your session management code.

If you do that, then I don't think there are any obvious disadvantages to storing session data in that way. The obvious advantage is speed.

Edit: I came across this page which discusses, aside from speed, the main advantages/disadvantages of using memcached for storing sessions, namely:

  • It's easy to share sessions across multiple webservers without using sticky sessions.
  • However, memcached makes no promises of keeping the data up until expiration—only that the data will not be available after expiration. So if memcached is low on ram, hasn't been used lately, or the server goes down at all, the session data will be lost.
3
  • so what if we can use session why do we need to use cache ? Feb 6, 2011 at 9:04
  • oh i see, its a choice between shared or not eh? Feb 6, 2011 at 9:43
  • @Adam: I've personally never used memcached to store sessions, but being able to share session data across multiple webservers with minimal configuration may be a good reason to use it if you can tolerate the occasional lost session. And there may be other benefits to systems like Redis. Feb 6, 2011 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.