If I have this PHP code :
<?php
set_time_limit(0);
//mysql connection
while(true)
{
//check for updates in mysql (mysql_query...mysql_num_rows...)
flush();
ob_flush();
sleep(3);
}
?>
My Questions :
Won’t this code overload my MySQL server?
If I will have 1000 users online won’t this code cause problems?
Yeah, it will. It scales badly.
It would be better to put a fast layer in between, like a memcached or a redis. Something that runs in RAM.
There is no need to let every user check if the database did an update. Let one check it, and share the results with the others using the fastlayer.
Check the fast-layer when the last database-query was done. If it was more than 3 seconds ago, do a new db-query and update the fast layer. Else, just take the value from the fast layer.