Search code examples
firebasegoogle-cloud-firestoreconsistency

Is it possible to observe partial changes from an atomic Firestore write?


The Firestore docs says that both transactions and batched writes are atomic operations - either all changes are written or nothing is changed.

This question is about whether the changes of an atomic operation in Firestore can be partially observed, or whether the all or nothing guarantee applies to readers too?


Example:

Let's say that we have a Firestore database with at least two documents, X and Y.

Let's also say that there are at least two clients (A and B) connected to this database.

At some point client A executes a batched write that updates both document X and Y.

Later, client B reads document X and observes the change that client A made.

Now, if client B would read document Y too, is there a guarantee that the change made by A (in the same batched write operation) will be observed?

(Assuming that no other changes where made to those documents)


I've tested it and I've never detected any inconsistencies. However, just testing this matter can't be enough. It comes down to the level of consistency provided by Firestore, under all circumstances (high write frequency, large data sets, failover etc)

It might be the case that Firestore is allowed (for a limited amount of time) to expose the change of document X to client B but still not expose the change of document Y. Both changes will eventually be exposed.

Question is; will they be exposed as an atomic operation, or is this atomicity only provided for the write?


Solution

  • I've received an excellent response from Gil Gilbert in the Firebase Google Group.

    In short; Firestore do guarantee that reads are consistent too. No partial observations as I was worried about.

    However, Gil mentions two cases were a client could observe this kind of inconsistency anyway due to offline caching and session handling.

    Please refer to Gil's response (link above) for details.