D
D
Danil Sysoev2021-02-16 15:28:04
MySQL
Danil Sysoev, 2021-02-16 15:28:04

How to set up the server and code for multiple simultaneous requests?

I'll write it in order. Three points: Task, My crutch solution (problems) and Question.
Task:

Spoiler open only smart and smart
Чтобы не углубляться и не расписывать свою задачу со всеми деталями которые не имеют отношения к вопросу, я привожу просто абстрактный пример, сторонников отвечать "зачем это нужно?", "это бесполезно" попрошу не оставлять комментарии, это очень важно для меня. Спасибо за понимание.

There is a site in which there will be a load at a certain time. For example: every day at 19:00 about 2000 users click on the button. A "chain" is created in the database in the table:
id|parent_record_id|user_id
2 |_______________1|_____45
3 |_______________2|_____32
4 |_______________3|_____98

In addition to this record, records are also created in the database in other tables and certain calculations are carried out. The script runs in approximately 0.1 to 0.6 human seconds.
The chain must not be broken.
My crutch solution:
Server:
Processor 8 X QEMU Virtual CPU version 1.5.3 2099.998 MHz
RAM 12019 MiB
Server software: Apache+FastCGI
DBMS: MySQL
DB::beginTransaction(); // начинается неразрывная транзакция
$checkLine = Users::where('line_is_busy', '=', 1)->count(); // проверяем занята ли линия
if ($checkLine) { // если да то кидаем ошибку
     return "Линия занята, кликните еще раз, вдруг вам повезет попасть, а то разработчик не знает как тут реализовать грамотно очередь";
}
// если линия свободна то занимаем ее
Users::whereId($user->id)->update(['line_is_busy' => 1]);
//
// здесь выполняется тот скрипт на 0.1-0.6 секунд
//
Users::whereId($user->id)->update(['line_is_busy' => 0]); // освободили линию
DB::commit();

The solution above does not suit me in that if the first user clicked on the button. The rest click further until they "hit".
Questions:
1) How to implement so that the user does not receive an error, but simply waits for a response from the server.
2) In the course of the research, thoughts appeared that Nginx would be better suited for this task than Apache. Is it so? Or is it irrelevant in this context?
3) Is it possible to implement this task on the server with such data so that the server and the DBMS do not fall off?
4) How can this be synthetically tested?

PS I'm not asking you to solve the problem for me. I just lost my way a little and want to feed on your thoughts and suggestions.
Thank you very much in advance for your answers and your time.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Vorobyov, 2021-02-16
@YoungOldMan

Via Queues
https://laravel.com/docs/8.x/queues

S
Slava Rozhnev, 2021-02-16
@rozhnev

Possible implementation option (one of the possible):
When you click on the button:
- block the button
- display the inscription "wait for a response"
- AJAX - request to the server
- on the server we put the request in the queue
, the client either waits for the request from the queue to be executed or does not wait, but periodically checks the status to unlock the button and show "OK"
Instead of AJAX, you can fasten a WEB-socket, then the server will be able to notify the client about the end of the operation.
On the server, provide a request check for uniqueness so that one user does not block the system with many requests, etc.

A
Anton Anton, 2021-02-16
@Fragster

start transaction, lock table exclusively, perform operations, commit transaction
https://dev.mysql.com/doc/refman/8.0/en/lock-table...
In principle, the blocking and waiting on it will only be for the duration of the transaction, which should be much less than the total execution time of the script

D
darkfriend, 2021-03-15
@darkfriend

Perhaps it is worth looking towards semaphores, the same mutex for example.
It can be implemented through a database, or through a file or, for example, through redis... Have you
considered it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question