CodeVirtuoso
CodeVirtuoso

Reputation: 6438

PHP - to store big session array in $_SESSION or in database?

I need to store a big array during the session (currently up to few kB, and I'd limit it to 0.25MB max).

In your opinion and practice, is it better to store it in $_SESSION or in database?

Speed matters, but so does processor/memory usage, as it's on shared host, and I wouldn't want them to shut the site down for resource overuse.

Would you say there's a size range in which $_SESSION can be used with confidence it will work well? (For example 0kb-100kB or whatever your practice/tests showed).

Thanks.

Upvotes: 4

Views: 5041

Answers (4)

Matt Esch
Matt Esch

Reputation: 22956

Sessions are loaded into memory usually after being stored in a session file on the file system when you use the default session handler. You don't have persisted memory issues with sessions unless you are are explicitly using memory to store your sessions. In my opinion it is bad to have large sessions anyway. There has to be some fundamental flaw in the design. If you want to relate data to a user this is usually achieved by designing your database such that data is associated to the correct users simply through foreign keys. You have the ability to query a small subset of this data rather than loading a large chunk of data into memory and filtering it. Sessions are only really useful for user authentication. A RESTful API will not use sessions at all. I should probably note that I am biased in favour of stateless web. Sessions persist state between requests. I've only accepted authentication as a valid use case because browsers do not provide a versatile and secure alternative

Upvotes: 1

Niklas Lindblad
Niklas Lindblad

Reputation: 1041

The real performance penalty with sessions is that PHP rewrites the session data for every request. Writing to disk (which it will do in moste cases) is very slow. It should be used only for simple things like authentication and small data structures e.g. shopping carts and the like.

Depending on what kind of data it is and what software you have available on the server you should store it in the database or you could use a NoSQL solution like MongoDB, Redis or CouchDB.

Since you are considering using sessions in the first place, I take it as consistency of the data is not the number one priority. If the data is important you should use the MySQL database since it follows the ACID principles and will keep your data even after a client disassociates itself with the current session.

If the consistency is not important, consider using Memcached if it is available.

Summary: Use a database, but not necessarily MySQL (depending on what data it is).

Upvotes: 2

H27studio
H27studio

Reputation: 467

It depends a lot in the number of concurrent users of your site and your server. Since it is a shared server i would use a database if (and only if) you have a very big number of users, but its easier and faster with $_SESSION, and 200kbs its not a lot. Also, not using a DB saves you a lot of time retrieving data since it doesnt have to go back and forth DB server and web server on each request.

Upvotes: 2

Eugen Rieck
Eugen Rieck

Reputation: 65274

0.25MB with a sane number of sessions will use less resources if stored in the Session, than in the DB. So the likelyhood of resource overusage is lower with the session.

Upvotes: 4

Related Questions