title: Data structure to manage a maximum number of requests per minute for an api
body: <p>I need to send data to an external api, but this API has a limit of requests per endpoint(i.e: 60 requests per minute).</p>
  <p>The data come from Kafka, then every message goes to redis(because I can send a request with 200 items). So, I use a simple cache to help me, and I can guarantee that if my server goes down, I wont lose any message.</p>
  <p>The problem is, that there are moments when the Kafka starts to send to many messages, then the redis starts to grow(more than 1 million of messages to send to the api), and we can not make requests too fast as messages come in. Then, we have a big delay.</p>
  <p>My first code was simple:  <code>ExecutorService executor = Executors.newFixedThreadPool(1);</code><br />
  This works very well, when there are few messages, and the delay is minimal.</p>
  <p>So, the first thing that I did was change the executor to: <code>ExecutorService executor = Executors.newCachedThreadPool();</code><br />
  So I can demand new threads as I need to make the requests to  the external api faster, but, I have the problem with the limit of requests per minute.</p>
  <p><em><strong>There are endpoints that I can make 300 requests per minutes, others 500, others 30 and so on.</strong></em></p>
  <p>The code that I did is not very good, and this is for the company that I work, so, I really need to make this better.</p>
  <p>So, every time that I am going to request the external api, I call the makeRequest method, this method is <em>synchronized</em>, I know that I could use a synchonized list, but I think that a synchronized method is better at this situation.</p>
  <pre><code>// This is an inner class
  private static class IntegrationType {
  
      final Queue&lt;Long&gt; requests; // This queue is used to store the timestamp of the requests
      final int maxRequestsPerMinute; // How many requests I can make per minute
  
      public IntegrationType(final int maxRequestsPerMinute) {
          this.maxRequestsPerMinute = maxRequestsPerMinute;
          this.requests = new LinkedList&lt;&gt;();
      }
  
      synchronized void makeRequest() {
          final long current = System.currentTimeMillis();
          requests.add(current);
          if (requests.size() &gt;= maxRequestsPerMinute) {
              long first = requests.poll(); // gets the first request
  
              // The difference between the current request and the first request of the queue
              final int differenceInSeconds = (int) (current - first) / 1000;
             
              // if the difference is less than the maximum allowed
              if (differenceInSeconds &lt;= 60) {
                  // seconds to sleep.
                  final int secondsToSleep = 60 - differenceInSeconds;
                  sleep(secondsToSleep);
              }
          }
      }
  
       void sleep( int seconds){
          try {
              Thread.sleep(seconds * 1000);
           } catch (InterruptedException e) {
              e.printStackTrace();
           }
       }
  }
  </code></pre>
  <p>So, there is a Data Structure that I could use?
  What considerations should I take?</p>
  <p>Thanks in advance.</p>