I will go straight to the point.

There is a site on which there may be a potentially large number of users (more than a thousand, for example). And each user has a certain "counter", which is increased by a certain amount regardless of whether the user is online or not .

Also, at the same time, this amount must be constantly kept in the database in its current form.

So how can this be implemented? Is it realizable at all? Is it scalable?

I heard about a package like celery , will it go for this task? Or maybe this problem is solved somehow differently?

  • four
    From the statement it is not at all obvious that the amount should be kept permanently in the database in its current form. How about a calculated field like начальное значение + прошедшее_время * скорость_счёта ? - MBo
  • one
    I also thought about this option. It turns out that the value in the database will need to be updated only when the user logs in online? - Aquinary
  • Yes, only the value itself is not necessary to keep in the database, only the parameters for its calculation. - MBo
  • The @Aquinary value in the database should not be updated at all; it is necessary to calculate the value at the current moment when it is needed. - Sergey Gornostaev
  • one
    скорость счёта может меняться? If the law is known, then it will probably be easy to calculate. If changes occur due to some event (including timer / time), then upon the occurrence of this event the current counter is calculated according to the current law, recorded as a new base value. - MBo

2 answers 2

Solved this problem as follows.

As soon as the user does something on the page, he writes the value of the current timestamp to the database. After it refreshes the page, the code grabs the timestamp while updating this page and calculates the difference. After that, the difference is multiplied by the counting rate and summed with the current counter, updating the value. At the end of the timestamp in the database is overwritten by the new value.

The disadvantage of such a solution is if the counting rate can change at the moment when the user is offline, he will receive the amount depending on what point in time he enters the page.

    1. Connect to DB
    2. Start an infinite loop:
    3. We perform a call to the database like: "UPDATE users_list SET sum_field = sum_field + 1 WHERE something;"
    4. sleep (pause duration)
    • one
      And let the iron horse work without a break? If I'm not mistaken, this is called polling. Is it good to use it for such a task of low significance? - MBo
    • @MBo If I'm not mistaken, this is called polling. - You're wrong. Polling is when a survey. The word polling itself means polling. Nothing is polled here. I did not understand the question about "goodness". SQL server you are already using. And if you need to increase all the counters by one every 10 seconds, this means that every 10 seconds you need to increase all these counters. Magically, they themselves will not change. - Sergey