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

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

Sending each change once

🎯You'll be able to

Send outbox changes in order, use idempotency keys so a repeated send is stored once, and start syncing when the connection returns.

🏠The everyday picture

A shopkeeper gives a numbered receipt for each payment. If a customer comes back saying "did my payment go through?", the shopkeeper checks the number instead of taking the money again.

📘What it really is

📝A worked example

Input: KhetNote's test 5: the server stores a visit, then the answer is lost.

Steps:

  1. The first send fails with a network error, so the change stays in the outbox with one attempt recorded.
  2. The next sync sends it again with the same Idempotency-Key.
  3. The server finds the key, returns the stored answer, and doesn't write again; the phone removes the change.

Expected output: one copy of the visit on the server, and an empty outbox.

🌍Where it's used

📝Case scenario

A delivery app retried "mark delivered" after timeouts, without keys. Some deliveries were recorded three times, and drivers were paid for them. Adding a key per change, kept by the server for a few days, ended the duplicates.

🖱️Try it

In server/logic.js, find where the server remembers keys. Explain what it returns for a repeated key. Success: you can name the map and the line that returns the stored answer.

⚠️Common mistakes

🛠️For your project

Project 1's tests 4 and 5 check syncing on reconnect and one copy after a lost answer.

✅Quick check

Why can't the phone tell whether a timed-out change was stored?

Show the answer

The server may have stored it and the answer been lost on the way back.

What does the server do when it sees an idempotency key it already knows?

Show the answer

It returns the first answer and doesn't store the change again.

← PreviousSave first, sync later
ProLearners Assist