Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is storing data in $_SESSION different from memcache(d)?

The obvious difference is that when stored in memcache(d) data becomes available cross the system. Sessions store data per user, though, session_id() could theoretically be used to share this data with more users.

However, in terms of performance, speed and memory usage - is there a difference?

like image 292
Gajus Avatar asked Feb 23 '12 14:02

Gajus


2 Answers

PHP sessions are stored in the filesystem by default. You can modify this behaviour so that they are saved in a database, or in your case memcached.

So in terms of performance, memcached is generally faster than the filesystem. This obviously depends on your environment.

See session_set_save_handler

like image 159
Vitamin Avatar answered Sep 20 '22 09:09

Vitamin


Apples and oranges. They achieve two entirely different things. Your real question is file storage vs. memcache. You could theoretically store session info in memcache instead of the file based storage. Then the performance of session would be identical to directly pushing a value to memcache.

All things being equal, memcache performs far better than file caching. In terms of memory, when you read the file to get the data (in this case the session file), it goes into memory anyhow, so its doesn't save any space. In fact if multiple requests happen to apache different worker process may need to read the same session file--each using their own chunk of memory until the worker process is reaped. Using memcache, this doesn't happen.


like image 38
Ray Avatar answered Sep 19 '22 09:09

Ray