What is the best practice for PHP to constantly check for changes to the database?

I am creating a backend module (written in PHP) that will be used to monitor private chat rooms that are not active for [Exactly] 300 seconds (5 minutes) . If so, the script will update the database (sets the maximum number of users to a certain number and other things). I keep track of the downtime by the difference in time now () and the last message sent.


What I did: set a cron job that will run (via php-cli) my monitoring script every minute or 60 seconds. Inside the monitoring script:

$expire_time = time() + 60; //this loop will run for 60 seconds while(time() < $expire_time) { $idle_time = get_all_chatrooms_idle_time(); foreach($idle_time as $s_time) { if($s_time >= 300) { update_changes(); } } usleep(500000); } 

The condition of instant configuration of maximum users after 300 seconds of inactivity cannot be achieved. Therefore, I can’t follow the advice, for example: “Avoid doing anything until something really requires it,” although this makes a lot of sense.

Cause? The data of active and inactive chats must be in real time, because they will also be displayed on the dashboard. The paid payment of moderators depends on this.


Why not check every dashboard load? Sorry, but still not possible.

The check should be performed on the server side, and the dashboard is updated itself using ajax, a poll every second.

When I attach the monitoring code to the page requested by my ajax calls, I think it will be more resource intensive than my current implementation (correct me if I am wrong)

Let me give you a rough estimate of the number of users, so that you can imagine what kind of load / traffic we get:

  • number of posts, including moderators: ~ 800
  • number of chats: ~ 250
  • (x) number of moderators in a chat: ~ 50
  • (x) my boss and his staff:

(x) - you can view the control panel


Is there a better way? Am I doing it right?

+6
source share
3 answers

This cycle is redundant. It can run many thousands of times per minute, even on a moderate server, and it generates high CPU utilization even for a real-time application. Add a counter and view the iteration counter. I think this creates an even greater burden than processing each AJAX request.

First of all, determine the granularity in which you need information. Suppose you decide to have 3 seconds of granularity (for example, sweeping a database every 3 seconds) - this number may be too high for you, but it illustrates that you are not losing much. When AJAX pulls every second, you CAN see some counters that need to crawl continuously to scan back once or twice. (Will you really see that this depends on the nature of your counters.)

If your counters are based on data in the range of seconds (for example, showing the sum of elapsed seconds or the sum based on $ / sec), then secondly, the AJAX pull will not provide continuous counters. (Sometimes it skips the second or updates to the second second for network reasons).

Regardless of the granularity you choose, your final statistics will be fine, because they are based on absolute timestamps - no matter how late they are estimated.

If the second time, AJAX polling is used to implement a smooth counter, than you can do much better than this: the count should be done on the client side (for example, sending values ​​with their second step: revenue: <span data-inc="25">14432</span> and counting using JS). The only AJAX tool to monitor the status of stop / reseller counters. Then you only need to determine how long the alert can be late (for example, 10 seconds), then the counters will skip for max. 10s - return to the expected value. In this case, you should not clean the database more often (for example, half the interval). This allows, for example, for a 3-second sleep in your cycle, which drastically reduces the load.

If you can easily add the addition of a timestamp for the expiration of each chat to the database (either in the record or fixed) with an index that will speed up the reading of bits (and additionally allow expiration rules).

+1
source

In the comments, I recommend using technology such as node.js , which is an I / O with even control, an asynchronous platform for creating applications. If the chat is an external application, you can easily open the socket and listen to the chat room, register it, check user activity and listen to certain events.

+1
source
 #!/usr/bin/php <?php if( file_exists('/tmp/chatrooms_cron.lock') ) { die( 'There is already a script running.' ); } file_put_contents( '/tmp/chatrooms_cron.lock', 1 ); // Storing pid would be better // Run loop forever while( true ) { $idle_time = get_all_chatrooms_idle_time(); foreach($idle_time as $s_time) { if($s_time >= 300) { update_changes(); } } sleep( 60 ); } 

Tip for node.js (with socket.io):

I use node.js to work fine on my own! Will work in real time and work with browsers up to ie5 / ie6. This can be done internally in node.js.

 setInterval( function() { // Fetch and update peers }, 300000 ); 
+1
source

All Articles