-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change jitter implementation #10
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ThomWright
force-pushed
the
fix-jitter-impl
branch
from
July 4, 2023 09:47
594d19e
to
dfe5ce7
Compare
Decorrelated jitter has a major flaw: clamping. Retry intervals can get repeatedly clamped to the maximum allowed duration. This effectively removes any jitter. The implementation in this library is a variation which exacerbates this flaw. This change replaces this jitter implementation with three options: 1. None 2. Full 3. Bounded It also changes how 'maxiumum retry duration' is implemented. Before, there was no information about how long the task had been retrying, so there was no way to implement a correct 'maximum retry duration'. This has been reimplemented to require a start time for a task when setting a maxiumum retry duration.
ThomWright
force-pushed
the
fix-jitter-impl
branch
from
July 4, 2023 09:55
dfe5ce7
to
724e930
Compare
eopb
approved these changes
Jul 21, 2023
tl-rodrigo-gryzinski
approved these changes
Jul 21, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two main problems:
This change replaces the decorrelated jitter variation with three options:
It also changes how 'maximum retry duration' is implemented. Before, there was no information about how long the task had been retrying, so there was no way to implement a correct 'maximum retry duration'.
This has been reimplemented to require the task's start time when setting a maximum retry duration.
Details
Jitter algorithms
Decorrelated jitter has a major flaw: clamping. Retry intervals can get repeatedly clamped to the maximum allowed duration. This effectively removes any jitter.
The implementation in this library is a variation which exacerbates this flaw.
For comparison, a standard exponential backoff algorithm:
With “full jitter”:
Whereas “decorrelated jitter” is this:
Let’s break this down into two parts:
The
sleep
duration will generally increase every iteration. With this algorithm, when the previoussleep
grows as large asmax_duration
there is only a 1/3 chance of applying jitter. E.g.:This isn't great. In this case, 2/3 of the time the sleep will be the
max_duration
.So, decorrelated jitter is this:
But instead, what our algorithm does is more like this:
Again, broken down:
Here, there is no real bound on how high
min_duration * exp ** attempt
can go. E.g.:In this case we have a very small chance of any meaningful jitter: 0.0003% chance by my calculation.
Total retry duration
The current implementation makes a guess at how many attempts it will take to exceed a given total duration. It can't be accurate because of the random jitter, so this is a best guess based on mean jitter.
Propagating state
Again, decorrelated jitter is:
This relies on the previous
sleep
value. But theshould_retry(&self, n_past_retries: u32)
function doesn't have access to the previous sleep value, which is (presumably) why a variation on the algorithm was used.Most similar retry libraries take
&mut self
rather than&self
to keep track of this state internally.For this library it might not help much though, because retry state is tracked outside of the policy object. E.g. for retrying AMQP messages, we send the number of attempts in a header. The service receiving the retried message looks at the header to retrieve the state and makes the next retry decision based on that.
Before this change, "number of attempts" was the only state this library supported.
Start times
In order to accurately stop retrying after a given total duration, we need to know how long we've been retrying for. The simplest way to do this is to propagate the task start duration.
The API has been changed to accommodate this, in a way which keeps the
RetryPolicy
trait as-is.To do