0

I have a cloud function which fetches some JSON data. THat's all it does. I followed tips highlighted in this video : https://www.youtube.com/watch?v=IOXrwFqR6kY

So, I have cors and rp dependencies, and nothing outside of my function. The data is compressed (I think that's a default feature). Chrome dev tools shows the data is gzipped anyway. It's 37KB once zipped. Dev tools consistently indicates a TTFB of around 4.5sec. Content download is only around 7.8ms.

If I do a curl request to the same json data from my local machine, I get the following :

    time_namelookup:  0.028s
       time_connect:  0.225s
    time_appconnect:  0.921s
   time_pretransfer:  0.921s
      time_redirect:  0.000s
 time_starttransfer:  1.574s
                    ----------
         time_total:  1.576s

It seems there is a significant gap. If i'm not mistaken, TTFB should be compared to time_starttransfer of my curl request. What is the gap due to ? Does it all have to do with cold start ? I cannot seem to be able to get lower than a total of 4.6secs total with my cloud function. The server i send the requests to has a fairly consistent uptime response of around 500ms, according to the data they share.

What can I do to lower that number closer to 1.5s, or lower potentially ?

Thank you!

John Doe
  • 1,092
  • 3
  • 12
  • 25
  • I wonder if it's not related with cold start.... Is this performance the always the same or it's for the first time like this and then better? – vitooh Apr 22 '20 at 19:25
  • Yes i believe it's due to cold start. It seems 4.5s is the best i can do with my simple function. I found a solution though. Please see my answer – John Doe Apr 23 '20 at 19:31

2 Answers2

0

ok, so I found the way. For anyone else in the same case, here is an explanation :

I was introduced to Google Cloud via Firebase, therefore Firebase is all I knew about Google Cloud solutions until yesterday. There is little mention, or at least it's not obvious enough, of other Google cloud services from https://firebase.google.com.

Turns out, Google does not have 1, not 2, but 5 different cloud compute options. Firebase's Cloud Function is 1 of them. There are 4 others. See this good video summary here that goes through each one of the 5 options, and highlights well the differences https://www.youtube.com/watch?v=wzPmgWJ5fpU&feature=youtu.be

So, based on my understanding and my experience, Cloud functions is a serverless solution meant to be used with other Firebase services, such as Firestore, Storage and Hosting. With these services, functions responses are blazing fast. In order to interact with 3rd party APIs, you need to use one of the other 4 solutions, because response times from requests to 3rd party APIs are far beyond acceptable. As a guideline, Google recommends a TTFB of up to 200 ms (https://developers.google.com/web/tools/chrome-devtools/network/understanding-resource-timing?hl=ko). And lighthouse has a coded TTFB threshold of 600ms (https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/audits/time-to-first-byte.js). Anyway, it's a guideline.

The closest to Firebase Cloud functions in terms of ease of use is AppEngine. https://cloud.google.com/appengine. The other solutions allow greater flexibility, but also require infrastructure expertise.

AppEngine is a serverless solution that allows you to focus on the back-end code, and keep your costs under control with a PAYG pay scheme.

In terms of coding experience, Firebase C.F is designed to make things much easier for you with the onCall method for instance. AppEngine still remain relatively easy to use and you can get your backend up and ready pretty fast.

Anyway, with AppEngine, and with the same server request and same dependencies as mentioned in the OP (cors, compression, rp, express), I can get the same JSON data in under 1 sec (versus 4.5sec with C.F.). That's quite an improvement, isn't it ? I probably can get that figure even lower.

This said, there are 2 different AppEngine configs : standard and flex. Standard would be the obvious choice for most developers I'm guessing. If you pick flex, you may want to read this first : Pricing of Google App Engine Flexible env, a $500 lesson

From a purely Google customer POV, i'm still not sure why I have to involve 2 different serverless services to make my things work, but that's the way.

I just wish Google's services were better described on their Firebase website, it would have saved me some precious time. I learned about AppEngine thanks to someone's comment on reddit. I wish the Google guys could have at least pointed me to their other solutions as a reply.

John Doe
  • 1,092
  • 3
  • 12
  • 25
0

To avoid so-called “cold start” which in fact is something you want to avoid looking at your questions, a good solution is to use an App Engine Flex.

App Engine is part of the different platform Google Cloud Platform (GCP). Yep, Firebase and GCP are different products, although in some parts of them are overlapping each other. Firebase is meant to be for mobile apps while GCP is a full cloud solution.

Some features are indeed the same. GCP project can be configured to be Firebase project and every Firebase project is in background GCP project. Solutions like Storage, Firesore and Functions are the same behind the scenes. When you create them in one of the platforms those are accessible as well from the other.

If you want to know more about GCP and Firebase you can read this.

Cloud Functions are solutions that by design should be used often, and they are automatically scaled. If they are not used they are not stored as a ready environment (scaled to 0 instances). Before first use the environment has to be prepared: instance needs to be created and function needs to be deployed.

To avoid “cold start” you have to use a solution where you keep at least one instance of your application alive all the time. Good choice will be App Engine Flex where you can set a minimal number of instances to 1: min_num_instances for automatic_scaling or instances in manual_scaling in the app.yaml file.

I think that there are more possibilities to do it within GCP, App Engine Flex seems to be natural choice when you want to avoid “cold start”.

I hope it will help!

vitooh
  • 4,132
  • 1
  • 5
  • 16
  • Mate. Isn't your answer almost word for word what I answered to my own post 12 hours earlier ? Please read my answer. AE flex is not the best default solution here. – John Doe Apr 24 '20 at 12:46
  • I consider this is just another opinion in this area. My answer is different enough to be separate answer. :) I just focused on fact that you have to keep at least one instance up if you want to use your function rarely and have it always ready and provided example. I read your answer several time and didn't find this there. – vitooh Apr 27 '20 at 06:32