resillience4j ratelimiter spring webflux (reactor)

47 views Asked by At

I have a service that is connected to DB, that produce records, and then for each record I need to call a third-party service keeping in mind certain requests per second limitations (not faster than 200 requests per second let's say).

Flux.defer(() -> R2dbcRepository.selectRecords()) //~80k records
   ....
   .parallel(16)
   .runOn(Schedulers.parallel())
   ...
   .flatMap(record -> callCarsService(record))
   .
Mono<SomeResponse> callCarsService(record) {
   return webClient.get()
            .uri(thirdPartyServiceUri)
            .accept(MediaType.APPLICATION_JSON)
            .retrieve()
            .bodyToMono(SomeResponse.class)
            .transformDeferred(RateLimiterOperator.of(RateLimiter.of("name", config))); //config with limitRefreshPeriod = 1second & limitForPeriod: 200
}

while jvm memory looks fairly stable, container memory gets exhausted at some point if I set it to 180 requests per second let's say. At 150 requests per second it's fine.

I already tried .limitRate and putting RateLimiter on parent Flux, but it doesn't seem to improve memory issues. I'm kinda new to reactor and not sure if I understand how RateLimiter works in reactive environment. (especially if we have R2dbcRepository producing records, but RateLimiter is assigned only to third party service client. I'd appreciate any help. Thanks in advance.

0

There are 0 answers