Optimizing AJAX .....
There has been an explosion in the use of ajax in many websites. I dont like to use a technology just because it is there or it is the coolest thing. But in situations where you want to get information from the server every 'n' seconds or minutes, it makes sense to use ajax. But ajax can crash the browser when not used properly in this situation. The things we need to take care when we are making async calls every 'n' seconds are,
1>to keep least number of request objects in the stack.
2>to abort a connection after some timeout period.
3>to release the memory, or deallocating the request object.
But before that first we need to calculate the RTT(Round Trip Time, which is nothing but response time)using which we can calculate 'n', this will help in reducing the load on server.
To calculate the RTT, we can either make a synchronous or asynchronous request of a known page or a image in server of known size which is less than 64kb(the max size in a single ip packet). First record the current time. Then make the request. Record the time after the request gets completed. The difference between the two time is the RTT.
This call should be made when the page loads. If data being fetched is from database then make a request to a page which connects to database and retrieves data. This will give the health of the server. To further optimize and reduce the load on server make this RTT expire every 'm' seconds and recalculate RTT.
After calculating the RTT, set the value of 'n' to RTT or more. The reason we the value of 'n' should be greater than RTT is let's say 'n' is less than RTT, the client will be making two or more requests before the response has arrived. If the same thing is done by let's say 10000 clients simultaneously, the server will endup processing more number of requests per client. This will affect the performance of the server considerably.
Set a timeout for each connection based on the value of the RTT. If the response doesnt come within the timeout period , abort or discard the connection. Release the request object.
These steps will ensure smooth functioning of a AJAX application, and also enabling server to service more users with the current infrastructure.
UPDATE: 25th SEP 2006
I had implemented this logic and had faced several problems in a high traffic site, but in a testing environment it worked fine. There are some things which needs to be done additionally to have a smooth ajax application without much errors. You can actually ignore RTT, by making requests only when the previous request gets finished. This approach has 2 advantages,
1> you wont crash the browser and also there wont be stack overflow.
2> will be kindof dynamic, so when the load on the server is more and the response time increases there wont be many requests piling up. The browser blocks ajax calls or gets data from cache when calls are made to the same url again and again. This is true in IE. To avoid this, add a dummy field(timerasdf) in the query string with current time(bcoz its unique) as its value.
Whenever the there is no response, keep a timeout so that you can make the request again. This approach has solved many problems, and also has reduced the load on server.
| sun | pvs
0 comments:
Post a Comment