Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Cold start latency of 5+ seconds even after using the preferRest option for firebase-admin #1851

Closed
HarshitPersona opened this issue May 26, 2023 · 5 comments
Assignees
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. needs more info This issue needs more information from the customer to proceed. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@HarshitPersona
Copy link

Thanks for stopping by to let us know something could be better!

Linked issues- https://issuetracker.google.com/issues/158014637 and firebase/firebase-admin-node#1901

Environment details

  • OS: macOS Ventura
  • Node.js version: 18
  • npm version: 9.5.1
  • @google-cloud/firestore version: 6.5.0

Thanks!

@HarshitPersona HarshitPersona added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels May 26, 2023
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label May 26, 2023
@ehsannas
Copy link
Contributor

Hey @HarshitPersona , can you provide more information about your environment? Are you sending the requests from your local machine (macOS)?

@ehsannas ehsannas self-assigned this May 26, 2023
@HarshitPersona
Copy link
Author

HarshitPersona commented May 28, 2023

I have written a cloud function in GCP in Nodejs18 environment which is connecting to the firestore database through firebase-admin library. See below for the code snippet-

let isAlreadyInitialized = false;
let db;

const middleware = async (req, res) => {
  try {
    const apiKey = req.headers["x-api-key"];
    if (!apiKey)
      return res.status(401).send({ message: "A valid API Key is required!" });

    const { initializeApp } = require("firebase-admin/app");
    const {
      initializeFirestore,
      getFirestore,
    } = require("firebase-admin/firestore");

    if (!isAlreadyInitialized) {
      const app = initializeApp();
      initializeFirestore(app, { preferRest: true });
      db = getFirestore(app);
      isAlreadyInitialized = true;
    }

    const query = db.collection("apps").where("apiKey", "==", apiKey);

    const querySnapshot = await query.get();
      return res.send({ tests: "This is a test response!" });
  } catch (e) {
    return res.status(500).send(e);
  }
};

exports.fetchCreative = middleware;

If you notice here - return res.send({ tests: "This is a test response!" }); I have send back a test response but this response is taking 5+ seconds on cold starts even though I used preferRest option as you can see in the above code.

@HarshitPersona
Copy link
Author

"firebase-admin": "^11.8.0" - this is my dependency

@ehsannas
Copy link
Contributor

Thanks for reporting @HarshitPersona .

Would you be able to answer these questions that'll help us understand what's going on?

1- How are you measuring the latency/delay? It's conceivable that a part (or majority) of what you have measured is in network latency for the response, not the time it took to update the database.

2- What would your measurement be if you set preferRest to false?

3- What would your measurement be if you don't interact with Firestore? Meaning, your cloud function just does initializeApp(); and then return res.send({ tests: "This is a test response!" });?

@ehsannas ehsannas added the needs more info This issue needs more information from the customer to proceed. label Jun 7, 2023
@ehsannas
Copy link
Contributor

Closing since there's been no response in 3 weeks. Feel free to comment if the above information comes to light.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. needs more info This issue needs more information from the customer to proceed. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants