Introduction: Improve Cold-Start of Firebase Cloud Functions

About: Developer, Engineer and love tech...

N.B This was implement on both a Casino Website and Bet Prediction Website

Now a days Serverless architecture are the modern world for hosting websites. The advantages are that you can create value without ever thinking about the infrastructure or underlying software. Essentially, it lets you focus on your code.

A cold start is caused by the initial process of creating a container which runs your code. Based on the language you’re using, the latency can often be well over a few seconds.

The problem is when traffic is low and so latency is bigger (more traffic less latency). To avoid this the instance must keep on going and not fall to sleep. Another Problem is that if you have

few seconds of latency to start instance, users will leave your site since the average wait now a days is 3 seconds.

You can sometimes experience ~30 seconds after any period of inactivity. So Here’s how to avoid the problem.

Step 1: 1st Tactic to Reduce Cold Start

There are two main tactics you can use when battling cold

starts: First of all remove all necessary npm that take time to load and second tactic is to keep the instance Hot!

minimising the duration of the cold start (meaning cutting down the latency of the cold start itself) and minimising the number of times cold starts occur. The former is done by using common programming patterns and common sense, while the latter is achieved with a technique called function warming.

So the First tactic is to Reduce all the NPM installs that are not wanted inside the project. Also improve the Code in all screens which means less data, which means more faster to load. Also very important is Lazy loading is Ideal for large js files.

Updating all the NPM projects to the latest versions are also ideal to get better performance.

A good link for this is the Tips and tricks from Google Firebase team:

https://firebase.google.com/docs/functions/tips

https://www.youtube.com/watch?v=IOXrwFqR6kY

Step 2: Second Tactic How to Reduct Cold Start of Firebase Function

The Second tactic is to ping my firebase functions every x
amount of time to keep them warm. First method is t use a website that does the pining for you.

This Ping API do ping from various countries: https://ping-api.com/ . You can register for Free using a Github Account.

The following are how to setup the Ping:

Go to Setting tab and add a New title Project, Url Website that would like to ping. the two most important settings are the time that the Ping will be Scheduled, for example every 6 Hours (it can be scheduled from 1minute to 24 hours) and after set the region that the Ping will be done.

Best Thing to do is from all the regions since will get the most Ideal.

Other features include email notification when there is a Post failure and get the test information to all members' email.

Second method which is also good is to create C# program to run the pings for you every x amount of time. The following is the code:

Code:

List website = new List();

website.Add("https://www.feedinco.com/tips/football-tips");

website.Add("https://www.feedinco.com/tips");

website.Add("https://www.feedinco.com/tips/btts");

website.Add("https://www.feedinco.com/blog/2019-Hottest-Wags---Football-Players-wife---girlfriends");

website.Add("https://www.feedinco.com/tips/soccer-predictions");

website.Add("https://www.feedinco.com/tips/tomorrow");

website.Add("https://www.feedinco.com/tips/overunder");

website.Add("https://www.feedinco.com/tips/tomorrow/overunder");

website.Add("https://www.feedinco.com/acca");

website.Add("https://www.feedinco.com/tips/tomorrow/btts");

website.Add("https://www.feedinco.com/tips/best-bets");

website.Add("https://www.feedinco.com/betting-tools");

website.Add("https://www.feedinco.com/tips/tomorrow/doublechance");

website.Add("https://www.feedinco.com/tips/doublechance");

website.Add("https://www.feedinco.com/tips/predictions-wins");

website.Add("https://www.mrcasinoslots.com/online-roulette-bonus");

website.Add("https://www.mrcasinoslots.com/free-casino-slot-games-with-bonus-rounds");

website.Add("https://www.mrcasinoslots.com/free-slots-with-bonus-and-free-spins");

website.Add("https://www.mrcasinoslots.com/free-slot-machine-games-with-free-spins");

website.Add("https://www.mrcasinoslots.com/free-online-slots-with-bonuses");

website.Add("https://www.mrcasinoslots.com/play-free-slots-online");

website.Add("https://www.mrcasinoslots.com/free-slots-real-money");

foreach (var a in website)

{

WebRequest request = System.Net.HttpWebRequest.Create(a);

try

{

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

WebResponse response = request.GetResponse();

StreamReader sr = new StreamReader(response.GetResponseStream());

Console.WriteLine(a + " ping ok");

response.Close();

response.Dispose();

}

catch (WebException wex)

{

Console.WriteLine(a + " ping timeout error!!");

}

request.Abort();

Thread.Sleep(5000);

}

Check out the performance on the follwoing websites: Free slots online and Football Tips