• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Delaying HTTP requests and CPU performance

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?
0
Mohamed Abowarda
Asked:
Mohamed Abowarda
  • 5
  • 4
4 Solutions
 
Ray PaseurCommented:
I think the first question I would ask is, "Do you have 100,000 internet connections to the server?" and if the answer is "Yes" then you might look into optimizing the data base performance with appropriate indexes.  There is almost never and advantage to adding a sleep() command to the program logic, unless you are deliberately trying to slow down an external process.
0
 
Mohamed AbowardaSoftware EngineerAuthor 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.
0
 
Ray PaseurCommented:
Kind of sounds like a jQuery or similar AJAX application.  If you want to support 100,000 concurrent clients with one or two second response time, you will have to make 100,000 or 50,000 requests per second.  It is no problem to do so because Yahoo, Google and Facebook do exactly this sort of thing.  But these web sites have made a considerable investment in "server farms."

To try to put this into perspective I was in the Yahoo East data center a couple of years ago.  The data center manager told me that his daily workload was defined by how many truckloads of servers he would need to deploy each day.  Usually he added a tractor-trailer full of servers, perhaps 80-100 racks every day.  His floor space was contiguous and covered several football fields.  The largest single expense in running the data center was the cost of electricity to power and cool the equipment.  Even at $900,000 per rack, the KvA and BTU issues dwarfed the cost of the equipment.  It was a eye-opening visit, to say the least!
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
Mohamed AbowardaSoftware EngineerAuthor 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.
0
 
Ray PaseurCommented:
Servers do not send data to clients independent of a request from the client.  The client-server model depends on the client to make the request and the server to respond.  So I think if I were trying to throttle the requests, I would use the JavaScript that runs the AJAX interface on the client side rather than trying to sleep on the server side.
0
 
Mohamed AbowardaSoftware EngineerAuthor 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,
0
 
Ray PaseurCommented:
I don't have any example of this.  I just happen to know this can be done because Google's GMail service does it.  I would expect you could find something useful in jQuery (not sure of that, but it seems likely).
0
 
Mohamed AbowardaSoftware EngineerAuthor 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?
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
Question closed. Thanks,
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now