Search code examples
firebasehandlebars.jspuggoogle-cloud-functionstemplate-engine

Firebase Cloud Functions generated page flagged with reduce server response time


I have a Firebase website project, right now everything looks good except for the Firebase Cloud Functions generated pages flagged with reduce server response time on pagespeed insights. Insights would result 0.5 - 1.5 seconds.

Static pages get a score of 99 on mobile and 86 on desktop. With the cloud functions generated pages however the highest I get for mobile is 75 and lowest drops to as low as 30.

Here is some background on what is happening:

  • The page being requested is identified to ready the values needed for the Firestore queries.
  • 4 Firestore queries (3 collections with <100 documents and 1 with <5000 documents, which are very small).
  • Data from the queries are organized and prepped for render.
  • Page renders with res.render()

Original Build:

  • Cloud Functions
  • Express
  • Cors
  • Handlebars (as the template engine)

This build would usually get me a score of 60-75. Server response time is still high but it is consistent within the range 0.5 - 1.5

Alternative Build:

This build I use the same code for the queries except for the template engine rendering step.

  • Cloud Functions
  • Express
  • Cors
  • PUG (as the template engine)

This build gives both the best and the worst scores. I don't get a medium score on this of 60-75, The highest I got is a perfect 100 and the lowest is a very low 30. The bad thing about this is that it is very inconsistent and I can't determine the reason.

Is it a Cold Start problem?

I have read about cold start so I tried experimenting if it was the problem.

First Attempt I made a function that would ping the functions used by my Cloud Functions. I started it up and put it on a loop every 10seconds on my desktop. After a few minutes I checked Pagespeed if it had any effects, no improvements.

Second Attempt I made a function that would fetch the page I am testing on Pagespeed. I started it up and put it on a loop also on my desktop. I checked Pagespeed and it also did not have any effects.

So I guess I should cross out the Cold Start issue? Or am I handling the cold start issue wrong? Or maybe its a templating thing since using two different template engines have different results?

Anyway, this is what I have done so far, any idea on how to reduce the server response time of Firebase Cloud Functions generated pages?


Solution

  • Cold start might be your problem.

    If you keep one Cloud Function warm it will keep a container with that function warm - so that the next user can come and use the function in a warm state.

    However - if you have multiple users attempting to use the cloud function it will allocate one warm container to a connection, then another couple of cold containers to the other users. It's how they scale - building out more containers.

    I'm assuming whats happening is your tests are sometimes connecting to the warmed container - and when they are in use the tests are having to wait for a cold start on the other containers.

    Read up on Cold Starts

    A new function instance is started in two cases:

    When a new function instance is automatically created to scale up to the load, or occasionally to replace an existing instance.