mergeUnsentWrites property

bool mergeUnsentWrites
final

Not in the official SDKs: a supported opt-in extension that differs from them in what the backend sees (see "What the app gives up" below). Whether a write to a document whose previous write is still pending and was not sent yet replaces that write with one merged write, instead of queuing a second one. Off by default; when off nothing changes.

This keeps the queue of pending writes short for an app that stays offline for a long time and edits the same documents again and again: about one pending write per document instead of one per edit, so a smaller cache, a shorter reconnect and fewer billed writes. The documents on the backend end up the same as when every write is sent, as long as the backend would have accepted each of them.

A write is merged only when all of these hold:

  • It is a single write (set, update or delete, or a WriteBatch with one write), and the newest pending write of its document is also a batch with one write. Batches with several writes are atomic and are never merged into, nor merged.

  • That pending write was never handed to the network layer in this session (in particular it is not in flight, and the up to 10 writes queued for the write stream are left alone). Writes queued while the network is enabled but the device is offline are handed out ahead of time, so the first few writes of an offline period stay separate.

  • No other client uses the cache: with a persistent cache, no other client refreshed its row in the cache within the last 30 minutes (unless the database is an ExclusiveDatabase); clients that share their state are never merged.

  • The two writes combine in one of these ways (other combinations stay queued as two writes):

    • The new write is a set without merge (also one with serverTimestamp, but not one with increment, arrayUnion or arrayRemove, which the local view computes from the value before the write) or a delete: it replaces the pending write, whatever that was. The result of such a write does not depend on the document before it.

    • The new write is an update or a set with merge, and the pending write is:

      • a set (with no transforms but serverTimestamp): one set of the pending value with the new fields applied;
      • an update or a set with merge: one write of the fields of both (the paths of both masks, with the values after both), with the precondition of the pending write; only when the pending write has no precondition (a set with merge) or both are updates (if the first one fails on a missing document, so does the second);
      • a delete, and the new write is a set with merge: one set of the new fields.

      Their transforms (serverTimestamp, increment, arrayUnion, arrayRemove) combine field by field:

      • a transform of the pending write on a field that the new write writes, deletes or sets to serverTimestamp is dropped (the later write wins); a serverTimestamp of the new write stays;
      • two arrayUnions of the same field become one arrayUnion of the elements of both, in order and without repeating an element; two arrayRemoves become one arrayRemove of the elements of both;
      • an increment of the new write on a field whose value before it is known (one the pending write set, or any field when the pending write is a set or a delete) is written as its result, when both numbers are integers (within +-2^53) or both are doubles;
      • after an update or a set with merge, a transform on a field that overlaps nothing the other write writes, deletes or transforms (no field equal to it, inside it or containing it) stays as it is, and so does a transform of the new write on a field it writes itself.

      Anything else stays queued as two writes, in particular an increment after an increment of the same field: the backend adds the two numbers one at a time, and a field that holds a double can round differently when the two are added first. Also an arrayUnion after an arrayRemove of the same field (or the reverse), an arrayUnion or arrayRemove of the new write after a set or a delete, a pending set with increment or array transforms, an update that deletes a nested field at or above a serverTimestamp of a pending set, and any other overlap of a transform with a field of the other write. minimum and maximum combine only as the first and last rules above say (overwritten, or on a field the other write leaves alone).

    The local view of the merged write is the same as with two writes (the documents, their pending-writes flag, query results, and the value under a pending serverTimestamp with ServerTimestampBehavior.previous), except for a serverTimestamp of the pending write that stays: until the backend confirms it, its estimate is the time of the newer write.

The merged write takes the place of the newer write in the queue: writes to other documents queued in between now reach the backend before the earlier state of this document, which never reaches it at all. The future of every merged write completes, or fails, with the merged write, and waitForPendingWrites waits for it.

Writes that are already queued when an instance starts with the setting on (queued in an earlier session, or with the setting off) are merged once, at start, by the same rules and on the same conditions: each write is merged into the pending write of its document as if it had just been made, in the order of the queue, so the queue becomes the one the setting would have built from the same writes. This runs in transactions of 500 writes queued as operations of the instance, so reads and writes of the app run between them; no write is sent to the backend until it is done. A write made meanwhile is merged when the merge of the queue reaches it. The progress is saved with each transaction: a merge that is stopped (the app is closed, the process is killed) resumes where it was at the next start, and a queue that was merged is not read again. It does not run while another client uses the cache, and the queue of a user who signs in later is merged when they sign in. After the process was killed, the row of the killed client in a SQLite cache counts as another client for up to 30 minutes (not with an ExclusiveDatabase, such as the web openers): until then neither merge runs, and writes queue as with the setting off.

What the app gives up:

  • Security rules and Cloud Functions triggers see one write instead of several, and never the intermediate states. Rules that check a transition (request.resource against resource) see the transition from the first state to the last.
  • A rejection rejects all the merged writes: the future of each of them fails with the same error.
  • Writes to other documents in between no longer see this document in its intermediate state, for example a rule that reads it with get(). Do not enable this when rules or functions depend on the order of writes across documents.
  • The precondition of a write that is replaced by a set or a delete is not checked: an update of a missing document that is followed by a set succeeds instead of failing.

Implementation

final bool mergeUnsentWrites;