The Amazon cloud, explained.

Home / Blog / Featured / Adaptive Polling in PHP and SQS

When looking for messages in SQS, there is nothing wrong with your approach (read, sleep, read, etc).

But, for a more sophisticated approach, check out Joel’s sample at http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1941. His calls his approach ‘adaptive polling’.

The motivation for picking a different polling algorithm is efficiency and cost. SQS charges you for every operation (about 1 penny/10, 000 requests), so if you poll every second when there are no messages, you are spending a bit more money than you need to.

Also, if you poll and sleep at regular intervals, you aren’t processing messages as quickly as you could be. For example, if there are 1000 messages in your queue, you don’t necessarily want to sleep for 1 second after reading each one, you might as well try to read them as fast as you can. Strike while the iron is hot as it were :)

I’ve implemented a version of adaptive polling in PHP, I posted that (along with the functor sample) here: http://static.learnaws.com/sample_code/PHPFunctor.zip

All of the polling is done in the AdaptivePoll class; to act on any given message, you simply have to define your bodyFunction method as shown below:

picture-46Using this class lets me quickly create workers for any processing that I want to do. The example above is obviously pretty trivial, but using SQS-driven workers and adaptive polling, I can always be assured that I’ll be as scalable as possible.

One Response to “Adaptive Polling in PHP and SQS”

  1. Learn Amazon Web Services » Blog Archive » Integrating with SalesForce.com via SQS Says:

    [...] using my adaptive poll class to implement my Salesforce integration [...]