0

I'm trying out long polling for the first time.

In the PHP script I have a while-loop with a sleep-timer that freezes the script for 10 seconds, then it looks for new stuff in the database again.

I'm thinking performance and server/database load/connections:

What is worse for the server: Many GET-requests (ajax), or many opening/closing of the DB connection?

Would it in any way be better to use long polling but close and re-open the DB connection in each round of the while-loop (to free the limited number of connections)?

user1121487
  • 2,662
  • 8
  • 42
  • 63

1 Answers1

0

This is less trivial, than it sounds: What starts with a simple "should I or not" alternative rapidly increases in complexity with scaling to more servers.

Having hit walls with both approaches, we have come up with a proxy scheme, that seems to work well even on cheap shared hosting:

  • Run a single instance of a simple proxy script, that polls the DB (On shared hosting we start this from a cron job, that starts an instance only, if no other is running, so we easily survive reboots)
  • Have that proxy script translate the expensive DB poll into a cheaper poll: SysV SHM and flagfiles in the filesystem both work fine. The proxy should keep its single DB connection open
  • Have your potentiallly many long-pollers check the proxied flag.

This has made it possible to implement a short server-sided polling intervall without running into problems, when concurrent polls increase.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92