Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quarkus Rest-Client Throttling/Rate Limiting

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)?

like image 337
jaivalis Avatar asked Sep 06 '25 03:09

jaivalis


1 Answers

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>
like image 72
Felipe Windmoller Avatar answered Sep 07 '25 23:09

Felipe Windmoller