4

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.

1
  • 1
    I assume using a DB will definitely be slower. 250 kb isn't that much of data
    – knittl
    Jan 5, 2012 at 12:59

4 Answers 4

4

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.

2

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.

2

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).

1

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

5
  • I am very curious about flaw in the design remark, maybe you are right. What I am doing is allowing users to check thousands of elements for processing, so these elements are naturally listed in multiple pages. I need a way to remember which elements the user checked while moving around pages. Do you think this approach is flawed, and if so, how would you do it? Reply greatly appreciated. Jan 5, 2012 at 13:19
  • 1
    I tend to aim to ensure that every view is addressable. If you need to save data, such as selections, you persist that data on the client side (in the form of hidden inputs, Json, or as part of the address) and you post that data back with each request. If you have a lot of data and can't rely on JavaScript being present, I think the best option is to create tables which contain a user binding field and a session binding field, and to address those objects by their id. Having multiple pages doesn't mean you need to have the data at each page.
    – Matt Esch
    Jan 5, 2012 at 13:38
  • Under average usage, the user doesn't revisit the previous pages, they more often continue with the next page of entry. So essentially what you are doing is storing partial data in your database, and once that data is complete, getting your database to manipulate it (if you even need to move it). But you do not need to reload data to do that, your database can manipulate it for you without querying the data and returning it to the application. The session identifier I suggested is simply so that you can clean up abandoned sessions that aren't reclaimed by the user after a certain period.
    – Matt Esch
    Jan 5, 2012 at 13:41
  • The user is also able to continue their selection if their session expires, you just have to reclaim it for them based on their user id and not their session id. The session id is merely used for expiring the data. You could do this yourself with a timeout, but this is in some ways duplicating the session timeout logic and you would have to manually persist the data in the same way that the sessions are manually persisted between pages.
    – Matt Esch
    Jan 5, 2012 at 13:42
  • I accepted another answer as it directly answered my question and may be useful to others as such, but also points to you for indeed pointing a flaw in my design and giving a better solution. Jan 5, 2012 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.