What is skinny during a long poll with ajax and webapi ... is this going to kill my server? and string comparison

I have a very simple long ajax request to vote:

(function poll(){ $.ajax({ url: "myserver", success: function(data){ //do my stuff here }, dataType: "json", complete: poll, timeout: 30000 }); })(); 

I just selected this example this afternoon and it seems to work just fine. I use it to create some html on my page and it is almost instantly as far as I can tell. I'm a little worried that this will cause workflows to open on my server, and if I have too much load on the server, it will be completely stopped. Can someone shed light on this theory? At the back end, I have a webapi service (.net mvc 4) that calls the database, builds the object, and then passes the object back. It also seems to me that in order for this to work, the server had to constantly call the database ... and this may not be good.

My next question is the best way for a client to determine if html needs to be updated on my page? I am currently using JSON.stringify () to turn my object into a string and comparing a string that comes down to the old string, and if there is a delta, it rewrites the html on the page. Right now there is not a whole lot in the object that has gone down, but it can potentially become very large, and I think this string comparison can be quite resource-intensive on the client ... especially if it does it almost constantly.

The bottom line for me is this: I'm not sure I know exactly how long the survey works. I just searched for it and found the above code example and implemented it, and at first glance it is awesome. I'm just afraid that this will happen (on the server), and my way of comparing old results with new ones is going to put out (on the client).

any information you can provide is greatly appreciated.

TIA.

+6
ajax asp.net-web-api long-polling
source share
2 answers

I agree with SLaks - i.e. I use SignalR if you need a real-time web application with the website http://www.asp.net/signalr . It is difficult to implement a long survey well, let someone else handle this complexity, that is, use SignalR (a natural choice for WebApi) or Comet.

SignalR tries to use 3 other communication methods before resorting to long polling, web sockets, events sent by the server, and forever ( here ).

In some cases, you may be better off with a simple survey, i.e. punch every second or so to update ... take a look at this article. But here is a quote:

when you have a high volume of messages, a lengthy survey does not give any significant performance improvement over traditional polls. In fact, this could be worse because long-term polls can get out of hand in an unregulated, continuous cycle of immediate polls.

The fear is that with any significant load on your web page, your 30 second ajax request may be your own denial of service attack.

Even Bayeux ( CometD ) resorts to a simple poll if the load is too much:

Increased server load and resource hunger are eliminated by using the reconnect field and intervals for throttling clients, which in the worst case degrade traditional polling behavior.

Regarding the second part of your question.

If you use a lengthy survey, then your server should ideally only return an update if something really changed, so your user interface should probably β€œtrust” the answer and assume that the answer means new data. The same applies to any approach like Server Push.

If you reverted to a simple polling method, you can use the built-in http methods to detect updates using the If-Modified-Since header, which will allow you to return 304 Not Modified, so the server will check the timestamp of the object and return 200 only with the object if it has been modified since the last request.

+8
source share

OK, my two cents:

  • As others have said, SignalR is checked and code verified, so I would really think about using this instead of writing my own.
  • SignalR changes some of the IIS settings to optimize IIS for this kind of work. So, if you want to implement your own, look at the IIS changes made in SignalR
  • I assume that you are doing a long survey so that your server can implement some form of push server. Just keep in mind that this means turning your stateless HTTP server into a stateful machine , which is not good if you want to scale. A long poll behind the load balancer is not very pleasant :) For me, this is the worst thing that the server presses.
  • ASP.NET uses ThreadPool to service requests. Long polling will result in a ThreadPool thread. If you have too many of these streams, you may end up in hunger (and tears). As a number in the chalet, 100 is not too much, but +1000.
  • Even the SignalR team says that the IIS box optimized for SingalR is probably not optimized for regular ASP.NET, and they recommend separating these fields. Thus, it means cost and overhead.

At the end of the day, I recommend using a lengthy survey if you are solving a business problem (and not because it is just cool), because then it will pay for its costs and overhead and headaches.

+12
source share

All Articles