Transactions

How an edit becomes a transaction, how the server orders and applies it, and how it is acknowledged, retried or rejected.

Every change to a document is a transaction. This page follows one from the moment a client makes an edit to the moment every other client has it, and explains what happens when edits race, connections drop or the server says no.

Commands and transactions

A command is one small change to a document. There are nine kinds:

CommandChange
value.setReplace the value at a path
object.set, object.deleteSet or delete a field of an object
array.insert, array.move, array.deleteInsert, move or delete an array item, by its id
tree.insert, tree.move, tree.deleteInsert, move or delete a tree node, by its id

Commands address fields by key and items and nodes by id, never by index. That is what makes them meaningful when the document has changed in the meantime.

A transaction is a batch of commands that applies atomically, with a unique id. You do not write commands by hand: the client builds them from what you do to the document's proxy.

document.transaction((root) => {
  root.title.set("Plan");
  root.items.push({ text: "First step" });
});

Writes that change nothing produce no command. Setting a field to the value it already has, or moving an item to where it already is, creates no transaction.

The life of an edit

  1. Local apply. The client applies the transaction to its copy of the document at once and notifies listeners. This is the optimistic update: the user sees the edit without waiting for the server.
  2. Queue. The transaction joins the client's queue of pending transactions. The client submits them one at a time, in order.
  3. Commit. The server applies the transaction to its latest copy of the document, validates the result against the schema, and makes it durable.
  4. Broadcast. The server sends the committed change, with the changes that took effect, to every session on the document, including the one that submitted it, and then answers the submitter with the transaction's outcome.
  5. Convergence. Each client applies the committed change to its copy of the server's document and replays its own pending transactions on top.

The server is authoritative. Whatever a client shows optimistically, every client converges on the server's document.

When edits race

The server applies a transaction to the document as it is now, not as it was when the edit was made. This is called rebasing. Most commands still apply, because they address their target by key or id. A command is skipped, without failing the transaction, when a concurrent change removed or restructured its target:

  • the field, array item or tree node it addresses no longer exists;
  • the tree parent of an insert or move no longer exists;
  • a tree move would now create a cycle;
  • an insert's id already exists.

The rest of the transaction still applies. For example, if one user renames a card while another deletes it, the rename does nothing; a title change to another card in the same transaction is kept.

When two users write the same field, the last write the server commits wins.

The client replays its pending transactions with the same rules, so what the user sees locally matches what the server will commit.

Retries are safe

A transaction's id is an idempotency key: the server evaluates an id at most once per document, and records its outcome.

A transaction's id and its request (its commands, and the revision and schema version they were made on) are fixed when the edit is made. When a client does not learn a transaction's outcome (a lost connection, a timeout, a server error), it sends the same request again with the same id. If the first attempt was evaluated, the server answers with the recorded outcome and applies nothing. An edit is never lost and never applied twice.

The server answers ids from its record for its retry horizon: at least 30 days. A transaction id is a version-7 UUID, which holds the time it was made; an edit older than the horizon is not sent.

Rejections

A transaction that will never apply is rejected. The client removes its optimistic effect and reports it once, with a reason and the server's code. The client groups the codes as follows:

CodeMeaning
invalidThe commands are malformed, or the result would violate the schema (validation_failed)
conflictThe document was migrated, deleted or recreated since the edit was made, or an exact edit lost a race
not_foundThe document does not exist
forbiddenThe session may not write: its credential is read-only, or the database is read-only

unavailable, resource_exhausted and internal are not rejections. They mean the server recorded no outcome, so the client keeps the transaction and sends it again with the same id.

A skipped command is not a rejection either: the transaction was accepted, and the commands that still applied took effect. A transaction whose every command was skipped, or changed nothing, is answered unchanged.

Edits and schema migrations

Every edit names the schema version of the document state it was made on. The server rejects an edit made on another version than the document's (schema_mismatch, reported with the reason migration_version), so an edit made for an old document shape is never applied to a migrated document. See Schema versions.

Next