Ajax polling vs SSE (performance on server side)
Asked Answered
D

2

1

I'm curious about if there is some type of standard limit on when is better to use Ajax Polling instead of SSE, from a server side viewpoint.

  • 1 request every second: I'm pretty sure is better SSE
  • 1 request per minute: I'm pretty sure is better Ajax

But what about 1 request every 5 seconds? How can we calculate where is the limit frequency for Ajax or SSE?

Doti answered 29/4, 2019 at 20:4 Comment(4)
Missing from your connection are important details like how often is there actually new data to send from server to client? A lot of empty polls are really inefficient. How quickly does the client need to see that data? This determines appropriateness of push vs. poll.Singley
Yes but I'm more concern about the server side, not the user experience. Suppose we need new data every 10 seconds, is not a waste of resources to keep a connection open on the server for 10 seconds doing nothing?Doti
No, it is not a waste of anything to keep an idle connection open for 10 seconds. In fact, it's saving you lots of overhead in establishing a new connection every 10 seconds. Think about this. If your friend was going to say "beep" to you every 10 seconds on the phone, would they make an initial call, stay on the line and just say "beep" to you every 10 seconds or would they call you, say beep, hang up, call you again in 10 seconds, say beep, hang up, etc... Repeat after me. An idle connection doesn't cost you anything, but establishing a new connection over and over does.Singley
Off the top of my head, this seems like a premature optimization problem. If you really have too many users (an unusual problem) then you could optimize the frontend to relax the frequency after a certain period of inactivity, or do what YouTube does: "Are you still there?"End
S
8

No way is 1 request per minute always better for Ajax, so that assumption is flawed from the start. Any kind of frequent polling is nearly always a costly choice. It seems from our previous conversation in comments of another question that you start with a belief that an open TCP socket (whether SSE connection or webSocket connection) is somehow costly to server performance. An idle TCP connection takes zero CPU (maybe every once in a long while, a keep alive might be sent, but other than that, an idle socket does not use CPU). It does use a bit of server memory to handle the socket descriptor, but a highly tuned server can have 1,000,000 open sockets at once. So, your CPU usage is going to be more about how many connections are being established and what are they asking the server to do every time they are established than it is about how many open (and mostly idle) connections there are.

Remember, every http connection has to create a TCP socket (which is roundtrips between client/server), then send the http request, then get the http response, then close the socket. That's a lot of roundtrips of data to do every minute. If the connection is https, it's even more work and roundtrips to establish the connection because of the crypto layer and endpoint certification. So doing all that every minute for hundreds of thousands of clients seems like a massive waste of resources and bandwidth when you could create one SSE connection and the client just listen for data to stream from the server over that connection.

As I said in our earlier comment exchange on a different question, these types of questions are not really answerable in the abstract. You have to have specific requirements of both client and server and a specific understanding of the data being delivered and how urgent it is on the client and therefore a specific polling interval and a specific scale in order to begin to do some calculations or test harnesses to evaluate which might be the more desirable way to do things. There are simply too many variables to come up with a purely hypothetical answer. You have to define a scenario and then analyze different implementations for that specific scenario.

Number of requests per second is only one of many possible variables. For example, if most the time you poll there's actually nothing new, then that gives even more of an advantage to the SSE case because it would have nothing to do at all (zero load on the server other than a little bit of memory used for an open socket most of the time) whereas the polling creates continual load, even when nothing to do.

The #1 advantage to server push (whether implement with SSE or webSocket) is that the server only has to do anything with the client when there is actually pertinent data to send to that specific client. All the rest of the time, the socket is just sitting there idle (perhaps occasionally on a long interval, sending a keep-alive).

The #1 disadvantage to polling is that there may be lots of times that the client is polling the server and the server has to expend resources to deal with the polling request only to inform that client that it has nothing new.

How can we calculate where is the limit frequency for Ajax or SSE?

It's a pretty complicated process. Lots of variables in a specific scenario need to be defined. It's not as simple as just requests/sec. Then, you have to decide what you're attempting to measure or evaluate and at what scale? "Server performance" is the only thing you mention, but that has to be completely defined and different factors such as CPU usage and memory usage have to be weighted into whatever you're measuring or calculating. Then, you may even need to run some test harnesses if the calculations don't yield an obvious answer or if the decision is so critical that you want to verify your calculations with real metrics.

It sounds like you're looking for an answer like "at greater than x requests/min, you should use polling instead of SSE" and I don't think there is an answer that simple. It depends upon far more things than requests/min or requests/sec.

Singley answered 30/4, 2019 at 1:39 Comment(4)
OK, I understand the CPU is not a problem in SSE, but then what about memory? suppose new data every 5 seconds (always is new data every 5 seconds), if server is Apache+PHP, every SSE connectiion will require near 20MB, 200 users is 4GB all the time, is not better to have 4GB only every 5 seconds?Doti
@Doti - I would not think that Apache+PHP would be the right technology for continuous data transfer. Certainly not if you're firing up a PHP instance for every connection. Instead you want a true server that has no additional overhead per connection other than a socket. So, if this is a Apache+PHP optimization question, then the bigger fish to fry is in Apache+PHP. With a real server just accepting incoming sockets, you'd have very tiny overhead for each new socket. Your question doesn't mention Apache+PHP at all. That's where your issue is. Wrong technology for this type of problem.Singley
@Doti - FYI, all my experience with webSockets is using node.js which is particularly good at managing lots of connections and lots of inactive connections because it doesn't use threads for connections. A design or environment where there was a thread or process per connection would be disastrous for scaling lots of connections. So, first off, you have to choose the right server architecture for managing a bunch of connections.Singley
Excellent response to a hopelessly vague question. The two biggest takeways: Any kind of frequent polling is nearly always a costly choice., and I don't think there is an answer that simple. It depends upon far more things than requests/min or requests/sec. Also: for those servers that support it, WebSockets can indeed be a good solution.Iterative
I
0

"Polling" incurs overhead on all parties. If you can avoid it, don't poll.

If SSE is an option, it might be a good choice. "It depends".

Q: What (if any) kind of "event(s)" will your app need to handle?

Iterative answered 29/4, 2019 at 20:11 Comment(3)
The question is actually what is the limit for SSE vs Polling. Because SSE needs to keep a process active all the time, for every user. This is good if we need frequent updates but, what is "frequent" here? 1 per second? every 5 seconds? 20 seconds is not frequent and is better polling?Doti
Oh - I thought you already knew that. The limit for SSE is 42. But the REAL question here: Q: Do you have anything you can use as an "event", so that you don't have to "poll"? Exactly what "state change" is it that you wish to be "notified" of?Iterative
Could be anything, for example suppose you want to update the price for some stock. If you update it every seconds is obvious SSE is a good choice, but if you update it every minute? is a good idea anyway to use SSE?Doti

© 2022 - 2024 — McMap. All rights reserved.