Skip to content

Commit 4b2180f

Browse files
author
Kaushik Gopal
committed
feat: add explanation for exponential backoff strategy with intended examples
1 parent 481e175 commit 4b2180f

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,30 @@ Cases demonstrated here:
100100
3. run a task constantly every 1s (same as above but there's no delay before the first task fires off)
101101
4. run a task constantly every 3s, but after running it 5 times, terminate automatically
102102

103+
### Exponential backoff
104+
105+
[Exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) is a strategy where based on feedback from a certain output we alter the rate of a process (usually reduce the number of retries or increase the wait time before retrying or re-executing a cetain process).
106+
107+
It makes far more sense with examples, and RxJava makes it (relatively) simple to achieve such a strategy.
108+
109+
#### retry (if error) with exponential backoff
110+
111+
Say you have a network failure. A sensible strategy would be to NOT keep retrying your network call every 1 second. It would be smart instead (nay... elegant!) to retry with increasing delays. So you try at second 1 to execute the network call, no dice? try after 10 seconds... negatory? try after 20 seconds, no cookie? try after 1 minute. If this thing is still failing, you got to give up on the network yo!
112+
113+
We simulate this behaviour using RxJava with the `retryWhen`
114+
115+
#### "repeat" with exponential backoff
116+
117+
Another variant of the exponential backoff strategy is to execute an operation repeatedly for a given number of times, but with delayed intervals. So you execute a certain operation 1 second from now, then you execute it again 10 seconds from now, then you execute the operation 20 seconds from now. After a grand total of 3 times you stop executing.
118+
119+
Simulating this behavior is actually way more simpler than the retry mechanism. You can use a variant of the `delay` operator to achieve this.
120+
103121
## Work in Progress:
104122

105123
Examples that I would like to have here, but haven't found the time yet to flush out.
106124

107125

126+
108127
### Pagination
109128

110129
a. Simple pagination

0 commit comments

Comments
 (0)