How do Cloud Functions work?

Developer building products for Fortune 500!
Search for a command to run...

Developer building products for Fortune 500!
No comments yet. Be the first to comment.
Writing code is both an art and a science, but it can be a daunting task. As developers, we invest a significant amount of time crafting intricate lines of code. However, what if I told you that a simple habit of adding code comments can revolutioniz...

In the fast-paced world of software development, clear and comprehensive code documentation is like a treasure map 🗺️ that helps fellow developers navigate your codebase. In this blog post, we'll embark on an exciting journey to discover the power o...

As a developer, you understand that writing clean and efficient code is essential for maintaining and scaling your Node.js applications. Code quality not only affects the performance and stability of your application but also makes it easier for your...

"Kubernetes is awesome, I would love to see auto-healing in my systems” Working with new frameworks is fascinating, but it also births a few questions. “Are enterprises actually using it?” “Is it really worth the switch? my current solution works fin...

Serverless computing is one of the fastest-growing trends of the decade. Cloud Functions is the Function-as-a-Service (FaaS) offerings from Google. Serverless minimizes DevOps complexity while maintaining high elasticity.
With Cloud Functions, you only write the business logic without thinking about infrastructure.
Components of a Cloud Function
Trigger
The entry point of the cloud function. It could be an HTTP request, Pub-sub message, or a Cloud Scheduler event.
Configuration
The runtime configuration for your function. These include values like env variables and max number of parallel executions allowed.
Code
The business logic and the dependencies for your function. The event arguments are passed in the request object of the function.
Integration with 3rd Party Apps
Interacting with 3rd Party Apps like Slack, SendGrid pretty much any 3rd API.
Serverless Backends
This is a common use-case for mobile where the usage is highly elastic.
DevOps triggers
Triggering a code build in response to a pull request approval.
Data processing / ETL
Basic ETL use cases like file processing as soon as a new file is created. Cloud Function has a Cloud Storage trigger available.
Webhooks
A common use-case for OAuth2 flows like server-side redirections.
Cloud Functions are simple executable Linux Containers.
The lifecycle stages of a Cloud Function
The cloud function is converted to an executable container in the build process. The container is automatically created from the code in the build stage. This container is stored in the Google Container Registry. During execution, the container is pulled from the registry.

Source: Google Cloud
Let’s understand each of the layers.
Operating System
This operating system image to run the function. Maintainance activities like patching and updating are handled by Google.
Runtime
The runtime will be installed on the image during execution. This is the same runtime configured during creation.
Functions Framework
This is the piece that actually runs your code (also open-source) on the trigger.
Function Code and dependencies
This is the actual business logic function we want to run as a response to the trigger.
Role of Functions Framework
It acts as a mediator between the trigger and your source code. It abstracts the trigger objects to appropriate req and res objects.
We get an executable container at the end of the build stage. On triggering a function, a container is deployed to an instance. An instance could be Linux / Windows machine.
The container execution occurs in the following steps:
Download and Deploy the container image
Run the container with arguments.
The above steps are necessary for each execution.
Now let’s say we are making a fresh call to the function. The executor will look for an instance that already has this container(“hot start”).
If it cannot find such an instance, then it has to request an instance and deploy the container. As a result, a delay in form of latency is introduced to spin up the container. This is also known as a “cold” start.
Cloud Functions greatly make your project needs serverless. However, there are some important limitations to be mindful of.
Max Execution Time
The maximum duration for which a cloud function can run is 9 mins. The limits are frequently updated, so be sure to check the latest limits.
Memory Limit
The runtime memory limits are ranging from 128 Mb to 2 Gb.
Number of accessible CPU cores
During execution, the function has only one CPU core available. Thus, there are limits to parallel processing use cases. A good practice is to trigger additional cloud functions for parallel processing.
Think like a Functional Programmer
Functional Programming concepts like Immutability and Pure Functions would prove to be beneficial here.
Remove unwanted files and dependencies
Decreasing container size helps reduce cold start time and overall resource usage. A case study shows that reducing dependencies gave helped 2x in reducing cold start times.
Test with Functions Framework
The Functions Framework runs the business logic at runtime. For local development and testing make use of Functions Framework.