PHP - to store a large array of session in $ _SESSION or in a database?

I need to store a large array during a session (currently up to several kilobytes, and I would limit it to a maximum value of 0.25 MB).

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

Speed ​​matters, but also CPU / memory usage, as on a shared host, and I would not want them to close the site for excessive use of resources.

Could you tell me if there is a size range in which $ _SESSION can be used with the confidence that it will work well? (For example, 0kb-100kB or whatever your practice / tests show).

Thanks.

+4
source share
4 answers

0.25 MB with a reasonable number of sessions will use less resources if they are stored in the session than in the database. Thus, the likelihood of resource downtime is lower with the session.

+4
source

It depends on the number of simultaneous users of your site and your server. Since this is a shared server, I would use a database if (and only if) you have a very large number of users, but it is simpler and faster with $ _SESSION and 200 kbps not so much. In addition, without using the database, you save a lot of time to extract data, since it does not need to return back and forth the database server and the web server for each request.

+2
source

The real performance in sessions is that PHP overwrites the session data for each request. Writing to disk (which it will do in most cases) is very slow. It should only be used for simple things like authentication and small data structures, for example. shopping carts, etc.

Depending on what data and what software you have on the server, you must store it in a database or use a NoSQL solution such as MongoDB, Redis or CouchDB.

Since you are considering using sessions first, I believe data consistency is not number one priority. If the data is important, you should use the MySQL database, as it follows ACID principles and will store your data even after the client breaks itself into the current session.

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

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

+2
source

Sessions are loaded into memory, usually after being stored in a session file in the file system, when you use the default session handler. You have no memory problems with sessions unless you explicitly use memory to store your sessions. In my opinion, it's bad to have big sessions anyway. There must be some fundamental flaw in the design. If you want to associate data with a user, this is usually achieved by designing your database so that the data is connected to the correct users simply using foreign keys. You have the ability to request a small subset of this data, rather than loading a large piece of data into memory and filtering it. Sessions are really useful for authenticating users. The RESTful API will not use sessions at all. I must have noted that I am biased in favor of a stateless network. Sessions maintain state between requests. I only allowed authentication as a valid use case because browsers do not provide a universal and secure alternative.

+1
source

Source: https://habr.com/ru/post/1389460/


All Articles