title: AWS Parameter Store : AWSSimpleSystemsManagementException: Rate exceeded
body: <p>At the moment, I need to retrieve the last 10 values of some parameters in AWS Parameters Store</p>
  
  <p>I am using the following code in kotlin :</p>
  
  <pre><code>    val p1 = retrieveAllValidVersions("P1")
      val p2 = retrieveAllValidVersions("P2")
      val p3 = retrieveAllValidVersions("P3")
  </code></pre>
  
  <p>Here is the code of the retrieveAllValidVersions</p>
  
  <pre><code>    private fun retrieveAllValidVersions(paramName: String): List&lt;ParameterHistory&gt; {
          val res = mutableListOf&lt;ParameterHistory&gt;()
          val ssmClient = AWSSimpleSystemsManagementClientBuilder.defaultClient()
          var nextToken : String? = null
          do {
              val ssmParams = ssmClient.getParameterHistory(GetParameterHistoryRequest()
                      .withName(paramName)
                      .withWithDecryption(true)
                      .withNextToken(nextToken)
              )
              res.addAll(ssmParams.parameters)
              nextToken = ssmParams.nextToken
          } while (nextToken != null)
  
  
          return validVersions.sortedByDescending { it.version }.take(10)
      }
  </code></pre>
  
  <p>As described in <a href="https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html#limits_ssm" rel="nofollow noreferrer">https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html#limits_ssm</a>, the maximum number of versions for a parameter is 100 </p>
  
  <p>And as described by <a href="https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParameterHistory.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParameterHistory.html</a>, you can only retrieve 50 values in maxResults so I need to make 2 calls for each parameter (because I have more than 50 versions)</p>
  
  <p>So each retrieval of my 3 parameters costs 6 queries to SSM</p>
  
  <p>I cache the last 10 values of each param in memory for 5 minutes</p>
  
  <p>The problem is that when multiple instances of my lambdas expire their cache at the same time, they do the retrieval at the same time and </p>
  
  <pre><code>com.amazonaws.services.simplesystemsmanagement.model.AWSSimpleSystemsManagementException: Rate exceeded (Service: AWSSimpleSystemsManagement; Status Code: 400; Error Code: ThrottlingException; Request ID: xxx)
  </code></pre>
  
  <p>For ex, if 3 instances are concerned, that will do 18 requests in less than a second and I will hit the error (note : I do not know exactly if the number of instances that hit this code at the same time is 3, this is just a guess to illustrate that at some point you hit the error)</p>
  
  <p>So I have 2 questions : </p>
  
  <p><strong>First, is there a way to retrieve last versions of a parameter first ?</strong></p>
  
  <p>That way, I will then do half of the requests so I will hit the problem less often !</p>
  
  <p><strong>Second, how do I automatically retry a throttling error ?</strong></p>
  
  <p>I have find this AWS blog [1] post saying that I have to parse the error message but this is an old post (2013) and this is very ugly (the moment AWS changes the message, all the mechanism collapses) !</p>
  
  <p>[1] : <a href="https://aws.amazon.com/fr/blogs/messaging-and-targeting/how-to-handle-a-throttling-maximum-sending-rate-exceeded-error/" rel="nofollow noreferrer">https://aws.amazon.com/fr/blogs/messaging-and-targeting/how-to-handle-a-throttling-maximum-sending-rate-exceeded-error/</a></p>
  
  <p>A final note : I am using the parameters store with "auto-encryption" and IAM, I do not want to store the parameters in my own database nor to cache them in a shared memory cache like redis !</p>