Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX long polling, increasing efficiency

Tags:

php

For a while I've been playing with the idea of long polling for my notification system, but i've never been able to think of a way to make it more efficient for my backend.

Most implementations I have seen hold the connection open, and the php queries the database server every few seconds to see if new data has been aded. This strikes me as no better than having the javascript repetitively poll the server.

In either case I have my database servers being hit tens thousands of times, which is understandably not particularly desirable.

Are there any systems in place that could 'alert' the executing/sleeping long polling script to the new data?

like image 628
Charles Harris Avatar asked Aug 01 '10 08:08

Charles Harris


People also ask

What is AJAX long polling?

Long polling is the simplest way of having persistent connection with server, that doesn't use any specific protocol like WebSocket or Server Side Events. Being very easy to implement, it's also good enough in a lot of cases.

Why is long polling resource intensive?

Long polling is more resource intensive on the server than a WebSocket connection. Long polling can come with a latency overhead because it requires several hops between servers and devices.

Is long polling scalable?

Kafka is an example of technology implementing long polling model, and that's the reason why it is more scalable compared with RabbitMQ when there is a massive amount of messages and clients.

What is long polling How does it work and why would you use it?

HTTP Long Polling is a technique used to push information to a client as soon as possible on the server. As a result, the server does not have to wait for the client to send a request. In Long Polling, the server does not close the connection once it receives a request from the client.


1 Answers

Single system

If your application is the only system that changes the database, then you can trigger your listeners only when your application performs changes (ideally only for changes to the entities which are interesting for each listener).

Multiple systems

In the case of multiple systems accessing/changing the database, you can either

  • work with database triggers

or if you don't want to do that (I usually avoid it), you can either

  • make sure that all other systems accessing the database will always notify your application (via some messaging mechanism)

or if that's not possible

  • you can at least optimize by having only one loop that queries the database frequently, and informs all listeners at once (so you don't have a loop for each listener).
like image 86
Chris Lercher Avatar answered Oct 30 '22 22:10

Chris Lercher