1

We develop an application involving a lot of ajax. Such as showing users notifications, updating what they see via ajax while they are on the web page. This update is due to the actions of other users in the application.

  1. In the code base we are generating a query inside setTimeout function of javascript. This makes an ajax call.
  2. Every call is a separate query to the database.
  3. There are many calls on the same page.

So we run queries against the database after 15 seconds, no matter if something changes or not. Will this approach work if we are expecting high traffic?

1

Polling like this can get expensive (in terms of resource usage), but whether or not that causes a problem depends on a lot of factors (number of users, size of db, hardware...).

Maybe you should consider using server sent events instead.

1

Issues will mainly arise due to current browsers' limitations to perform only a limited number of concurrent requests. The number of concurrent request varies by browser but usually within 2-6. So, if a page has multiple setTimeOut(), only the predetermined number of requests will pass through. Others will follow only when those requests are complete.

1

Yes, polling will work. Its not the most sophisticated way of watching for changes, but it will do the trick. We can't tell you if your server will be able deal with it or not, that's just something you'll have to test. But I imagine if you are doing a single, highly optimized database query per poll and returning small amounts of data, you'd be able to handle fairly high traffic.

That said, you might consider long polling or websockets instead. That would require changes to how your server piece worked, but would give you more real-time updates from the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.