Memcache sessions vs files vs mysql


On a dedicated server with an HDD, I tested the time of PHP sessions with a save handler of files and as memcache. Also mysql without using session functions.

microtime average:
0.000272989273071 (files)
0.000823020935059 (memcache)
0.000809907913208 (pdo/mysql, select only - not including connection since it’s overhead for other uses)

I am surprised the files time is significantly faster. Perhaps there’s more configuration that I’m unaware of?

As far as scaling is concerned, if memcache isn’t much faster than mysql, wouldn’t it be just as easy to handle all sessions in a database, and just use $_COOKIE to manage the ID?

I will run these tests again on a SSD soon.

Any thoughts on this?

Using the default session storage location (files) is normally good enough for most websites.

Though, there its three reasons to switch it out:

  1. You are on shared hosting, or host multiple sites on the same server and you are worried if either has security issues.
  2. Your site become popular, and you need more juice than the current server is able to provide.
  3. You want the control you get by writing your own custom session handler.

Storing the session data in the database give you more control compared to memcache, i.e. its easier for someone to flush a memcache server by mistake, then to truncate a database table. But the trade off is that it require a lot more server resources.

As your website grow, database writes starts to become an expensive process. So at that point you need to create another database cluster for sessions, or just move them into another storage method, like memcache.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.