AWS SQS: Decode the Secret Power Of Short & Long Polling
Tags: sqs, aws, serverless, awsbigdata
Published: 2023-12-19 06:25:53

Discover the dynamics of Short & Long Polling in AWS SQS. When using the ReceiveMessage API call, SQS can be configured to perform short or long Polling, which will retrieve data from distributed components. Short Polling fetches a subset of servers, while long Polling queries all servers for message retrieval. let's understand each Polling mechanism for seamless integration into your application.

## What is Short & Long Polling in SQS?

While invoking the ReceiveMessage API call in SQS, the option to configure Short & Long Polling arises.

### Background

SQS, a distributed queue, distributes messages across various components in different regions. Short & long polling capitalises on this architecture, enhancing accessibility to fetch data from these distributed components.

**Short Polling:** This polling method optimise the user experience by sending data seamlessly fast by extracting the message in the subset of the server which stores the message.

**Long Polling:** On the opposite to short polling, this method search all the servers to fetch the messages in the server, which will take time to fetch all the server even there is no messages in those server.

![Mind map of polling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2qs29odnzx2k1i5h59p.png)

## Consuming Message through Short Polling

We Will Invoke SQS in two scenarios: an empty queue and a queue with data. For short polling, set WaitTimeSeconds to 0.

**Code**
On the code level, to make the api call to short polling you need to specify the parameter `WaitTimeSeconds` to 0, and `MaxNumberOfMessages` to 3 (you can set as much you want).

```
const aws = require('aws-sdk')
const sqs = new aws.SQS({ region: 'us-east-1' });

const shortPolling = async () => {
    try {
        const params = {
            QueueUrl: 'Queue-URL',
            WaitTimeSeconds: 0,
            MaxNumberOfMessages: 3
        }
        const data = await sqs.receiveMessage(params).promise();
        console.log('[INFO] Received messages from SQS by short polling', JSON.stringify(data));
    } catch (error) {
        console.log('[ERROR] Error while short polling the sqs', error);
        throw error;
    }
};
```

**Scenario1:**
When the queue is empty, SQS quickly looks over the server and returns an empty message.

![Short Polling Empty scenario](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtdlafc0xktyo2cobbd2.png)

**Scenario2:**
let assume you have 5 message in your queue and  make a query to get those message. SQS fetches a subset of the server and returns messages such as MSG0,3(message with number postfix of 0 and 3). 

![Response Short Polling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njva114na3zdpfxraa0b.png)

For subsequent calls, it returns Message 2 as it finds the third message in the server.

![Second Short polling response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjji2cm3ra48jgi90458.png)

## Consuming Message through Long Polling

Let's Invoke SQS in two scenarios: an empty queue and a queue with data. For long polling, set WaitTimeSeconds to 20 seconds.

**code**
below code will be used for both the scenario, here i have specified the WaitTimeSeconds to be 20 seconds and maxMessage to be 3, this will make long polling request.

```
const aws = require('aws-sdk')
const sqs = new aws.SQS({ region: 'us-east-1' });

const longPolling = async () => {
    try {
        const startTime = new Date().getTime();
        const params = {
            QueueUrl: 'https://sqs.us-east-1.amazonaws.com/693651664797/PollingQueue',
            WaitTimeSeconds: 20,
            MaxNumberOfMessages: 3
        }
        const data = await sqs.receiveMessage(params).promise();
        const endTime = new Date().getTime();
        console.log('[INFO] Time taken for long polling', `${endTime - startTime} ms`);
        console.log('[INFO] Received messages from SQS by long polling', JSON.stringify(data));
    } catch (error) {
        console.log('[ERROR] Error while long polling the sqs', error);
        throw error;  
    }
};
```

**Scenario1:**
When attempting to invoke the queue devoid of data on the server, SQS patiently awaits for a duration of 20 seconds, which is scanning through all distributed servers to fulfil the request, ultimately responding an empty array.

![Long Polling no message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oodfj9rt5j8fuwj1jxge.png)

**Scenario2:**
For this polling it will take all 20 seconds to fetch all the messages in the server, and after 20 seconds it return the response.

![Long Polling response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1u6v42bmjruz40lyd3k2.png)

## Summary

Harness the potential of Short & Long Polling in AWS SQS. Grasp the intricacies of each polling mechanism, tailor them to your scenarios, and optimise message retrieval for a responsive and efficient application. Tailor your SQS interactions with precision, leveraging the strengths of short and long polling based on your use case.