Wyko Rijnsburger     About     Feed

Rate-limiting API-calls with Reactor 3

I’ve been working with Reactor 3 recently. Reactor is a Reactive library similar to RxJava, but developed for Java 8 by the guys at Spring.

I ran into a situation where I had to call an external API for all items in a stream. That external API only allowed four requests a second, so I had to rate-limit the outgoing calls. Luckily, using Reactor, this is easily implemented.

We can use the delayElements method on the stream to limit the amount of emitted items per second.

Flux.just("ID1", "ID2")
    .delayElements(Duration.ofMillis(250))
    .map(id -> apiClient.doSomething(id));

Update 12th April 2017

This post has been updated to use the new delayElements method instead of the now deprecated delayMillis.