-2

I am writing a HTTP client and am trying to decide what would be the correct time to wait before retrying the HTTP request in case the downstream service is for some reason not responding. I intend use a library which has a great feature for scheduling requests. It has a function called Schedule.exponential e.g.

val exponential = Schedule.exponential(10.milliseconds)

The definition is:

  /**
   * A schedule that always recurs, but will wait a certain amount between
   * repetitions, given by `base * factor.pow(n)`, where `n` is the number of
   * repetitions so far. Returns the current duration between recurrences.
   */
  def exponential(base: Duration, factor: Double = 2.0): Schedule[Clock, Any, Duration] =
    delayed(forever.map(i => base * math.pow(factor, i.doubleValue)))

I am not sure what is the best practice on setting the base and factor value. What is reasonable or preferred practice here?

1 Answer 1

2

There is no particular good set of values that always works well.

The base duration must be some positive number. Intuitively, it indicates the duration after which the first retry is performed. This should be short enough to feel “fast” in the context of your software, but long enough that the reason that caused the original request to fail might have disappeared.

The exponent must be any number larger than 1. Small values feel faster to the user, but increase the total volume of requests during failures. Values larger or equal to 2 limit this volume and prevent unconstrained growth[1].

[1]: because ∑n = 0 … ∞ 1/2n = 2 is a convergent series.

If we look beyond exponential backoff, any convergent series can be used as the basis for determining backoff duration with a fixed maximal volume of requests. For example, using the Fibonacci sequence as factors for increasing the backoff has similar properties, though it will result in a higher volume of requests.

2
  • I'm not saying you're wrong but I don't follow the reasoning in your footnote yet. Surely the reciprocals of fib converge to something a bit less than four? It's a higher overload due to failed requests but it's still convergent and so bounded surely? Commented Aug 18, 2022 at 15:19
  • @Dannie You are right, any convergent series can be used. I'll edit the post accordingly. Commented Aug 18, 2022 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.