0009 The server rebases a push that is behind
Status: Accepted
Context
A push names the version its steps apply to. When someone else saved first, the server answered 409 and the client pulled the steps it missed, rebased its own over them and pushed again. With short save delays (an edit visible to others within ~150 ms), two people typing in one chapter kept losing that race: in one measurement a client had 96 pushes refused and 116 pulls in 17 s, about 33 requests a second for two people, and its saves kept being delayed.
The client's checksum (ADR 0007) proves its base: the result of its steps on the server's copy at its version must equal its document. Rebasing on the server must keep that guarantee.
Decision
A client that can take it sends rebase: true. When its version is behind but the server still has the document at that version (DocState.recent, the newest rebaseWindow accepted changes, default 64; ProseMirror nodes are shared, so each costs little), the server:
- validates the steps on the document at the push's own version, exactly as before (parse, apply, the client's checksum, mass-delete guard, role), so a wrong base is still
divergedand nothing is applied; - maps them over the steps since with prosemirror-collab's own rebase (undo them, apply the missed steps, map each through the undos, the missed steps and the ones already mapped, mirrored),
rebaseOver; - validates the mapped steps on the current document (schema, guard, role) and commits them like any push;
- answers 200 with
rebased: { steps, clientIDs }(the steps it missed) andchecksum(the server's document after).
The client receives the missed steps (its unconfirmed steps rebase over them by the same algorithm, so they become exactly what the server stored), then confirms its own, and compares the checksum; a mismatch resyncs.
It answers 409 as before when the client didn't ask, the version is no longer kept (a restart empties the window), a missed step is the pusher's own (a push that landed but whose answer was lost: the pull confirms it instead of applying it twice), or a step no longer maps or applies.
Consequences
- Concurrent typing costs one request per save: in the two-client test, pushes refused as behind went from about 40 to 0.
- Old clients and old servers keep working: without
rebase(or on a server that ignores it) it is 409, pull and push again. - The server and the client must rebase identically; the server reimplements prosemirror-collab's algorithm (
rebaseOver), and the returned checksum catches any difference. - The server keeps a few recent documents per open document in memory.
Addendum: room pushes (WebSocket)
Room pushes take the same path. CollabClient sends rebase: true on its push messages (option rebase: false turns it off; servers that don't know the field refuse as before). The room passes it to submit, so the same validation, fallbacks and order apply: beforePush on the steps as sent, then the role policy and pushPolicy on the rebased path. After a rebased accept the room broadcasts the accepted steps before the ack, which carries checksum (the server's document); the client compares its confirmed document and stops as diverged on a mismatch.
The client receives a batch of server steps one author's run at a time: prosemirror-collab confirms our steps only when they lead what it receives, and a reconnect's backlog can hold the steps we missed followed by ours as the server mapped them (our push was rebased while its ack was lost). Received in one go, ours would apply a second time.
Several instances without an owner: when storage refuses a write as a version conflict, the copy catches up and a push that takes rebasing is retried once, mapped over what the other instance wrote, instead of being answered as behind. In the simulated two-client test, behind answers went from 11 to 0 (23 to 0 with three clients); on four instances, from 6 to 0.