title: SQS maxNumberOfMessages
body: <p>Using a Java client application I am querying an SQS queue for messages. The queue has 12,000 messages as setup for testing. I am using openJDK with aws-java-sdk latest (software.amazon.awssdk 2.10.62) pom.xml is shown further down.</p>
  
  <p>The issue I am seeing is that despite setting the <strong>maxNumberOfMessages(10)</strong> I only ever get 3. I understand that it is a maximum not a guarantee of the number of messages however there is no waver in the number of messages returned. It is always 3. </p>
  
  <blockquote>
    <p><strong>AWS Documentation:</strong>  MaxNumberOfMessages
        The maximum number of messages to return. Amazon SQS never returns more messages than this value (however, fewer messages might be
    returned). Valid values: 1 to 10. Default: 1.
        Type: Integer
        Required: No</p>
    
    <p><strong>Consuming Messages Using Short Polling</strong></p>
    
    <p>When you consume messages from a queue using short polling, Amazon SQS
    samples a subset of its servers (based on a weighted random
    distribution) and returns messages from only those servers. Thus, a
    particular ReceiveMessage request might not return all of your
    messages. <strong>However, if you have fewer than 1,000 messages in your
    queue, a subsequent request will return your messages.</strong> If you keep
    consuming from your queues, Amazon SQS samples all of its servers, and
    you receive all of your messages.</p>
  </blockquote>
  
  <p>So we have tested two clients in java using both the older aws sdk and the newer one with the same results. Always only 3 messages back. </p>
  
  <p>Interestingly if instead of running the application externally (on my mighty desktop) you run it as an AWS Lambda you get 10 messages. This lambda test was done using JavaScript by a colleague.  </p>
  
  <p>So the question remains why do we only ever get 3 messages per request and seemingly within lambda you can get 10. </p>
  
  <p>Given there is a cost per request is the weighted random distribution based on amazon profit =))</p>
  
  <p>SQS Test Method:</p>
  
  <pre><code>public void SQStart()
  {
      AwsBasicCredentials awsCreds = AwsBasicCredentials.create("accessKeyID", "secretKeyID");
      AwsCredentialsProvider creds = StaticCredentialsProvider.create(awsCreds);
      SqsClient sqs = SqsClient.builder().credentialsProvider(creds).region(Region.EU_WEST_1).build();
      GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder()
              .queueName(QUEUE_NAME)
              .build();
      String queueUrl = sqs.getQueueUrl(getQueueRequest).queueUrl();
  
      for (int x =1; x &lt; 100; x++) {
          ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
                  .queueUrl(queueUrl)
                  .maxNumberOfMessages(10)
                  .build();
  
  
          List&lt;Message&gt; messages = sqs.receiveMessage(receiveMessageRequest).messages();
          if (messages.size() &gt; 3 ) {
              System.out.println("YEY More than 3 Messages: "+ messages.size());
          }
      }
  }
  </code></pre>
  
  <p>POM.XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;groupId&lt;/groupId&gt;
      &lt;artifactId&gt;SQSTest&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;8&lt;/source&gt;
                      &lt;target&gt;8&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;software.amazon.awssdk&lt;/groupId&gt;
                  &lt;artifactId&gt;bom&lt;/artifactId&gt;
                  &lt;version&gt;2.10.62&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
  
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;software.amazon.awssdk&lt;/groupId&gt;
              &lt;artifactId&gt;sqs&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
              &lt;artifactId&gt;httpclient&lt;/artifactId&gt;
              &lt;version&gt;4.5.9&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
              &lt;artifactId&gt;httpcore&lt;/artifactId&gt;
              &lt;version&gt;4.4.10&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-logging&lt;/groupId&gt;
              &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
              &lt;artifactId&gt;aws-java-sdk-s3&lt;/artifactId&gt;
              &lt;version&gt;1.11.720&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
              &lt;version&gt;1.6.1&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>