Serverless Application on Google Cloud

Ever since Google Cloud released Cloud Function (BETA), there has been lots of excitement in the community. Google Cloud functions is an event-driven serverless compute platform which abstracts major configurations form developers like auto-scaling, on-demand execution and no server provisioning. This helps a developer to focus more on the development of service. And that's the reason where we are also excited. 

Before Moving to serverless applications I will briefly explain what is Google Cloud Functions.

  1. It is the simplest way of running your code in the cloud
  2. Automatically scales, highly available and fault tolerant (Deploy like a pro)
  3. No servers to provision, manage, patch or update (Major relief for any developer)
  4. Pay only while your code runs
  5. Connects and extends cloud services

Behind the hood, Cloud Functions waits for a cloud service (Supported Google Cloud services) to emit an event and based on that event it responds by either writing back the output to the service or invokes another service on the cloud. 


At the time of writing this blog Cloud Functions provides two flavours of functions: 

  1. HTTP Functions 
    • Example: Web API, Web-hooks (HTTP Post Request)
  2. Background Functions
    • Triggers when certain cloud events have occurred
    • Currently, cloud functions response to pub-sub, cloud storage and firebase events

There are many different use cases in which a developer can work with Cloud Functions. For example Real Time File Processing. 



Above example describe the fact that Cloud Function event-driven compute platform, Whenever a user is uploading an image via mobile application to cloud storage a cloud function automatically triggers and resize the uploaded image. This links points to more use cases using Cloud Functions. 

Note: Only use case which is not supported by Cloud Function is the job which takes more than 9 mins to run. This is a quota set for Cloud Functions.  

One of the benefits of using cloud functions is that it is cheap: For every million invocations of cloud function, it costs only $0.4 plus Computes time and Network (Compute time and Networking price is relatively small as compared to Cloud Function million invocations). One can check actual pricing here

But, Let's back up a minute and ask why Cloud Functions not applications? We have been building applications for a long time then why we need to deploy small snippets of functions, it sounds like lots of work. Well, let's rewind and take a look at how we built an application a few years back.

Why Functions? 

A few years ago all the applications were running on-premise datacenters. These datacenters were shared machines as servers at the time were pretty much costly and to add new server it adds up a lot of money and to optimise these multiple applications were deployed on same servers. But with this scenario not everything was okay, there were some issues like conflicting dependencies.  


Then virtual machines came along and this was great as we can dedicate the whole OS to an application which removes the dependency conflict. But it is still a lot of overhead to manage an entire OS for a single application and it takes a while to boot up VM. Then along came containers, this is perfect as same fundamental OS kernel is shared across multiple services even though in each container each application thinks that they have their own OS. But there is still a lot of management involved in that. You have to see how many versions or how many copies of them is required. How many of them are needed and how many are up and running. How does networking work? There is still a lot of infrastructure management. So, then move into the world of a platform as a service and then ultimately functions as a service and somewhere in the spectrum we get into the world of what we called serverless. And the basic idea of serverless is that you just write a code and deploy it and that all you need to worry about. 

But still why functions? What advantage we get by just having functions? Well the answer is microservices. 


We move these applications out and build them as microservices, but why? One advantage is the independence of scale. Similarly, let's say different services have different release cycle then microservices architecture plays a great role in removing the complexity of different release cycle. So, making them independent lets you scale independently and also gives you better release cycle by making different teams work on that. 
But when you extract the microservice then you have an overhead of infrastructure management. And that's the place where serverless come into play. 

Principle of Serverless

  • Invisible infrastructure
    • You just worry about writing the code and the cloud provider takes care of scaling in up and down, load balancing etc. 
  •  Automatic Scaling
    • It automatically scales for us, not only up but also down and scales down to zero. Which means that if your code is not running then you are not paying anything. This is great application having unpredictable traffic pattern.  
  • o Paying for unused CPU cycle

Show me the Code! 

Let move onto deploying one actual cloud function. We will write a hello world! cloud function which translates bunch string into given language. But before that let's take a look into project structure for any cloud functions. 

Any Cloud Functions consist of two files

  1. index.js - actual code for cloud function
  2. package.json - dependency management

Noe let's write a code for cloud function which takes a string and converts it into given language. 

What we have done above is wrote a code for cloud function to translate a string in given lang and in JSON file we defined what dependency we needed for this cloud function.  

Now let's deploy this cloud function. 

$ gcloud functions deploy holaworld --trigger-http

After the deployment of function, it will automatically generate a httpsTrigger: URL: this is the end point for a deployed function which on POST request triggers an event.

Screen Shot 2018-05-28 at 18.25.24.png

Now let's the functions: 

Screen Shot 2018-05-28 at 18.26.58.png

We used *nix default curl command to test the function. As we can see that it is successfully translating "Hello Friends" in French and Japanese. 

This is one very small use case of Cloud functions we demonstrated here. And imagine we build several different an application like this to have more agility in application and in the team. 


Nowadays many organisations are moving towards development of microservices in a serverless way and Google Cloud is the good place to start deploying the services using Cloud Function. I find it very intuitive and easy as a developer, I can focus more on my code and not on infrastructure. 
Cloud Function is very promising and people behind this at Google are working hard to release cloud functions for deferent other languages. Hoping to see Cloud Function for python soon. 

With cloud function, you are actually managing your infrastructure. In other words, it is 

Infrastructure as Code