The problem
The first autosave was a debounce and a request. It worked in a demo. A review of how it behaved under failure found five ways it could lose an edit without telling anyone:
- A failed save cleared the status and moved on.
- Edits made during a manual save, or within five seconds after it, were dropped.
- Leaving the page inside the two-second debounce discarded the pending save.
- A new save cancelled the previous request only in the browser. The server could still process it.
- Only cell edits triggered a save. Changing the title, the days or the column order did not.
The server had its own version of the problem. Each save sent the whole itinerary, and whichever request took the row lock last won. A second tab, a collaborator, or a slow earlier autosave could silently replace newer work.
Constraints
- An itinerary is stored and saved as one document. Moving to fine-grained operations would have been a rewrite of the builder.
- Tabs opened before the release had to keep working, without being able to overwrite saves made after it.
- A viewer or a stranger must not learn anything from a version check, so permissions come first.
- The desktop and phone builders had to behave the same way.
How it works now
In the browser, autosave keeps a snapshot of everything a save would send and compares it with the last copy the server confirmed. “Unsaved” means those two differ, so any kind of change counts. Only one request is in flight at a time. Network errors, timeouts, rate limits and server errors are retried with a growing delay. Validation and permission errors are not.
On the server, each itinerary carries a version number. A save names the version it was based on. The server checks permissions, compares versions while holding the row lock, and either writes and increments, or returns HTTP 409 and changes nothing.
- Both tabs opened version 7. You change a day, and the edit is marked unsaved.
- Two seconds after you stop, your tab sends the trip with “expected version 7”. It matches, so the server stores version 8.
- Your collaborator, still looking at version 7, makes a different edit.
- Their save also expects version 7. The server compares that with version 8 while holding the row lock, answers 409 and writes nothing.
- Their tab stops saving and says why. Their edits stay on screen, and they can copy or download them before loading version 8.
The lines that decide
The whole guarantee rests on where the comparison happens:
@transaction.atomic
def update(self, request, *args, **kwargs):
# get_object() holds the row lock, so the
# check and the increment below cannot
# interleave with another save.
itinerary = self.get_object()
expected_version = int(
request.data.get('expected_version'))
if expected_version != itinerary.version:
return Response({
'code': 'version_conflict',
'version': itinerary.version,
}, status=status.HTTP_409_CONFLICT)
return super().update(
request, *args, **kwargs)
Reading the version first and locking later would leave a gap in which another save could slip through. Here the row is locked before the version is read and stays locked until the write commits, so there is no gap. The reply to a stale save carries the newer version number and no content.
What a conflict looks like
The person who loses the race keeps everything they typed. Saving pauses, the header and a banner say why, and they can copy or download a full draft (title, dates, days, rich text, transport and hotels) before loading the newer version. Manual Save is refused for the same reason, with the same explanation.

Decisions and tradeoffs
- Refuse instead of merge
- Merging two versions of a day’s plan can produce something neither person wrote. For small groups planning a trip, being told is better than being guessed at. The cost is that someone occasionally has to re-enter an edit by hand.
- One version per trip
- Two people editing different days still conflict. A version per day would reduce that, at the price of a harder consistency story when days are reordered or removed.
- A grace period for old tabs
- Pages loaded before the release send no version. They may keep saving until any newer page saves that trip. From then on an unversioned save is refused, because it could overwrite newer work.
- An exit prompt
- Closing a tab with unsaved or unconfirmed work asks first, including while a cell is still being typed in. Leaving the builder inside the app saves immediately instead.
How it was tested
The race condition is tested as a race: two threads send saves from the same version at the same moment against PostgreSQL, five times over, and each round must produce exactly one 200 and one 409.
barrier = threading.Barrier(2)
def run(slot):
client = APIClient()
client.force_authenticate(owner)
barrier.wait()
results[slot] = client.put(url, {
'content': content(f'tab {slot}'),
'expected_version': trip.version,
}, format='json').status_code
# start both threads, wait for both to finish
self.assertEqual(sorted(results), [200, 409])
The barrier holds both threads until each is ready, then releases them together. Without it one thread would usually finish before the other began, and the test would pass without testing anything.
| Suite | Covers | Result |
|---|---|---|
| Versioning, backend | Stale saves, concurrent saves, old tabs, permission order, failed validation | 13 of 13 passed |
| Save conflicts, browser | Two tabs, a collaborator on a phone, manual save, draft export | 3 of 3 passed |
| Autosave reliability, browser | Retry, offline recovery, request ordering, tab close, phone layout | 10 of 10 passed |
Outcome and limits
Each of the five loss paths has a fix, and the failures that matter most have tests that fail if the behavior regresses. The version check is live. I don’t yet measure how often conflicts happen in practice.
There is no live cursor or automatic merge, and edits made while offline last only as long as the tab stays open. If conflicts turn out to be common, versioning each day is the next step.