node.jsgoogle-cloud-functionscold-start

Cloud Function Cold Start delays sending push notifications


I have a strange problem with an HTTPS Cloud Function that based on specific rules send push notifications to client. In particular, when the Cloud Function starts for the first time, notifications are sent with a delay. On the contrary, after Cold Start, everything works as expected, the push notification is sent straight after.

My code looks like the following (very simplified):

exports.myCloudFunction = functions.https.onRequest(async (request, response) => {
  try {
    const numberOfImportedItems = await importData();
    if (numberOfImportedItems > 0) {
      await sendPushNotification();
    }
    response.status(200).send('OK');    
  } catch (error) {
    response.status(404).send('KO');
  }
});

where sendPushNotification is defined the following:

async function sendPushNotification() {
  // https://firebase.google.com/docs/functions/tips?hl=en#use_dependencies_wisely
  const module = await import('push-notification-module');

  // compose the notification here
  module.send(notification);
}

It's worth to note that import('push-notification-module') it's not a top level import but a local one since the in certain cases notifications are not sent (based on numberOfImportedItems).

Do you have any suggestion why this happens? Do you have any suggestion on how to mitigate this delay behavior?


Solution

  • Cloud Functions are stateless execution environments which are initialized from scratch, which leads to cold starts. Also as @Frank stated in the Stackoverflow Link,

    When your function hasn't been executed in some time, Cloud Functions puts it in a mode that uses fewer resources so that you don't pay for compute time that you're not using. Then when you hit the function again, it restores the environment from this mode. Once your functions are being frequently triggered in production, chances are they'll hardly ever hit a cold start again, especially if they have consistent traffic. If some functions tend to see spikes of traffic, however, you'll still see cold starts for every spike.

    Also you may review the Tips & Tricks For Cloud Functions documentation to avoid issues when using Cloud Functions..

    Several recommendations in this document center on what is known as a cold start. Functions are stateless, and the execution environment is often initialized from scratch, which is called a cold start.

    From the description, it seems like the intended behavior when a function is invoked in some time or a cold start issue. You may keep your function always warm or check the minimum instances configured for your function.

    By default, Cloud Functions scales the number of instances based on the number of incoming requests. You can change this default behavior by setting a minimum number of instances that Cloud Functions must keep ready to serve requests. Setting a minimum number of instances reduces cold starts of your application.

    You can refer to this Documentation on how to set a minimum instance limit for existing functions.