Answer the question
In order to leave comments, you need to log in
What are the benefits of storing sessions in a database?
A couple of people said that storing sessions in a database is the best option (if you need long-term storage of sessions), what is the advantage?
I was answered in flexibility, but where the work with sessions becomes more flexible, I don’t understand ...
Please tell me what + and -
Answer the question
In order to leave comments, you need to log in
The session is stored in the database in case there is something of value there.
For example, there is a website builder or a social network. And the user can do something even before registration, for example, create a post or fill out his profile. You can serialize all its temporary data and place it in the database, most often JSON in NoSQL.
You can also save to a regular session, but usually the session storage space is regularly cleared and then the data will be lost.
It is worth saving to the database only when the user has done something. It is pointless to allocate space in the database for each user.
When the user logs in, the temporary data is transferred to a permanent storage location.
Regularly, the session area of unauthorized users is cleaned, but humanely, that is, not very actively, with a large storage interval.
I usually came across something else - the data from the database is unloaded into the session in order to load the database less.
Here is the scheme: https://toster.ru/answer?answer_id=645494
The advantage is simple, if the site is highly loaded, then it can have several backends on different servers, on which the load is balanced. And if you store sessions in files, then the user will be logged out when switching the server.
It is also convenient to associate a session with a specific user in the database. You can introduce restrictions such as "you cannot be logged in from two browsers", which is convenient for online games.
Or give the functionality "Log me out on all devices."
And there are probably plenty more options.
nothing in my opinion. 15k users a day will come to you and you will have +15k entries in the database... My personal opinion.
By default session data are stored in files. The implementation is locking a file from opening a session to the point it's closed either by session_write_close() or at the end of request. While session file is locked all other requests which are trying to use the same session are blocked ie waiting for the initial request to release session file. This is fine for development and probably small projects. But when it comes to handling massive concurrent requests, it is better to use more sophisticated storage, such as database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question