---
title: "Chapter 15. Distributed State"
url: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch15"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-18T12:24:57.879Z"
book: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
access: "lead-in"
purchase: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
alternates:
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/read/from-closures-to-autonomous-systems/ch15"
    markdown: "https://www.mikefluff.com/ru/read/from-closures-to-autonomous-systems/ch15.md"
  - locale: "en"
    url: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch15"
    markdown: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch15.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/read/from-closures-to-autonomous-systems/ch15"
    markdown: "https://www.mikefluff.com/pt/read/from-closures-to-autonomous-systems/ch15.md"
---

# Chapter 15. Distributed State

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

In the fourteenth chapter data went from the server to the client: something changed in the database, and a moment later it was visible on screen. But the user changes data too, and then it has to travel the opposite way. The user clicks «add order», the browser sends a request to the server, the server checks permissions, writes the order to the database, answers, and only then does the order appear in the list. If the server is on another continent, this takes a couple of hundred milliseconds, and on a mobile network a whole second. Every click comes with a spinning wait icon, and the application feels slow even though the server is fast.

Meteor decided that there was no need to wait. Among the seven principles it announced in early versions of its documentation was latency compensation: the client simulates what the server will do, and a method call looks as if the server had answered instantly. It works like this. Methods, the functions that change data, are written in shared code that is loaded both on the server and in the browser:

```
Meteor.methods({
  addOrder(title) {
    check(title, String);
    return Orders.insert({ title, owner: this.userId });
  }
});

Meteor.call('addOrder', 'order');
```

When the client calls a method, Meteor does two things at once. It sends the server a DDP message with the method’s name and arguments, and right away it runs the same method in the browser. The documentation calls this execution a stub or a simulation. The stub writes to Minimongo, Tracker sees the change, and the order appears on screen before the request has even reached the server. Then the server runs the method for real and sends two messages: the result of the call and a notice that all changes caused by this method have already been sent to the client. On receiving the notice, the client throws away everything the stub wrote and keeps what the server sent. If they did the same thing, the user will notice nothing.

* * *

It is worse when the stub and the server did different things. The simplest example is the identifier of a new document. The stub in the browser makes it up on its own, the server makes it up on its own too, and the identifiers come out different. The experiment in `experiments/13-latency` models this scheme in a few dozen lines: the stub inserts a document immediately, the server answers after a hundred and fifty milliseconds, and after the answer the client adopts the server’s state. During reconciliation the document with the stub’s identifier disappears and an identical document with the server’s identifier appears in its place. For the user this is a flicker, and for an interface that keys elements by identifier it also means recreating the element with all its internal state. The DDP specification calls it exactly that, flicker, and describes how Meteor got rid of it. The client sends along with the call a seed for the random number generator, and the server generates the identifier from the same seed with the same algorithm. The identifiers match, and reconciliation changes nothing, which the experiment also shows. In the twelfth chapter Temporal replaced `Math.random` so that a replay of the code would match the first run. Here the same trick is needed so that two runs of the same method on different machines match.

---

The rest of this chapter is free on the site once the reader confirms an email address: https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch15
