Course contentsShow
← Cloud and Infrastructure
Lesson 564 of 2,98910. API Gateway and Event-Driven PatternsFree lesson

Long Polling vs Short Polling

Optimizing queue consumption with long polling to reduce API calls and improve message retrieval efficiency.

Long Polling vs Short Polling

You'll learn how to optimize queue consumption by choosing the right polling method to reduce API calls and improve message retrieval efficiency.

The Core Idea

When consuming messages from a queue (like SQS), your application must repeatedly check if new messages are available. There are two ways to do this:

Short polling is like quickly peeking into your mailbox every few seconds. Each check is instant, but if there's no mail, you wasted a trip. You make many quick requests, and most return empty-handed.

Long polling is like waiting at your mailbox for up to, say, 20 seconds. The mailbox "holds" your request open until mail arrives or the wait time expires. You make fewer requests overall and catch messages as soon as they arrive.

Why Long Polling Wins

With short polling, you might make hundreds of API calls per minute, most returning no messages. Each call costs money and creates unnecessary network traffic.

With long polling, your receive request stays open (typically up to 20 seconds). If a message arrives during that window, it's returned immediately. If not, the request completes empty after the timeout. Either way, you've made just one API call instead of dozens.

Efficiency Impact

  • Short polling: High API call volume, increased costs, delayed message detection
  • Long polling: Fewer API calls, lower costs, near-real-time message delivery

Most modern queue services default to or strongly recommend long polling. In AWS SQS, you enable it by setting the ReceiveMessageWaitTimeSeconds parameter to 1-20 seconds.

Key Takeaway: Long polling dramatically reduces the number of empty queue checks by keeping connections open, cutting costs and improving message retrieval speed compared to short polling's rapid but wasteful repeated checks.