The user types hello.
Five PUT requests leave but the one carrying hel arrives last.
The server stores hel. The UI shows Saved โ.
No error, no log: a lost update, an anomaly the database literature formalized back in 1995 [1]. Your frontend can cause one without any help from the database.
Autosave is only the first of three write bugs this article covers, each with its fix, using @ngsignal/mutation (introduced here). Each one has a live page in the demo.
1. Autosave: send only the latest version
Parallel PUTs can be processed in any order. A FIFO queue fixes the order but sends every outdated version. Autosave needs something in between: finish the request in flight, then send only the latest version.
readonly save = httpMutation(
(doc: Doc) => ({ method: 'PUT', url: `/api/docs/${doc.id}`, body: doc }),
{ concurrency: { mode: 'queue-latest', debounce: 500 } },
);
At most two calls exist at once: one in flight and one waiting. Each keystroke replaces the waiting call, and a replaced call is never sent.
Two rules follow:
- Send the whole document. Each PUT carries the full state, which is what makes it idempotent [2], so skipping versions loses nothing. A skipped delta would be lost.
- Don't roll back a replaced call. The call that replaced it carries a newer state.
Why put the debounce in the mutation instead of a debounceTime() before calling it? Because a debounce outside the mutation hides the edit: for 500 ms the mutation knows nothing about it, so the UI still says Saved. Inside the mutation, the call is pending from the first keystroke.
๐ /autosave
2. Check, then delete: ordering several mutations
The user checks todo #42, then deletes it right away. The PATCH leaves first, the DELETE just after. They come from two separate mutations, so nothing orders them:
PATCH /todos/42 โโโโโโโโโโโโโโโโโ 404 โ
DELETE /todos/42 โโโโ 204 โ
Sending a request first doesn't mean it arrives first. Lamport made the point in 1978 [3]: the only order that exists is the one you create. Here, the order comes from a queue the two mutations share:
readonly todoWrites = mutationQueue();
readonly toggle = httpMutation(
(todo: Todo) => ({ method: 'PATCH', url: `/api/todos/${todo.id}`, body: todo }),
{ concurrency: this.todoWrites },
);
readonly remove = httpMutation(
(id: string) => ({ method: 'DELETE', url: `/api/todos/${id}` }),
{ concurrency: this.todoWrites },
);
If several components write todos, move the queue to a root service so they all share it. Don't put it at module level: during SSR, every request would share it.
Group by entity? Add a key
One queue for the whole list is correct, but it's slow: checking #7 waits behind #42's slow PATCH. That's head-of-line blocking, the problem HTTP/2 and then QUIC were built to remove [4].
A key fixes it the way Kafka partitions do [5]: writes stay in order per todo, and different todos run in parallel. Each mutation tells the queue which todo its input targets:
// toggle
{ concurrency: { queue: this.todoWrites, key: (todo) => todo.id } }
// remove
{ concurrency: { queue: this.todoWrites, key: (id) => id } }
- No key waits for every key. Exactly what "clear completed" needs.
- A key orders calls but drops none. Every toggle of #42 is sent. For only the latest state of each todo, use one queue-latest mutation per todo.
Things to watch with a shared queue:
- A request that never settles blocks every mutation on the queue. Pass the AbortSignal through and set a timeout.
- A mutation that awaits another mutation on its own queue waits forever. Chain it from onSuccess instead.
- The queue only orders the writes of one tab. Across tabs, the server has to arbitrate with ETag and If-Match [2], the optimistic concurrency control Kleppmann describes [6].
๐ /rename-then-delete ยท /save-per-document
3. Leaving before the save
The user types, then clicks a link. The route is destroyed, the debounced save is aborted, and the edit is lost.
A guard can't know which mutations a page created. A scope (new in 0.12.0) collects them:
{
path: 'docs/:id',
component: DocEditor,
providers: [provideMutationScope({ beforeUnload: true })],
canDeactivate: [() => {
const scope = inject(MutationScope);
return !(scope.isPending() || scope.hasError()) || confirm('Unsaved edits. Leave anyway?');
}],
}
- Pending covers calls that are in flight, queued or waiting for their debounce. That's the payoff of section 1: a debounce outside the mutation would hide the edit from the guard.
- Error matters too: for the user, an autosave that got a 500 is an unsaved edit.
- beforeUnload covers closing or reloading the tab. The browser only asks after the user has interacted with the page [7], so the route guard is the reliable part.
๐ /autosave: type, then click another page.
Cheat sheet
| Situation | Concurrency |
|---|---|
| Independent writes | default |
| Payment, submit | drop |
| Every write counts, in order | queue |
| Whole-document autosave | queue-latest + debounce |
| Several mutations, same data | shared mutationQueue() |
| Same, independent per entity | queue + key |
| Leaving with writes pending | scope + guard |
No store, no cache: a Promise chain with signals on top.
Next time: optimistic updates without rollback.
โญ GitHub ยท ๐ฆ npm ยท โก Demo ยท ๐ Recipes
References
- H. Berenson, P. Bernstein, J. Gray et al., A Critique of ANSI SQL Isolation Levels, SIGMOD 1995: the lost update (P4).
- RFC 9110, HTTP Semantics: ยง9.2.2 idempotent methods, ยง13.1.1 If-Match.
- L. Lamport, Time, Clocks, and the Ordering of Events in a Distributed System, CACM 1978.
- RFC 9000, QUIC: independent streams, without head-of-line blocking.
- Apache Kafka documentation: ordering is guaranteed within a partition.
- M. Kleppmann, Designing Data-Intensive Applications, O'Reilly 2017, ch. 7, "Preventing Lost Updates".
- HTML Standard, beforeunload: the prompt requires sticky activation.


Top comments (0)