Search code examples
firebasegoogle-cloud-platformgoogle-cloud-firestore

Does Firestore's FieldValue.serverTimestamp() Guarantee Unique Strong Consistency?


I have some questions regarding the behavior of Firestore's FieldValue.serverTimestamp():

  1. Indexing and Collisions: I want to rank documents by sorting them using FieldValue.serverTimestamp(). If I create the appropriate indexes, does Firestore's strong consistency model guarantee that these server timestamps will not collide?

  2. High Volume Requests Without Indexes: In a scenario where there are no indexes, even with strong consistency, if a high volume of FieldValue.serverTimestamp() requests are made simultaneously for different documents, is it possible for the server timestamps to exactly match and thus collide?

I read the Firestore documentation and searched online for details about FieldValue.serverTimestamp()'s precision and consistency guarantees.

I expected to find whether the nanoseconds always end with "0000" and if strong consistency prevents timestamp collisions. However, I couldn't find definitive answers.

Any insights or official documentation references would be greatly appreciated. Thank you!


Solution

  • TL;DR: You should not depend on server-side timestamp values being unique.

    1. ... If I create the appropriate indexes, does Firestore's strong consistency model guarantee that these server timestamps will not collide?

    No.

    1. ... is it possible for the server timestamps to exactly match and thus collide?

    Yes.

    It is quite possible for multiple documents to be created with the same timestamp, and this has nothing to do with the strength of the consistency model that Firestore uses.