← Table of Contents · Page 119 of 868
CONTROL STRUCTURES AND DECISION ROUTING
Part 3 of 9
LOOPS, RETRIES, AND GUARDED REPETITION
Sometimes a step fails for temporary reasons.
A website may be too busy, or a network connection may hiccup for a second.
Trying again often works.
A retry is simply running a failed step again.
But blind retrying is dangerous.
If the problem is permanent, such as a wrong password, then endless retries waste effort and may even lock an account.
So engineers use guarded repetition, which is repetition with rules that limit it.
A typical guard has three parts.
First, a maximum attempt count, such as three tries.
Second, a waiting period between tries that grows each time, called exponential backoff.
Exponential backoff means waiting, say, 1 second, then 2 seconds, then 4 seconds, doubling each time.
The growing pause gives a struggling service time to recover and avoids hammering it with rapid requests.
Often a small random amount is added to each wait, called jitter, so that many agents do not all retry at the identical moment.
Third, an escalation path for when the attempts run out, such as telling a human, using a backup method, or ending gracefully.
One more warning: only retry a step if repeating it is safe.
You will learn in Chapter 15 that a safe-to-repeat operation is called idempotent.
Retrying a read-only lookup is almost always fine.
Retrying a payment could charge twice unless the tool is designed to prevent that.
Advertisement
Read Aloud:
0:00 / 0:00