Ram Iyer
Ram Iyer

Reputation: 1679

Firestore ServerTimestamp Query

I am wondering if there is a possibility of the Firestore ServerTimestamp to be exactly the same for 2 or more documents in a given collection, considering that multiple clients will be writing to the collection. I am asking this because, Firestore does not provide an auto-incrementing sequential number to documents created and we have to rely on the ServerTimestamp to assume serial writes. My use-case requires that the documents are numbered or at least have a semblance to a "linear write" model. My app is mobile and web based

(There are other ways to have an incremental number, such as a Firebase Cloud Function using the FieldValue.Increment() method, which I am already doing, but this adds one more level of complexity and latency)

Is it safe to assume that every document created in a given collection will have a unique timestamp and there would be no collision? Does Firestore queue up the writes for a collection or are the writes executed in parallel?

Thanks in advance.

Upvotes: 0

Views: 101

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317948

Is it safe to assume that every document created in a given collection will have a unique timestamp and there would be no collision?

No, it's not safe to assume that. But it's also extremely unlikely that there will be a collision, depending on how the writes actually occur. If you need a guaranteed order, add another random piece of data to the document in another field, and use its sort order to break any ties in a deterministic fashion. You will have to decide for yourself if this is worthwhile for your use case.

Does Firestore queue up the writes for a collection or are the writes executed in parallel?

You should consider all writes to be in parallel. No guarantees are made about the order of writes, as that does not scale well at all.

Upvotes: 1

Related Questions