Store in the database or in sessions

Asked

Viewed 879 times

4

While doing a survey, I found that some colleagues store the products of the trolley in sessions like this:

$_SESSION[cesta][$indice][produto]

In particular, I usually store the customer’s products in a database table and after a certain open period (I have a Statuscompra field = 'A' and the date "NOW()"), for example, 3 days depending on the customer’s stock, the system automatically deletes.

But now I am in doubt. What is the best way to work with shopping cart in a virtual store? I refer to good practices...

3 answers

4


There is a problem with using only session, is that this default lasts very little time (in general the lifetime of a session is 24mins), I can put it in the cart now, I will talk/ask my girlfriend/mother/father... and in one/two hours it will be gone, in this scenario it is "bad" because there is very little room for manoeuvre (time) for the user to keep the "candidate" products to be bought. In this case the ideal would be to reinforce with cookies (your way is also correct, in the database and after 3 days these are deleted), or if you can, change the lifespan of the session as the colleague @Ttkdroid mentions in his reply (although this consumes resources on the server).

That said, then:

It actually depends on your needs, there is no rule for that, if you want to keep permanently what users thought, but did not purchase, you should enter in the database, if you want it to be only temporary you can just leave in session/cookies.

I believe that many systems, mainly the great keep this data permanently, for future functionalities (eg algorithms to know the interests and show results according to the categories of products that have already been in your cart and/or that you have already purchased) or simply for statistics, as fellow @Daniel Omine said may also help in strategic sales decisions.

It all depends on whether you think it’s worth it, you have support for, and what goals you’ll keep permanently.

Personally until today it was very rare to create a system in which to store the information of the cart in the database, simply because for the great part of the online stores this information would be wasted, that is, it would be data that would not have any purpose in the future and would only occupy disk space, and therefore not worth saving.

Thinking/weighing this, this decision is always in the hands of the programmers/responsible/system owners.

  • 1

    The first part of the answer is very imprecise. The second part is speculative. No decision can be made from it.

  • 1

    @Nottherealhemingway the first part is not to decide anything, it is just to talk about the session alone and point out a disadvantage, the doubt of the colleague goes around

  • 2

    @Nottherealhemingway is not to make any decision, none is wrong, depends on the needs. It is up to the responsible to weigh this and decide whether it is worth keeping permanently. There is no right or wrong, this decision is made depending on the system that will be worked

  • 2

    @Miguel’s answer is correct. What is "wrong", for the scope of the SO-pt site, is the question because it is based on opinions. Decisions that depend on the business model are based on opinion and this is reason to close a question. And that’s exactly what describes the answer. In general, the "right" way is to save the cart in a database even if the Cart is abandoned. This data is extremely important to understand the behavior of customers in various aspects and directly influence strategic sales decisions. It is a broad and very deep theme.

2

I know you expect a definitive answer but have no definitive answer to this question, everything will depend on the architecture of your application.

If you are going to host the system on a webfarm, using in-memory session as usual is not an option, as each client request can be answered by a different server. In that case you would have to store in a database or have a session server.

If using a REST API, it is recommended to store in a database as well.

You can also store in the client, in Sessionstorage, if the data is not critical. This avoids the problem of session timeout and does not use storage space on the server.

Sessions occupy server memory, on a large-scale system it is best to avoid using Session.

Based on these facts I suggest storing in a database or Localstorage for a better scalability of your system.

1

You can change session timeout parameters in php

// manter sessão por 1 hora
ini_set('session.gc_maxlifetime', 3600);

// cada cliente vai se "lembrar" da sessão por 1hora
session_set_cookie_params(3600);

session_start(); 

You adjust how long in seconds you want your session to last.

Either extending the session or storing in the database has its pros and cons. Where in the database you get a lot of junk that should be cleaned if the user does not proceed with the checkout, the longest session consumes resources from the webserver by the time you determined.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.