Page 1 of 2

server got stuck

Posted: Fri Nov 20, 2009 8:32 pm
by dell6400
I have developed a application where I have used date and time counter which is completely dynamic. Not only that count dynamic but also other some information is also dynamic. I am explain you how it is dynamic. It is completely real time function with ajax and jquery. If someone change the date and time directly in database it will be display instantly in fron page where user can't feel or see the change because it is in real time. Similarly user can insert the data in fron page in real time. I mean without any page refresh. In my home page there could be about 20 to 30 counter for different items and all have real time fuction where user can enter data and see the change instantly in real time. I have developed This application use php, mysql and ajax and jaquery.

here My problem is that if there are more user then the site got stuck. what could be the reason? big http request? mor highg database connection? due to this system got over load and CPU usage goes high.

Please anyone can help me to get ride of this problem. where could be the problem

Thanks

Re: server got stuck

Posted: Sat Nov 21, 2009 12:57 am
by requinix
dell6400 wrote:here My problem is that if there are more user then the site got stuck. what could be the reason?
Something must have jammed it up. Clearly. Because "stuck" is such a descriptive term.

What's happening? Timeouts? Delays? Errors?

Re: server got stuck

Posted: Sat Nov 21, 2009 3:40 am
by iankent
dell6400 wrote:...completely real time function with ajax and jquery. ... because it is in real time .... real time. ... about 20 to 30 counter ... all have real time fuction ... instantly in real time.

system got over load and CPU usage goes high.
the reason your CPU is so high is because you have 20-30 counters in 'real-time'. You haven't specified what real-time is, because it certainly isn't real-time in the case of javascript, however I'm guessing you've got your timeouts set incredibly low so that the page seems to update instantly? not good...

say you've chosen a timeout of 10ms. 20-30 counters executing code every 10ms isn't going to happen. Just the time to execute those timers will exceed the timeout period. this will be even worse if you're using synchronous ajax calls (i.e., you send the request and javascript waits for a result), as any delay retrieving data from the server will quickly stack up.
edit: this means you've got lots of timers all waiting on previous timers to complete, and while they're waiting to even start running any code, the timers are adding more to the waiting list! it's just a case of running out of time

first off you need to reduce how much code is being run all the time. make your timeouts at least a few seconds (will anybody even notice if one page updates a few seconds after somebody changes it?!), and preferably a bit longer if you need to have 20-30 of them!!

secondly, do you need that many counters? can some of those be merged into one? for example, if you're updating 30 separate values on the page, why not have 1 counter and update them all in one timer? at least try to minimise the number of timers, each one has its own overhead that'll be using up precious resources

finally, can you do the same thing with your HTTP requests and merge them? instead of sending off a single request for each bit you want to update, consider getting PHP to return an XML document containing ALL of the updates for the page, then using javascript to process that XML file and update the page in one go. A simple way is to use an XML document containing an element ID and value pair, then loop through it with javascript assigning the values to the elements with those ID's.

hth

edit:
also, with that many calls to your server from a single user, it wont take many users to overload your server completely

Re: server got stuck

Posted: Sat Nov 21, 2009 5:18 am
by dell6400
it's not like that... I have products so each product hold one counter with date and time. each date and time is coming from database with the hepl of ajax and j query. Similarly once user do anything with the products the dataase should got updates. so I can't minimize the counter.

so if there are about 100 concurrent users then I think there will be a since connection for database right? might be that is causing problems? if it is the problem how other web site are running without problems...

Re: server got stuck

Posted: Sat Nov 21, 2009 5:26 am
by iankent
dell6400 wrote:it's not like that... I have products so each product hold one counter with date and time. each date and time is coming from database with the hepl of ajax and j query. Similarly once user do anything with the products the dataase should got updates. so I can't minimize the counter.

so if there are about 100 concurrent users then I think there will be a since connection for database right? might be that is causing problems? if it is the problem how other web site are running without problems...
if I'm understanding you correctly then you have to. if you have that many timers running at near real-time, its going to be putting a massive load on your browser and a huge load on your server with multiple users.

other websites do exactly as I suggested - minimise the number of timers, minimise the number of server requests, and increase the time between requests. Facebook for example sends off a single request to the server every 10 seconds or so to retrieve all the updates for the page. A long way from your 30+ timers running near real-time.

if you have 100 concurrent users, then all of those users will be running all of the timers. i.e., if you have 30 timers running code every 5 seconds for one user, 100 users means 300 timers every 5 seconds. if every timer is generating a single HTTP request, that's 60 requests a second on your server, not very sustainable!

Re: server got stuck

Posted: Sat Nov 21, 2009 5:33 am
by dell6400
As you said minimize the no. of counter but it depends on the product listing on the site. each product hold a sing counter. as well how to minimize the http request?

as you said, how I can send a single request if 100 user uses the page from 100 different places.

Thanks

Re: server got stuck

