Question

When is it best to use database as a session store in PHP? I know that 1 instance will be when I am sharing request load across multiple servers, I will need to maintain session state across those servers.

An article i am currently reading http://onlamp.com/pub/a/php/excerpt/webdbapps_8/index.html?page=2 says

Using files as the session store is adequate for most applications in which the numbers of concurrent sessions are limited

I do not get why the number of concurrent sessions should affect the preferred session store.

Are there other reasons why I should choose to store session data in a database?

Was it helpful?

Solution

Storing a large amount of data in files can get slow as you have to search through large amounts of possibly unrelated data to the page you are currently serving up.

Interfaces for interacting with data in files are also not as streamlined as interfaces, to, say, MySQL - MySQL is built for this sort of thing, while single files aren't really.

Concurrency is another issue - databases will handle it for you if different users of your website are simultaneously trying to read and write data - this happens more often as the user base becomes larger. Doing these tasks simultaneously is possible with a database, but much more difficult with storing information in files. You would end up having to program some synchronization code, and that may get messy.

If you expect your userbase to grow over time, then it may be best to immediately design your program around a database, so that you may not have to switch as users start to wait on other requests being filled before their page gets loaded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top