I have recently taken over the maintenance of a Spring Boot project. This project has some RateLimit errors in the logs when the app was contacting a remote REST API. Turns out that this app was also using the synchronous, blocking RestTemplate client to make the API calls, instead of the newer Spring WebClient, that happens to be using the Reactor API under the hood.

And you know what is great about Reactor and using Reactive APIs in general? It makes programming with Data Stream so easy. Which also means it makes retry strategies implementation easier.

Let’s talk about the error in the log first. What I got looked like this:

As usual, the interesting bit in stack traces is at the top. The error is 429 Too Many Requests, and the message says there is a 1 minute rate limit. Decomposing this, the HTTP status code returned is 429. It’s a Rate Limit error, meaning the API tells the caller it has sent too many request. This can usually be solved by waiting a bit, and you might even have a Retry-after header in the response telling you how long you have to wait.

Let’s see how we can get that information with Spring’s WebClient:

This code is sending a GET request. The WebClient allows us to take a look at the request’s response and react appropriately thanks to the onStatus method. The first parameter is a boolean used to filter on returned HTTP Status code. Here, when the status code is 429, we do something.

We take a look at the Response headers, look if there is a Retry-After header, if so, we initialize the delayInSeconds variable with it, if not, we set a 60 default value. Then we can send back the Response body mapped to a RateLimitException. The body content will be used as an error message and the delayInSeconds will be available in a separate field. Take a look at the Exception code for more details:

So what needs to be done is catching this specific error and then retrying after the given duration. Reactor makes that easy by providing Retries strategies. All you need to do is call the retryWhen method:

There are different Retry methods, here we can use the withThrowable() builder. It gives a Flux that should contain the RateLimitException. So we start by applying a filter to make sure of that. Then we map that exception to the actual Retry object. Here it’s the Retry.fixedDelay strategy, taking a maximum attempts and duration as parameters. The duration comes from the RateLimitException that was thrown earlier.

With that, each time a request returns a 429, the client will wait the appropriate time until it retries. And it was much easier to implement with Reactor than using try/catch with Spring’s RestTemplate. I know Reactive programming might be a little intimidating at first, but it’s a great way to manage data streams like HTTP requests and responses, or to manage connections to databases that support reactive programming, like Couchbase.

Want some more help and ideas?

Author

Posted by Laurent Doguin, Developer Advocate, Couchbase

Laurent is a Paris based Developer Advocate where he focuses on helping Java developers and the French community. He writes code in Java and blog posts in Markdown. Prior to joining Couchbase he was Nuxeo’s community liaison where he devoted his time and expertise to helping the entire Nuxeo Community become more active and efficient.

Leave a reply