S.Sreeram
S.Sreeram

Reputation: 225

Firestore: Reading data with references do increase in number of requests?

When documents on firestore is read, firestore wont give references data, if any. so currently I am requesting firestore for data from reference path. Do this increase in number of requests to server, eventually decrease in performance and increase in pricing ? How storing references is helpful in terms of requesting data from server ?

Upvotes: 2

Views: 1156

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Reading a document that has a reference counts as a read of that document. Reading the referenced document count as a read of another document. So in total that is two reads.

There is no hidden cost-inflation here: if the server were to automatically follow the reference, it would also have to read both documents.

If you're looking to minimize the number of documents you read, you can consider adding the minimum data you need from the referenced document into the document containing the reference. For example, if you have a chat app:

  • you might want to include the display name of each user posting the message in the message itself, so that you don't have to read the user's profile document.
  • if you do so, you'll have to consider what to do if the user updates their display name. See my answer here for some options: How to write denormalized data in Firebase
  • the number of users is likely smaller than the number of chat messages (and rather limited in a specific time-frame), making the number of reads of linked documents lower than the number of messages.
  • by duplicating the data, you may be inflating the bandwidth usage, especially if the number of users is much lower than the number of messages.

What this boils down to is: you're likely optimizing prematurely, but even if not: there's no one-size-fits-all approach. NoSQL data modeling depends on the use-cases of your app, and Firestore is no different.

Upvotes: 3

Related Questions