Event Source Performance

I am currently working on a large project that requires the implementation of events sent by the server. I decided to use the event-event transport for him and started with a simple chat. Currently, the client side only listens for a new chat message event, but in the future the project will have many more events. Firstly, I really worry about the server side of the script and get hung up on it, and secondly, I'm not sure that using mySQL database as storage (in this case for chat messages) is actually a good practice. The current loop issues new messages as they appear in the database:

$statement = $connect->prepare("SELECT id, event, user, message FROM chat WHERE id > :last_event_id");
while(TRUE) {
    try {
        $statement->execute(array(':last_event_id' => $lastEventId));
        $result = $statement->fetchAll();
        foreach($result as $row) {
            echo "id: " . $row['id'] . "\n";
            echo "event: " . $row['event'] . "\n";
            echo "data: |" . $row['user'] . "| >>> \n";
            echo "data: " . $row['message'] . "\n\n";
            $lastEventId++;
        }
    } catch(PDOException $PDOEX) {
        echo $PDOEX->getMessage();
    }
    ob_flush();
    flush();
    usleep(10000);
}

, , , - . while() (?) usleep().

, , :

  • - (1000-5000 -)?
  • , ?
  • mySQL ?

, , .

+2
2

1000 ? Apache PHP? , , : , Apache PHP. , , 20 , 20 1000 . , 12 , - 12 1000 . ( M2.xlarge EC2 17 , , 500-1000, , .)

10- , . , , MySQL , , memcached. , , . , PHP, , , .


: OP , usleep(10000) 0.01, 10 . ! :

memcached, - , - PHP . MySQL MySQL; , , , . - , PHP- . , , , . ( , , .)

PHP script socket_select() 15- . , , . (15- - - , SSE keep-alives.)


( 20 12 )

+3
  • - , - (1000-5000 -)?

, -. , , php-, c/++

  • , ?

memcached , / mysql db

  • mySQL ?

, , , - master-slave, db

,

+2

Source: https://habr.com/ru/post/1532732/


All Articles