Gajus
Gajus

Reputation: 73998

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?

Upvotes: 7

Views: 647

Answers (4)

Ray
Ray

Reputation: 41508

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.


Upvotes: 2

Vitamin
Vitamin

Reputation: 1526

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

Upvotes: 5

Bot
Bot

Reputation: 11865

Sessions are stored in either a file or database. Memcache is stored in memory for faster access.

Upvotes: 0

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

Well, you can simply use memcache for $_SESSION. That's definitely faster, as it doesn't require a lot of calls to a PHP API, but it goes straight to the C API instead.

Small performance bonus though, PHP really isn't all that slow.

If you're going to compare memcache with filesystem sessions: use memcache. Really, you should, as that simply stores them in memory instead of storing them on the filesystem. A lot faster. Of course, you do risk losing session data if the memcache server overloads. Memory usage of memcache will of course be higher than the filesystem alternative.

Upvotes: 0

Related Questions