Rajan Rawal
Rajan Rawal

Reputation: 6313

Long Polling And Server Behavior

Even though working on PHP for so long, recently came to know about long polling, otherwise I was sending periodic ajax.

It is understood that sending periodic ajax consumes bandwidth ( Considering the case if site have thousands of users). As well server remains busy to serve periodic ajax requests.

Advantage of long polling is that it drastically reduces the bandwidth as ajax responses only when there is change, Unless ajax remains open. However server need to keep on working ( kind of while loop until false condition) until some changes has happened.

My question is, In this type of technique, server has to incur load. Wont it affect the say way as periodic ajax when there are thousands of uses?

Sorry If I am wrong.

Upvotes: 1

Views: 2030

Answers (1)

bsa
bsa

Reputation: 2801

When you say "load", let's consider bandwidth, CPU time, and other resources.

Bandwidth

As you say, periodic ajax consumes bandwidth. There will be an HTTP request-response pair for each poll, even if the response from the server is basically empty. With long polling, the server doesn't respond unless it has something to say. This is where you save bandwidth.

CPU

If your long-polling implementation uses a sleep() on the server side, it will not use many CPU cycles.

For PHP:

Note that in general, you should use a sleep()-like function any time you want to delay in software. Do not use a tight loop without a sleep() for a timed delay.

Other

Depending on the configuration of your server, each active user will probably use a process or thread. Even while it is sleeping, this process or thread will consume some resources, including any allocated memory. Unless you're talking about a huge number of simultaneous users, or your application uses a lot of memory on the server per user, you're unlikely to run into hardware limitations. You might run into a soft-configured limit on the number of threads or processes first.

So, if your application holds a very large amount of memory per active user, long-polling could make you memory-bound with fewer users than a periodic AJAX implementation which allocates and deallocates resources per ajax hit. With good design, this shouldn't be a problem though.

Upvotes: 3

Related Questions