Airikr
Airikr

Reputation: 6436

How much does $.ajax affects on the server?

I'm currently reloading (refreshing if you prefer that word more) my pages every 5 minutes but this is quite a long time for me. I don't know how much a reload of a page with the code below affects the server and it's just that I want to know; if I change the interval time from 5 minutes to 1 second, how much does this take on the server? Will it slow the server down? My website has over 5 visitors per half hour but it will be more after the update.

$(document).ready(function() {


    setInterval(function() {
        $('#loading-page').show().html('<img src="images/animation-loading.gif" alt="" title="Reloading the page">');

        $.ajax({
            url: 'get/statistics',
            type: 'GET',

            success: function(s) {
                $("#load-statistics").html(s);
            },


            error: function() {
                $('#loading-page').show().html('<img src="images/exclamation-red.png" alt="" title="Couldn't reload page. Trying again soon">');
            }
        });
    }, 300e3);


});

Thanks in advance.

Upvotes: 0

Views: 155

Answers (5)

BuddhiP
BuddhiP

Reputation: 6451

This will be highly dependent on what actions your page performs.

Also, as mentioned 1 second may be too extreme, specially for statistics kind of page, at the same time 5 minutes may be too slow to be useful, of course this will depend on content/nature of your website.

Your server load will depend on request frequency as well as number of concurrent users.

What I wanted to point out here is you can use server side 'cache'ing to reduce server load considerably in this kind of scenarios. Judging by the name of your page, statistics are ideal candidates for server caching. Because they don't have to be 100% accurate, and once generated, can be good for next two minutes (for example) or so.

So if you cache your statistics response, all subsequent requests will be given the chached content, reducing the load on the server. And with the right infrastructure request might not even reach your server (ex: certain network switches can cache and return content).

You will have to find a proper caching mechanism depending on your server side technology.

Upvotes: 1

freedev
freedev

Reputation: 30137

It will very likely depends on what happen server side. Couldn't you talk with the developer that developed the server side service? It will also depends on how many concurrent browser will call this service. Many service are designed to answer million of times concurrently, but usually how many call you can do and how frequently is written into the service documentation.

I think that an empiric approach could be your way, changing the frequency number of seconds or minutes, accordly to the server answer time and the server load (if you can measure it). You can also measure the time spent by your JavaScript since the call is started to the response is received.

Upvotes: 1

palaѕн
palaѕн

Reputation: 73926

You can test it yourself. Try this:

var start = new Date().getTime();

$.ajax({
    url: 'get/statistics',
    type: 'GET',

    success: function(s) {

        var end = new Date().getTime();
        console.log('milliseconds passed', end - start);

        $("#load-statistics").html(s);
    }

});​

Check the console for the processing time. You can then change the interval time based on the average of lets say 10-20 sample time you will get at the console.

Upvotes: 2

leon
leon

Reputation: 772

For that number i.e 5 visitors per half hour, I suspect that there shouldn't be any 'difficulty' in the server handling the updates to the pages, assuming that each update is not a crazy-intensive-long operation that bogs down the server. There are also various caches or caching mechanisms that could further improve performance by only obtaining the changed resources rather than re-producing unchanged resources. When the load increases i.e more users per x-period, then you can invest in tuning your application and/or server. Inspecting with monitoring tools could be a start.

Upvotes: 1

Lyth
Lyth

Reputation: 2211

A simple answer is "5 minutes = 300 seconds", so reducing interval to 1 second will load server 300x times.

It will be a bit less than that, because there may be some browser limitations and delays in the network and so on, but substantial anyway. And think of users and the traffic they generate.

1 second is just too extreme, it's hardly ever needed. You might as well fake it (as some counters do) to create the dynamic effect.

Upvotes: 2

Related Questions