50

For the structure of my database I need to know if the automatically generated identifiers in Firebase Cloud Firestore are unique in the collection or in the whole database.

How to do that?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Arco
  • 723
  • 1
  • 5
  • 8

2 Answers2

76

The keys generated by calling add() in Firestore are not tied to the collection on which you call add(). Instead they are random identifiers that are statistically guaranteed to be unique. In the case of Firestore (and Firebase Realtime Database) these keys are generated client-side.

If you're interested, have a look at how the Firestore JavaScript SDK implements the logic:

  1. add calls doc()

  2. doc calls AutoId.newId()

  3. AutoId.newId() generates a client-side ID

In itself the logic is similar to how the Firebase Realtime Database generates its push IDs. The main difference seems to be that Firestore's auto-generated keys are not based on the local timestamp, so they cannot be meaningfully used to order the documents in the collection.

holmberd
  • 2,393
  • 26
  • 30
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Are there any better ways to use `.collection()` and `.doc()` to access data rather than having to create a custom id for each document/collection? What I mean is that if I let Firestore do auto id, I have no practical way of pulling the record(s) I need unless I use my own naming convention... – CodeFinity Jul 19 '18 at 23:43
  • Ok. I guess something like this might work. https://stackoverflow.com/a/46748716/1653236 – CodeFinity Jul 19 '18 at 23:48
  • 2
    After reading newId() code i had some doubts about the id uniqueness. It seems to create a string from 20 random picked chars. How can this be guaranteed unique? – Pablo Nov 23 '18 at 17:51
  • 1
    Ok, there are 63^20 posibles id. is very very unlikely but not imposible, is that correct? – Pablo Nov 23 '18 at 18:05
  • 3
    Correct, it is extremely unlikely, but not guaranteed. Same as UUIDs, and GUIDs (2^122 combinations). – Frank van Puffelen Nov 23 '18 at 20:53
  • 5
    @FrankvanPuffelen I found it pretty interesting that the SDK depends on `Math.random`, which is only pseudo-random. Does this imply that in practice, it's good enough to generate random IDs on the client side (in general) even though they aren't completely random. I'm thinking that if it works at Firebase scale, it must be good enough for my own app. I wonder if the Firebase team tested cryptographically secure random number generation too and decided that it was too slow or just not worth it. – jon_wu Apr 23 '19 at 00:43
  • @jon_wu The secure crypto prng are good because they looks perfectly chaotics and evenly distributed. Even if it's not perfectly the case with Math.random, I think it's ok because it's not used to generate encryption keys or something like that. – rm4 Oct 11 '20 at 17:52
  • @rm4 Agreed on the theory of it. I wasn't sure if in practice (browsers in the wild creating records for a huge system like Firebase), the theoretical increased likelihood of [generating a collision](https://stackoverflow.com/a/28220928/1058558) would be an issue. Perhaps it just doesn't matter or it's just handled so gracefully that when it does (rarely), nobody needs to care. Just speculating though. – jon_wu Oct 12 '20 at 19:28
  • @jon_wu Not to argue, just to complement informations: There is no prng implementation from javascript, it comes from the browser, and all modern browser basically uses xorshift128+ algorithm. This algorithm is relatively evenly distributed so it's enough ''random'' to be used in most cases. Problems usually come from the fact that you cannot thrust client's inputs. But in firebase you can set any string as an id, so it's not really a problem there as client is in right to chose the id. As for the collisions, they probably already calculated the size of the string needed to avoid collision. – rm4 Oct 13 '20 at 19:33
1

It seems that in node and in modern browsers the auto generated IDs are crypto quality random. https://github.com/firebase/firebase-js-sdk/blob/66deb252d9aebf318d2410d2dee47f19ad0968da/packages/firestore/src/platform/node/random_bytes.ts

Tom Andersen
  • 7,132
  • 3
  • 38
  • 55