I have to call a critical API over a network (which makes it unreliable) many number of times in a go, many times a day. If the API fails to respond (timeout) or returns an error (because that server is in turn calling other unreliable third parties) i have to notify them about the error and then call the original API again.

Here, the error notifying may fail too and i will have to retry notifying error until i succeed. After which i can call the original API again.

I wanted to frame this using CompletableFuture. Is it a right choice of library for this kind of code? Or should i put a try catch loop in an infinite loop packed in a Runnable and execute it? Will CompletableFuture be an overkill? Is there any other alternative?

  • Your requirements are rather unusual, so I don't expect there to be an out-of-the-box solution that will fit well. If it were me I'd go with a loop. You might want to consider a timeout or retry limit rather than making it an infinite loop.
    – John Wu
    Oct 15 '19 at 4:20

Any API that goes to a server is unreliable. For any such API, you would expect that either you get an error very shortly after a call (usually due to a programming error), a correct reply quite shortly after the call but occasionally with a bit delay, a reply with a status indicating a problem at similar speed, or n indication that no useful reply will be forthcoming, often with a significant delay. Nite that whether your server reports a timeout or whether it reports that one of its servers reported a timeout can usually be treated the same.

It would be unusual that you’d have to inform the server about an error. Think hard whether it is really required.

Then talk to the server developers how the server will be responding if it crashed, if it is rebooting, or if it is under pressure and can’t keep up with requests. You don’t want to make a situation like that worse.

I’d try to figure out a good strategy when to repeat requests, when to back off, when to restart possibly due to a user request. What technology you use is secondary.

  • This API is a third party from which i need to interchange data. This data they fetch from other third parties. These requirements are dictated by their service and there is no scope for negotiation. I want to figure out a design where these requirements can be easily implemented.
    – ashish.g
    Oct 12 '19 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.