Sounds to me like you run DDOS on your database server. So no wonder it goes down. Either put some throttling on the incoming connections or look at clustering several mysql servers.
Improving the process of storing and updating data on the server with a large number of simultaneous processes.
I have hundreds of processes on one server. In the future, I want 1000 processes, or even 10000. Each process is connected to websocket and it receives data and updates the mysql table with this data (There are three fields in mysql in total: name, body, updated_at). Name - the name of the process, body - incoming data in json format, updated_at - the time of data update.
Data on average comes in about 0.1 seconds, rarely the time reaches 1 second and a couple of processes the websocket data comes in more than 100 seconds and they update the body and updated_at fields by the name field. One process - one name field.
The fact is that already with 100 processes an error occurs several times a day and all processes are restarted: PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in. mysql settings: 1) max_allowed_packet = 500M, 2) [PDO::ATTR_PERSISTENT => true].
Please tell me if there is a way to improve this structure or through what better tool (for example, not mysql, but something else) to put into practice the storage, updating and collection of data with hundreds or thousands of processes, so that it is much faster and more reliable. The server that is being used: 2 vCPUs, 2 GB memory. Server is not powerfull, but I think if we improve the process above, which I described, it will be possible to run 1000 processes on such a weak server.
Here is a simplified version of the code. And there are about 100 such php scripts running:
<?php
use WebSocket\Client;
require dirname(__DIR__) . '/vendor/autoload.php';
if (!isset($argv[1])) die('Give right arguments!' . PHP_EOL);
$parameter = $argv[1];
// connection to socket
$client = new Client(
'wss://websocket.com:9999/' . $parameter,
['timeout' => 200]
);
// connection to db
try {
$connect = new PDO(
'mysql:host=' . MYSQL_HOST . ';port=' . MYSQL_PORT . ';dbname=' . MYSQL_DB . '',
MYSQL_USER,
MYSQL_PASSWORD,
[PDO::ATTR_PERSISTENT => true]
);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo '[ERROR]' . $e->getMessage() . PHP_EOL;
throw new Exception();
}
while(true) {
$body = json_decode($client->receive(), true); // wait, when data will get
$sth = $connect->prepare(
/** @lang sql */
"UPDATE `table` SET `body` = :body, `updated_at` = :updated_at WHERE `name` = :name"
);
$sth->execute([
'name' => $parameter,
'body' => json_encode($body),
'updated_at' => date('Y-m-d H:i:s')
]);
}
Please or to participate in this conversation.