How does firestore generate auto doc ID offline?

Viewed 50

getFirestore().collection("MyCollection").doc() creates a document with auto generated ID instantly without await. The function description is as follows:

Get a DocumentReference for a randomly-named document within this collection. An automatically-generated unique ID will be used as the document ID.

It specifically said unique. How does it guarantee the ID is unique offline? The combination is 1.39e42, chances are low but not low enough to say impossible. If the collection has 1 trillion documents or millions of documents per day for hundreds of years, the chance of getting a nonunique ID is pretty high. For enterprise-grade projects, I don't think the risk is acceptable.

It's not just one user. For millions of users out there, the chance of someone's project hit this problem is not low at all.

1 Answers

Await is used for asynchronous operations. The generation of the ID is not an asynchronous operation, as it takes place on the client. So there is nothing you should wait for.

The collisions of IDs are incredibly unlikely and you can/should assume they'll be completely unique. That's what they were designed for. So you don't have to be concerned about it.

This built-in generator for unique IDs that Firestore uses when you call add() or doc() without passing any parameters, generates random and highly unpredictable document IDs, which prevents hitting certain hotspots in the backend infrastructure.

Related