---
title: "The stub, the server and reconciliation"
url: "https://www.mikefluff.com/en/experiments/js-ch15-latency-comp"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-18"
description: "Optimistic UI: latency compensation with a method stub"
book: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
alternates:
  - locale: "en"
    url: "https://www.mikefluff.com/en/experiments/js-ch15-latency-comp"
    markdown: "https://www.mikefluff.com/en/experiments/js-ch15-latency-comp.md"
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/experiments/js-ch15-latency-comp"
    markdown: "https://www.mikefluff.com/ru/experiments/js-ch15-latency-comp.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/experiments/js-ch15-latency-comp"
    markdown: "https://www.mikefluff.com/pt/experiments/js-ch15-latency-comp.md"
  - locale: "es"
    url: "https://www.mikefluff.com/es/experiments/js-ch15-latency-comp"
    markdown: "https://www.mikefluff.com/es/experiments/js-ch15-latency-comp.md"
---

# The stub, the server and reconciliation

*Optimistic UI: latency compensation with a method stub*

## What is an optimistic UI?

An optimistic UI applies a user’s change on the screen before the server confirms it, on the bet that the server will agree. Meteor called this latency compensation: a method runs twice, once as a stub in the browser and once for real on the server, and when the server’s answer arrives the client throws away what the stub wrote and keeps what the server sent. If the two did the same thing, the user notices nothing. If they differ, the user sees the change flicker or disappear. Retried calls also need care, because a resent call can run twice on the server.

Without latency compensation every click waits for a round trip: the browser sends the request, the server checks it, writes it and answers, and only then does the new order appear. Meteor ran the same method in the browser as a stub, showed its result at once and reconciled with the server afterwards. The experiment is a toy model inspired by Meteor, a few dozen lines, not Meteor itself.

Press “Add order”. The stub inserts the document on the client at once, the server runs the method 150 ms later, and the client swaps the stub’s document for the server’s. Slow motion stretches every delay ten times so the 150 ms can be seen. Switch modes: with ids of their own the document vanishes and comes back under another id; with a shared seed nothing changes; with a server rule the stub lacks, the order stays on screen for 150 ms and goes; after a dropped connection the resent call inserts a second document, unless the server remembers which calls it has already run.

In the book the model ran in Node 24.16.0 with the same 150 ms delay. Random ids gave the flicker the DDP specification describes. Passing the generator seed, as randomSeed in DDP, made the ids match and reconciliation changed nothing. The rejected call was visible for 150 ms. The retry inserted a second document, and the server that remembered call ids ran it once.

The chapter puts this next to the speculation inside V8: peek, bet, check, roll back. The difference is who sees the rollback. A deoptimisation only makes the program slower for a while, but a stub’s rollback happens in front of the user. So the programmer decides which actions may be guessed, a like or a to-do item, and which must be shown as waiting for confirmation, such as a payment, and makes methods idempotent because the runtime resends calls without knowing whether the server ran them.

_The page has an interactive experiment that runs in a browser: https://www.mikefluff.com/en/experiments/js-ch15-latency-comp_

## Sources

- [Meteor, DDP specification (DDP.md)](https://github.com/meteor/meteor/blob/devel/packages/ddp/DDP.md)
- [Kleppmann, Wiggins, van Hardenberg, McGranaghan, Local-first software (Ink & Switch, 2019)](https://www.inkandswitch.com/local-first/)

## From the book

Beside it in the book:

> There are two remedies. You can forbid the retry: Meteor.apply has a noRetry option, and then after a drop the client gets an error instead of a resend, and what to do next is up to the programmer. Or you can make the method idempotent, so that a repeated call changes nothing. For that every call needs its own identifier, and the server has to remember which calls it has already executed. In the fifth mode of the experiment the server does just that, and the second order never appears. In the twelfth chapter a client who was told «it failed» pressed the button again. A retry after reconnecting is the same story, except that the runtime presses the button on the user’s behalf.

- Read the chapter: [Chapter 15. Distributed State](https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch15) (opening free, rest in the full book; Markdown: https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch15.md)

- Book: [JavaScript: From Closures to Autonomous Systems](https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems)
