MA5: Offline-first sync: outboxes, retries and conflicts

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Deadlines, retries and backoff

🎯You'll be able to

Give every request a deadline, retry temporary failures with growing, randomised waits, and stop retrying changes the server refuses.

🏠The everyday picture

When a friend doesn't answer the phone, you try again in a few minutes, then later, then tomorrow. You don't ring every second, and if they've told you the number is wrong, you stop.

📘What it really is

📝A worked example

Input: KhetNote's backoffDelay(attempt, random).

Steps:

  1. The ceiling doubles from 1,000 ms: min(60000, 1000 × 2^(attempt − 1)).
  2. The wait is between half and all of the ceiling: ceiling × (0.5 + random × 0.5).
  3. With random at 1: 1,000, 2,000, 4,000 ms; at attempt 10, 60,000 ms (the cap). With random at 0: half of each.

Expected output: waits that grow, stop growing at a minute, and differ between phones.

🌍Where it's used

📝Case scenario

After a server outage, a ride app's phones all retried every second. When the server came back, the flood of retries took it down again, twice. Exponential backoff with jitter let it recover in minutes.

🖱️Try it

Work out the waits for attempts 1 to 7 with random at 0.5 (so each wait is 0.75 of the ceiling). Success: your numbers start at 750 ms and double, and the seventh is 45,000 ms, because its ceiling of 64,000 is capped to 60,000.

⚠️Common mistakes

🛠️For your project

Project 1's tests 6 and 7 check backoff, refused changes and the 10-second deadline.

✅Quick check

Why add a random part to the backoff wait?

Show the answer

So many phones coming back at once don't all retry at the same moment.

Which responses should not be retried?

Show the answer

Refusals such as 400 and 422, where the change itself is wrong.

← PreviousSending each change once
ProLearners Assist