Question about Firestore auto-generated document id

2,903 views
Skip to first unread message

Vincent Bergeron

unread,
May 19, 2019, 3:08:48 PM5/19/19
to Firebase Google Group
Hi!

Coming from Firebase RTDB, I'm currently moving part of my app to Firestore.

I was wondering why Firestore team choose a random document id generator rather than Firebase sequential key generator.

I also wonder if I can still generate my ID from push().key method and use them in Firestore.

Thanks

VB

Doug Stevenson

unread,
May 19, 2019, 5:18:47 PM5/19/19
to Firebase Google Group
Vincent,

I can't really comment on why the decision was made, but I can add some context and opinion.  The RTDB push IDs are not exactly "sequential".  They are based on the current time, as measured by the client device.  In general, this means that the IDs sort in chronological order.  However, this breaks down in two cases.  1) When the client's clock is wrong, and 2) the client was offline at the time the ID was generated, then synchronized later.  In both cases, you could observe that nodes could appear "out of order", significantly breaking the sequence that you might expect, since new records could very easily appear anywhere in the list.  (Also, malicious clients could easily fake this value.)

With Firestore, if you want time-based ordering based on the server's sense of time, I'd recommend using a timestamp type field, and have the client pass FieldValue.serverTimestamp() to it.  On top of that, I'd add a security rule that requires this to be the case, so that clients can't possibly send some other date.  Then, you can query the data using that field explicitly, rather than depending on data "hidden" in a push ID, since that's subject to inaccuracies on the client.

You can still use the RTDB push IDs if you want.  But personally, I would try to abandon the idea of sorting things based on the ordering of the hidden times in push IDs.

Doug

Tyler Rockwood

unread,
May 20, 2019, 5:19:49 PM5/20/19
to Firebase Google Group
Auto IDs are random because in Firestore having documents write to a narrow key spaces causes hotspotting and performance issues. https://cloud.google.com/firestore/docs/best-practices#hotspots

The RTDB has a different architecture so it doesn't have this narrow key range issue, but isn't as scalable as a database as Firestore. Most RTDB usecases max out at ~1k writes per second (wps) where Firestore's published write limit is 10k wps.

-Tyler 

Kato Richardson

unread,
May 21, 2019, 12:02:21 PM5/21/19
to Firebase Google Group
> However, this breaks down in two cases.
I think this probably deserves a bit more detail. There are more caveats to when these use cases apply.

> 1) When the client's clock is wrong
That's simplified a bit too much. The client will request the server time and adjust for clock skew. So this use case only applies if an ID is created during app initialization; before the clock skew has been determined.

2) the client was offline at the time the ID was generated, then synchronized later. 
Again, this would probably only apply if the client was offline during initialization, and the skew couldn't be determined. But it's probably still more accurate than Server.Timestamp in this use case (probably the one edge case where sequential ids are more useful than server time).


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/e1c51752-76ab-4167-b868-76a16ec75849%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Vincent Bergeron

unread,
May 24, 2019, 11:50:06 AM5/24/19
to Firebase Google Group
HI.

Because of this comment made by Tyler Rockwood:
Auto IDs are random because in Firestore having documents write to a narrow key spaces causes hotspotting and performance issues

If I want to transfer my FB data to Firestore, how should I handle my current document ID which are mainly FB generated key? If I just use the same key, I will cause hotspots and performances issues.

VB

Kato Richardson

unread,
May 28, 2019, 2:19:12 PM5/28/19
to Firebase Google Group
That shouldn't be an issue. You'll be throttled on how quickly you can import the data, but it won't affect your production scale once the ids are imported, as long as you use randomized Firestore IDs going forward.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages