We help IT Professionals succeed at work.

Delaying HTTP requests and CPU performance

261 Views
Last Modified: 2012-05-11
I am creating live chat in PHP that will allow all members to chat with each others.

What I am doing now is basically when the member send a chat message, the server record it in the database.

Each client make HTTP request when the page load, the server delay the response until there is a new message that was recorded in the database, then it delete the message and response to the HTTP request with the chat message.

The server keep looping to check if any message is avaiable in the database, I found that the apache was using (66% +) CPU when ONLY one member is online, so I added sleep() method between each loop like the following:

while(true)
{
   // Code here to check if any new chat message is avaiable in the database...
   sleep(3);
}

Open in new window


I tested with one member again after adding sleep() method and now I see it's working normally.

In the case the I have alot of members online (ex: 100,000 online members) I have doubts that this way could cause the server CPU to be used incredibly since it keep processing the above code for each online member.

Is that way will highly affect on the server performace?
Do you recommend using it or not?
What CPU and RAM is more suitable to run website that use that way?
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Author

Commented:
I want to be able to interact with all the clients while they are just opening the website, not doing anything.

This will allow me to show in-time notifications, use live chat, etc...

How would you recommend me to accomplish this without using huge server performance.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Author

Commented:
The idea I posted above will NOT make 50,000 or 100,000 request per second, it will only make one request for each client when they just enter the website, the request will keep delaying until there is a command in the database is awaiting to be sent to the client, after the server response to AJAX with the data in the database, the server will make another request and wait till any data is available in the database for sending, etc...

I don't think this will make problems with internet connection, since it's not sending huge amount of data to many clients at the same time.

The load will be on the CPU! since the PHP script is checking in the database and delaying for few seconds (ex: 3 seconds) and then checking in database again, etc... (No data sending till the server find any updates to send to the client).

The idea here is making AJAX request that don't response till there is data to send to the client.

Can keeping 50,000-10,000 PHP processes running and making SQL query on the database every 2 to 4 seconds affect incredibly on the average server performace? (delaying 2 to 4 with sleep() method)

If Yes, what I need to do to make this idea on the website while the server is working normally.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Author

Commented:
I would use the JavaScript that runs the AJAX interface on the client side rather than trying to sleep on the server side.

Can you please post example?

Thanks,
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Author

Commented:
I have found alot of ways to do that, though, I still don't know what idea that will use less server resources especially CPU.

In the case that the member is opening the website more than once, I want the HTTP response (ex: notification or chat text) to arrive on all the browsers tabs exactly as facebook chat.

Any idea?
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Author

Commented:
Question closed. Thanks,
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.