2

I am using quarkus.rest-client to call an external API and want to limit the frequency of those calls to say 50 per second, such that I don't drown the external service. What is the recommended way of achieving this without a side-car approach (through code)?

jaivalis
  • 488
  • 4
  • 19

1 Answers1

4

You could use the @Bulkhead Microprofile annotation and set the maximum concurrent threads limit for the execution of your method. But, this will only work inside of one instance of your application.

Eclipse Microprofile Documentation

Example copied from the above documentation:

@Bulkhead(5) // maximum 5 concurrent requests allowed
public Connection serviceA() {
   Connection conn = null;
   counterForInvokingServiceA++;
   conn = connectionService();
   return conn;
}
// maximum 5 concurrent requests allowed, maximum 8 requests allowed in the waiting queue
@Asynchronous
@Bulkhead(value = 5, waitingTaskQueue = 8)
public Future<Connection> serviceA() {
   Connection conn = null;
   counterForInvokingServiceA++;
   conn = connectionService();
   return CompletableFuture.completedFuture(conn);
}

You can even set the value on the deploy, so you can change this parameter without a new build.

To use the @Bulkhead, you must add the Fault Tolerance to your project

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-smallrye-fault-tolerance</artifactId>
</dependency>
Felipe Windmoller
  • 1,528
  • 1
  • 12
  • 24