Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to add timestamp as a document field in firestore database?

Tags:

While working with Cloud Firestore, I read this statement on the Add data to Cloud Firestore Docs:

Important: Unlike "push IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do not provide any automatic ordering. If you want to be able to order your documents by creation date, you should store a timestamp as a field in the documents.

So, I designed my database as it contains timestamp field in all the documents.

But after that, I read these statements on the Best Practices Docs:

Be aware that indexing fields with monotonically increasing values, such as timestamps, can lead to hotspots which impact latency for applications with high read and write rates.

Avoid high read or write rates to lexicographically close documents, or your application will experience contention errors. This issue is known as hotspotting, and your application can experience hotspotting if it does any of the following:

  • Creates new documents with a monotonically increasing field, like a timestamp, at a very high rate.

So I am confused now. I am developing a android app. Can i use timestamp as a field? Will it create any problem if there are lots of users for my app? And Yes, I must order the documents by creation date.

like image 268
Jayesh Babu Avatar asked Aug 02 '19 10:08

Jayesh Babu


People also ask

How do I get timestamp from firestore database?

serverTimestamp() you can use .

What counts as a document read in firestore?

When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed. (In contrast, when a document is deleted, you are not charged for a read.)

What is timestamp in firebase?

What is timestamp in firebase? A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.


1 Answers

There's a huge difference between those those 2 pieces of documentation.

The first one is referring to the fact that Cloud Firestore auto-generated IDs do not a have time component as Firebase realtime database push IDs have. That's the reason it cannot provide any automatic ordering. So it has nothing to do with the fact that you have a timestamp as a property of an object.

The second one is referring to the fact that is not recommended to use monotonically increasing values, such as timestamps as IDs of the document. So remember, that's the case only when using such timestamps as keys in the database, only in this case it can lead to hotspots which impact latency.

Since you are using a timestamp as field within each document, there is nothing to worry about. If you have used that timestamp as a document id in Coud Firestore or as a node key in Firebase realtime database, the above rules apply.

like image 54
Alex Mamo Avatar answered Sep 20 '22 22:09

Alex Mamo