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
The entry point of the cloud function. It could be an HTTP request, Pub-sub message, or a Cloud Scheduler event.
The runtime configuration for your function. These include values like env variables and max number of parallel executions allowed.
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.
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
In the build process, the cloud function is converted to an executable container. The container is automatically created from code in the build stage. This container is stored in the Google Container Registry. During execution, the container is pulled from the registry.
Let’s understand each of the layers.
This operating system image to run the function. Maintainance activities like patching, updating is handled by Google.
The runtime will be installed on the image during execution. This is the same runtime configured during creation.
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, 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.
I am a Software Engineer with experience building robust platforms for Fortune 500 companies.