TccHtnn
TccHtnn

Reputation: 936

Async task in firebase cloud function

I want to implement a function a by firebase cloud function.

In this function, when it received a request from client, I save the id and the score of client to a dictionary. In normally, I will save this data to firestore and response to client, but I want to save the writing operation to firestore, so I will update the data of client in dictionary and response to client. Checking every 15 minitues, I will save all data in dictionary to firestore and reset it.

This is the main flow I wrote:

var dic = {}; //save the temp data of client
var timeNow = new Date().getTime();  //get Time when function was deployed
exports.testSucCollectionQueryFunction = functions.https.onRequest( (request, response) => {
      getDataInfoFromRequest();  //id, and score
      dic[id] = score;  //update data info
      response.send(result); // only response that cloud had received request
      if (currentTime - timeNow > 15 minutes) {
           saveDatainDicToFireStore(); 
           dic = {};   //reset Dictionary
      }
}

I tested with the small concurrent connection from client (<5), it was still ok. But I dont know what happen with the 1000requests/second. So, if Can you help me any ideas to understand about this problem? Thanks for your help

Upvotes: 0

Views: 633

Answers (1)

JamWils
JamWils

Reputation: 785

So there are a few issues here.

First, response.send(result); should be the last line of code in the function. Once you call response you are sending a signal to the cloud function that you're function is complete. While the code after response might run, it isn't guaranteed. I'm referring to your if statement:

if (currentTime - timeNow > 15 minutes) {
    saveDatainDicToFireStore(); 
    dic = {};   //reset Dictionary
}

Next, cloud functions whether they are Firebase, AWS Lambda, or Azure Functions should be stateless. Depending on your workload their might be multiple containers created by the Firebase Function system and you do not have control over which instance you are caching data into. Plus, within that 15 minute period those instances might be shutdown due to inactivity and your dictionary will be lost completely.

If I were you I would redesign your architecture. I would either write to a temp location in the Firestore database that will act as a "caching" mechanism. Then setup a separate HTTP function that you will call every 15 minutes to aggregate that cached data and save it to your intended location.

Or simply write the data to that specific location right way instead of caching it at all. Finally, one other alternative is writing it to a file and saving that file in the Firebase Cloud Storage bucket for later processing, and then use that separate HTTP function from earlier to aggregate the data and write it to the Firestore.

Upvotes: 1

Related Questions