<p>If there are 100 messages in the queue and 5 consumers, the initial distribution will be no more than 10-10-10-10-10. </p>

<p><a href="http://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html" rel="noreferrer">A single response can never return more than 10 messages</a>.  </p>

<p>This seems like a non-issue.  </p>

<p>Race conditions related to multiple consumers should be a non-issue as well.  SQS is <em>designed</em> for multiple simultaneous consumers.</p>

<p>Use long polls and a 20 second max wait timer and be amazed. (No, a 20 second wait does not delay messages by 20 seconds.  It doesn't delay them at all.  You sort of need to see it in action to really understand how it works.)   </p>

<p>You're definitely overthinking some things, I suspect.</p>

CC BY-SA 3.0
2017-04-29T00:51:12.370Z
2017-04-29T00:51:12.370Z
Michael - sqlbot
<p>Good architecture in how you use SQS queues will solve your problems. If we assume there are, say, 3 minutes of processing per message, then you can almost guarantee equal distribution of the messages as this is very large compared to the time needed to poll the queue, if you delete the message from the queue only after it has been processed.</p>

<p>Do be aware there is a 12 hour Visibility Timeout limit on any SQS message, so if you dont delete it by then, it will re-appear on the queue. I suspect this is probably not a limitation for you, but do keep it in mind.</p>

CC BY-SA 3.0
2017-04-28T11:04:28.627Z
2017-04-28T11:04:28.627Z
Henry
<p>Long polling is always beneficial since it results in higher performance at reduced cost for the majority of use cases.  Unfortunately, you cannot control the number of messages each worker receives from the queue due to the distributed nature of the queue. But there are some client-side workarounds that can help you in balancing the load for workers.</p>

<p><strong>So, this is what we did as a workaround for this:</strong> </p>

<p>As one of the workaround, the poller script can control the number of messages each worker receives. A threshold can be set for maximum number of messages each worker can handle. This threshold can be a dynamic value and would probably be <code>ApproximateNumberOfMessagesVisible</code> divided by number of pollers/poller scripts. You can then keep the visibility timeout to be any lower value so if all the poller scripts are long polling at the same time, one of the pollers grabs the message, decides it is overloaded based on threshold, does not delete the message, the message goes back to the queue and it could be grabbed by other pollers who still has the capacity to grab the message. The threshold parameter can be fine tuned to meet the application's needs.</p>

<hr>

<p>Also, having failover mechanism would help too, like how <a href="https://iot.stackexchange.com/q/1371/2405">the answers in this post describe</a>. However, I can't afford to have failover queues in a distributed architecture, as it would increase the complexity. So, the above workaround was a better idea for my team.</p>

CC BY-SA 3.0
2017-05-04T03:45:22.720Z
2017-05-04T03:45:22.720Z
Dawny33
<p>I have multiple servers, each having a script polling an SQS queue [all polling the same queue].</p>

<p>So, is there any way I can ensure an equitable distribution of messages to all those clients [i.e. my worker servers here]. Like for example, if there are 100 messages in the queue, then <code>20-20-20-20-20</code> if there are 5 workers, and so on.</p>

<p>Can AWS ELB (Elastic Load Balancer) help me do it?  If yes, then how?  If not, then is there an alternate service in the AWS ecosystem which can help me do it?</p>

<p><strong>Or am I overthinking this?</strong> I mean, can this be solved straightforwardly in the polling script?  [Please keep in mind the race conditions involved due to multiple clients polling a single queue]</p>

CC BY-SA 3.0
2017-04-28T09:59:23.277Z
Maybe [related](https://iot.stackexchange.com/q/1371/2368) :D
Tensibai
<amazon-web-services><amazon-sqs><distributed-systems><aws-elb>
How to ensure equitable distribution of SQS messages in a distributed systems setting?
Dawny33
# How to ensure equitable distribution of SQS messages in a distributed systems setting?

(Asked by: username_0 on 2017-04-28)

I have multiple servers, each having a script polling an SQS queue [all polling the same queue].

So, is there any way I can ensure an equitable distribution of messages to all those clients [i.e. my worker servers here]. Like for example, if there are 100 messages in the queue, then `20-20-20-20-20` if there are 5 workers, and so on.

Can AWS ELB (Elastic Load Balancer) help me do it? If yes, then how? If not, then is there an alternate service in the AWS ecosystem which can help me do it?

**Or am I overthinking this?** I mean, can this be solved straightforwardly in the polling script? [Please keep in mind the race conditions involved due to multiple clients polling a single queue]

**Tags:** amazon-web-services, amazon-sqs, distributed-systems, aws-elb

> **Comments:**
> **username_1**: Maybe [related](https://iot.stackexchange.com/q/1371/2368) :D  

---

## 3 answers

### Answer by username_2 on 2017-04-29. Score: 6

If there are 100 messages in the queue and 5 consumers, the initial distribution will be no more than 10-10-10-10-10.

[A single response can never return more than 10 messages](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html).

This seems like a non-issue.

Race conditions related to multiple consumers should be a non-issue as well. SQS is *designed* for multiple simultaneous consumers.

Use long polls and a 20 second max wait timer and be amazed. (No, a 20 second wait does not delay messages by 20 seconds. It doesn't delay them at all. You sort of need to see it in action to really understand how it works.)

You're definitely overthinking some things, I suspect.


---

### Answer by username_3 on 2017-04-28. Score: 3

Good architecture in how you use SQS queues will solve your problems. If we assume there are, say, 3 minutes of processing per message, then you can almost guarantee equal distribution of the messages as this is very large compared to the time needed to poll the queue, if you delete the message from the queue only after it has been processed.

Do be aware there is a 12 hour Visibility Timeout limit on any SQS message, so if you dont delete it by then, it will re-appear on the queue. I suspect this is probably not a limitation for you, but do keep it in mind.


---

### Answer by username_0 on 2017-05-04. Score: 3

Long polling is always beneficial since it results in higher performance at reduced cost for the majority of use cases. Unfortunately, you cannot control the number of messages each worker receives from the queue due to the distributed nature of the queue. But there are some client-side workarounds that can help you in balancing the load for workers.

**So, this is what we did as a workaround for this:**

As one of the workaround, the poller script can control the number of messages each worker receives. A threshold can be set for maximum number of messages each worker can handle. This threshold can be a dynamic value and would probably be `ApproximateNumberOfMessagesVisible` divided by number of pollers/poller scripts. You can then keep the visibility timeout to be any lower value so if all the poller scripts are long polling at the same time, one of the pollers grabs the message, decides it is overloaded based on threshold, does not delete the message, the message goes back to the queue and it could be grabbed by other pollers who still has the capacity to grab the message. The threshold parameter can be fine tuned to meet the application's needs.

---

Also, having failover mechanism would help too, like how [the answers in this post describe](https://iot.stackexchange.com/q/1371/2405). However, I can't afford to have failover queues in a distributed architecture, as it would increase the complexity. So, the above workaround was a better idea for my team.


---

