0

I wrote a Firebase cloud function to get data from external api and read/write to Firestore. I ran the function locally with 'firebase serve' several times. Then I noticed for each hour ~500 Firestore API Calls were made, even when the function is not invoked. And Firestore Writes and Deletes remains 0, only Reads increased.

The spike in reads stopped only when I shut down my computer so I believe the cloud function is the cause.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');

admin.initializeApp();

exports.someFunc = functions.https.onRequest((request, response) => {
  const url = 'https://someurl.com';

  const getData = async url => {
    try {
      const response = await axios.get(url);
      const data = response.data;
      if (data.status === 'ok') {
        data.articles.forEach(element => {
          if (element.author) {
            element.author = element.author.replace(/\//g, ',');

            var authorRef = admin
              .firestore()
              .collection('authors')
              .doc(element.author);

            authorRef
              .get()
              .then(doc => {
                if (!doc.exists) {
                  authorRef.set({
                    sources: [element.source.name]
                  });
                } else {
                  var sources = doc.data().sources;
                  if (!sources.includes(element.source.name)) {
                    sources.push(element.source.name);
                    authorRef.update({
                      sources: sources
                    });
                  }
                }
                return null;
              })
              .catch(error => {
                console.log('Error getting document:', error);
              });
          }

          if (element.source.name) {
            element.source.name = element.source.name.replace(/\//g, ',');

            var sourceRef = admin
              .firestore()
              .collection('sources')
              .doc(element.source.name);

            sourceRef
              .get()
              .then(doc => {
                if (!doc.exists) {
                  var sourceUrl = 'https://' + element.url.split('/')[2];
                  sourceRef.set({
                    sourceUrl: sourceUrl
                  });
                }
                return null;
              })
              .catch(error => {
                console.log('Error getting document:', error);
              });
          }

          admin
            .firestore()
            .collection('articles')
            .add(element);
        });
      }
    } catch (error) {
      console.log(error);
    }
  };

  getData(url);
  response.send('Success!');
});

'authors', 'sources' and 'articles' collection only have at most 20 documents each so there must be something repeatedly calling the Firestore that I could not found.

Lan Vu
  • 3
  • 4

1 Answers1

0

Although this looks like a pricing issue I think this could be an issue of Cloud Functions Retries moreover your usage numbers are not matching to the code because you have Set and Update Methods Which would make at least one write. You might want to check if your functions are executing successfully.

In case you want to understand Firestore pricing this might help.

Firebase Firestore reads are based on many parameters such as

  1. Number of Documents Requested
  2. Real-time updates to the collection if you are listening to changes
  3. Firebase Firestore Security Rules (Every time your rules have to be evaluated)
  4. Number of pages requested if pagination is used

You can find a detailed explanation here

and a few more examples here

Mahi Tej Gvp
  • 984
  • 1
  • 14
  • 34
  • My function executed correctly when I invoked it, the correct number of writes is shown, but the number of reads increase tremendously. – Lan Vu Apr 12 '19 at 07:55
  • You said the number of deletes and writes were 0 and reads was 500? what are your security rules? do they restrict any read writes for the collections in your function – Mahi Tej Gvp Apr 12 '19 at 09:28
  • I tried to allow writes only in security rules, but reads still increased, so I don't think security rules apply to cloud functions. https://stackoverflow.com/a/53727783/10855587 – Lan Vu Apr 12 '19 at 15:15