Search code examples
javaspring-bootresttimedelay

Delay between every Rest api call


we are have one job where we are hitting internal api based on the few 1000 data that we have fetched from DB.

Currently we are hitting external api iteratively and calling internal api. This is result in to spike at that api end. They are suggesting to keep few miliseconds of delay to avoid this surge so that there won't be spike at producer of api end.

At initial thought, sleep can be used to achieve it which is very lower level api. However, I am exploring apis(available in spring-boot or java or other place) which is having more elegant way of considering this usecase.


Solution

  • A simple and common practise is to use some client-side rate limiting library to limit the rate of the client to call the API. Most of the fault tolerance library will provide such feature.resilience4j is the popular one which also provides a starter for spring-boot.

    Basically it allows you to define number of the API call that an client can be made in a given period , and how long the client will wait (i.e. timeout) until it is allowed to call the API.