Posted: Sat Nov 21, 2009 5:36 am
by iankent
dell6400 wrote:As you said minimize the no. of counter but it depends on the product listing on the site. each product hold a sing counter. as well how to minimize the http request?
Say you have 100 products listed on the page. Instead of giving each of them a counter/timer why not use a single timer which checks for updates for all 100 products in one go? For example, your ajax code could loop through all the listed products and retrieve their product ID's, then send this information off to PHP in a single request, and process the results you get back to update the relevant parts of the page. That would reduce both your counters and server requests in one go!
dell6400 wrote:as you said, how I can send a single request if 100 user uses the page from 100 different places.
You have one request per user, not one request for all users.

Re: server got stuck

Posted: Sat Nov 21, 2009 5:38 am
by dell6400
yes exactly one request for one users. but I have not alternative for that. how this site works swoopo.com it also do same isn't it?

Re: server got stuck

Posted: Sat Nov 21, 2009 5:44 am
by iankent
dell6400 wrote:yes exactly one request for one users. but I have not alternative for that. how this site works swoopo.com it also do same isn't it?
it appears to be sending one request every 1 second which updates all of the info on the page at once. it doesn't use a single timer for each item on the page, just one for the whole lot! (1 second is suitable for their purposes because they're dealing with live bidding, but thats still a lot slower than 'real-time')

edit: get Firebug - it lets you view what requests are being sent by a page, and if you enable it while viewing swoopo.com you can see the one request per second being sent and what info its getting back

Re: server got stuck

Posted: Sat Nov 21, 2009 5:48 am
by dell6400
yes my problem is also in my live bidding which i attempting to develop. I have loop all the products. and there will be a single call to ajax for all products info. but when there are 100 or 200 users I got server stuck. what could be the solution.

Re: server got stuck

Posted: Sat Nov 21, 2009 5:59 am
by iankent
dell6400 wrote:yes my problem is also in my live bidding which i attempting to develop. I have loop all the products. and there will be a single call to ajax for all products info. but when there are 100 or 200 users I got server stuck. what could be the solution.
if you've only got a single call on the client end and your server is struggling under the load, its because you've got your timeout set too fast or your server just isn't up to the job of serving that many requests. there's not really any solution for this except make less connections to the server, optimize your server-side code, or buy a better server.

without seeing your full code its hard to say, but with so many calls so often you're going to run into capacity issues at some point (which is why Twitter had so many problems a few months back when they had huge numbers of new users, their servers just weren't up to the job!)

Re: server got stuck

Posted: Sat Nov 21, 2009 6:05 am
by dell6400
It should be possible. that's why there are swoopo running life. as in my code, I have send the id of all products in a javascript function and this id call the ajax page with help of jquery and return all required data and with assign all data with help of div. similary if a user try to bid it calls ajax and insert the data in database. I have two files in ajax one for displaying real time changes data and another for user bid to insert in database. data displaying page called in each 1 seconds.

so I want the solutions in this case.

Thanks

Re: server got stuck

Posted: Sat Nov 21, 2009 6:16 am
by iankent
dell6400 wrote:so I want the solutions in this case.
1. Optimise your server-side code to reduce the time it takes to retrieve changes/make updates.
2. Upgrade your server with more RAM and faster CPU
3. Reduce the time between updates on the client-side to something higher than 1 second

Re: server got stuck

Posted: Sat Nov 21, 2009 6:22 am
by dell6400
I tried my best to optimism the code.now there are only few select statement which is not that much time taking. Although I used file instead of taking data from database directly.for server I used ukfast which is really fast. for date and time counter I must use 1 second for it's count down and how real time changes data instantly. isn't there any other kind of solution? can memcached solved it? or any other solution please suggest me sir. Thanks

Re: server got stuck

Posted: Sat Nov 21, 2009 6:29 am
by iankent
dell6400 wrote:I tried my best to optimism the code.now there are only few select statement which is not that much time taking. Although I used file instead of taking data from database directly.for server I used ukfast which is really fast. for date and time counter I must use 1 second for it's count down and how real time changes data instantly. isn't there any other kind of solution? can memcached solved it? or any other solution please suggest me sir. Thanks
only the javascript code contacting the server needs to be throttled, the ones updating the time remaining can be left at 1s (to ensure they update at the right speed), but then you need to separate out your calls to the server into a different timer so they run less often (e.g., update countdown every 1 second but only ask server for updates every 5 seconds)

alternatively, you could use something like a MEMORY table in mysql for retrieving the results (then you need to take care to update both the real table and the memory table while doing updates so no data is lost), or you could use memcache or something similar to speed up PHPs end of the data retrieval

also something to bear in mind, if you're using a shared server (which I assume you are), other people have websites running on there too which is using up additional CPU/RAM from the server. Not only does that mean you have considerably less than the full power of the machine available, but it also means you should take care that the performance of your site doesn't interfere with other users sites.

if you really do need to make this many requests and this often, consider a dedicated server which at least gives you the whole server to yourself!