Google Cloud Functions, unboxing the Serverless Sorcery

5 APRIL 2024Rohit Reddy Kaliki, Product Manager, Cloud Cost Management, Harness

banner

Overview

Taxis are great. They take you from point A to point B without the need to manage & maintain the vehicle – charging you only for the journey taken based on the time and distance, with a high degree of flexibility. Moreover, the experience of using a taxi requires minimal effort from the passenger. There's no need to focus on driving, navigating, or finding parking. But would you hire a taxi for all use cases? Probably not.

Serverless computing services are very similar – they offer great convenience to handle specific tasks without the burden of management and maintenance. They also offer a pay-as-you-go model, eliminating fixed costs while offering the flexibility to scale infinitely linearly with demand. While cloud functions provide significant advantages, they don’t offer the same degree of customisation and control as a traditional server.

The transition to Serverless compute services is fueled by the desire to enhance efficiency, minimize costs, and enable developers to concentrate on application development rather than on the complexity of the underlying infrastructure.

Google Cloud Functions, which is a key offering which is part of the serverless computing paradigm, offers all the benefits we discussed above. How does one manage Google Cloud Functions effectively? Which types of workloads are ideally suited for Cloud Functions, and how can they be executed both efficiently and economically? Let’s dig into some of these questions.

Google Cloud Functions: Introduction & Benefits

Google cloud functions are a serveless execution environment within GCP. This allows developers to run autonomous pieces of code with a high degree of separation of duties in an isolated environment. They can be hooked up to the rest of the architectural components via an event-driven architecture ranging from HTTP events to events ranging from other cloud services like Pub/Sub, Cloud Storage etc. This model allows developers to solely manage code while GCP handles the infrastructure, scaling & management aspects.

Benefits of Google Cloud Functions

Serverless Architecture

One of the key benefits is the abstraction from the management of the underlying infrastructure. The cloud functions scale automatically up and down based on the load, meaning they can handle millions of requests per second. Cloud functions integrations with other Google cloud services and responsiveness make them ideal for event driven architectures.

Development Velocity

Cloud functions enable rapid deployments and quick iterations, which can lead to a very positive impact on lead time to production and other DORA metrics. The simplicity in leveraging cloud functions combined with the ecosystem to leverage other cloud services efficiently makes cloud functions a compelling proposition.

Cost Effectiveness

Cloud functions have little to no upfront costs with a wide global reach. This results in low latency, high performance throughputs for end users across the globe. The pay-as-you-go model enables users to only pay for resources consumed as a function of time. This can lead to significant savings when used appropriately for workloads which have variable and spiky resource requirements.

Common Use cases

Cloud functions can be leveraged for a variety of use cases, but here are some of the most common use cases which maximize the advantages of the serverless model.

Event Driven Data Processing

Cloud functions are ideal for Extract, Transform, Load (ETL) tasks. They have tight knit integrations with Storage and Data services like Google Cloud Storage(GCS) and BigQuery. They can be triggered on first class triggers such as arrival of files into GCS, enabling processing in Batches or real-time. Google also offers other first class managed services like Dataflow & Dataproc for managed data transformations, but cloud functions are a great alternative if the use cases are not very complex and can benefit from the first class integration with other Google Cloud data products.

Lightweight API Development

Developers can create serverless APIs which can be invoked by the HTTP triggers. This approach simplifies hosting and building a scalable microservice, which comes with its own additional operational overhead challenges. This also enables each endpoint to scale independently with a high degree of separation of duties.

Integration and Automation

Cloud functions are a perfect intermediary layer between Google cloud services and other third party services. They can be leveraged to automate complex workflows, while maintaining a high degree of efficiency with minimal manual intervention. For example, they can be used to replicate data between Snowflake to BigQuery or handle alerting based on conditional triggers. They can also be used for trivial tasks like image resizing, data sanitization, or format conversion.

Comparisons with other clouds

All major cloud service providers have an equivalent to Google Cloud functions, each with its own unique value propositions. Let’s compare cloud functions with similar offerings from AWS and Azure and other serverless options.

AWS Lambda

AWS Lambda is Amazon Web Services' serverless Compute service, allowing users to run code without provisioning or managing servers. Like Google Cloud Functions, Lambda is event-driven and scales automatically, handling many requests in parallel. Key benefits that AWS Lambda offers are:

  • Integrations: Lambda offers extensive integrations with AWS services, including S3 (comparable to Google Cloud Storage), DynamoDB, and Kinesis, facilitating a wide range of serverless applications and workflows.
  • Runtime Support: AWS Lambda supports a broad array of runtime environments, including Node.js, Python, Ruby, Java, Go, .NET Core, and custom runtimes, providing flexibility in development.
  • Pricing: Lambda's pricing is similar to Google Cloud Functions, based on the number of requests and the duration of code execution.

Azure Functions

Azure Functions is Microsoft's serverless compute service within the Azure cloud ecosystem. It enables developers to run event-triggered code without managing infrastructure, similar to its counterparts in AWS and Google Cloud. Key benefits that Azure Functions offers are:

  • Development Environment: Azure Functions offers deep integration with Visual Studio and Visual Studio Code, making it appealing for developers working within the Microsoft development ecosystem.
  • Durable Functions: A unique feature of Azure Functions is Durable Functions, an extension that enables stateful functions in a serverless environment, ideal for complex orchestrations.
  • Multi-language Support: Azure Functions supports a range of programming languages, including C#, F#, JavaScript, Python, and PowerShell.

Others

Beyond these major cloud providers, there are other serverless platforms worth considering:

  • IBM Cloud Functions: Based on Apache OpenWhisk, IBM's serverless platform allows for the execution of code in response to events or direct calls.
  • Cloudflare Workers: Cloudflare offers a unique approach to serverless with its edge computing platform, running code closer to the user for improved performance.

Each serverless offering has its strengths and trade-offs, influenced by factors such as ecosystem integration, language support, and unique features. Organizations should consider their specific requirements, existing cloud relationships, and preferred development environments when choosing a serverless platform.

Optimization strategies

Let’s continue to use the taxi analogy to explain how to deploy cost efficient strategies.

Choice of Region

Choosing the right region to deploy your cloud function can significantly reduce your costs. This is very similar to choosing a taxi that is closest to you. If the Taxi (Cloud Function) is farther away, it will take longer (Latency) to reach you, consuming more fuel and cost (higher data transfer costs, resulting in latency). Picking the right region for your cloud function will result in lower network costs and reduce latency.

Concurrency

Google Cloud function Gen2 has a concept of concurrency – you can set the maximum number of concurrent tasks that can execute on a specific instance of a cloud function. This is very similar to carpooling, which enables more efficient routes and higher throughputs (handling multiple requests with the same resource) while being mutually beneficial for passengers.

Resource Configuration

Optimizing the configuration for your cloud functions is like choosing the right size and type of taxi for your ride. Just as how you wouldn’t hire a van for a single person (wasting fuel & space), you shouldn’t allocate CPU and memory disproportionate to the actual requirements of a specific task. Conversely, hiring a compact car for a large group of people with a lot of luggage might lead to performance challenges and throttling. Finding the right balance for your resource configurations is extremely important. Gen2 Cloud functions offer the capability to scale CPU and memory independently allowing to better configure CPU intensive/memory intensive workloads.

AutoScaling

Autoscaling for cloud function is like managing a fleet of taxis that automatically adjusts based on demand. During peak hours, more taxis are deployed to better service the demand, while during off-peak hours, the fleet size reduces. Similarly, you can enable AutoScaling of cloud functions. You can set the minimum number of workloads that you want to always run irrespective of traffic and set a maximum to cap off costs beyond a certain point based on the trade off between latency and costs.

Cold Starts

Cold start is very similar to a Taxi that is arriving from a distance since none others were available nearby. The initial delay in handling the request can be longer, but subsequent requests can happen more quickly as the system is already up and running to handle requests. Optimizing for cold starts can reduce costs due to decreased overall processing times. This can be strategically achieved by enabling concurrency and optimizing the min AutoScaling variable. Cloud functions also have a cool-down period before being completely deallocated, similar to how taxis wait for subsequent rides in their current location before returning to a central hub like an Airport or a mall with higher demand.

Harness & Cloud Functions

Cloud Asset Governance

Cloud Asset Governance, part of Harness Cloud Cost Management, can help optimize Cloud functions. Here's an example policy to delete orphaned cloud functions. This policy can automatically detect and clean up unused cloud functions. For cloud functions with non zero instances, this can lead to significant cost savings.

policies:
  - name: gcp-unused-cloud-functions-cleanup
    resource: gcp.cloud-function
    description: |
      Delete cloud functions that have not been invoked for over 90 days.
    filters:
      - type: metrics
        name: invocations
        days: 90
        value: 0
    actions:
      - type: delete

Cloud Asset Governance can also help with various other use cases around CPU and memory allocation. Read the documentation to learn more.

Cost Visibility, Budgets & Anomalies

Harness CCM can be used to visualize costs stemming from GCP cloud functions. Perspectives offer SKU level visibility of how these costs are split. Custom Dashboards can be leveraged for enhanced visualization options, custom alerting, and scheduled reporting.

image1.png

Harness CCM also detects anomalies on cost spikes related to Cloud functions, enabling users to proactively remediate snowballing of costs. Users can also set up budgets specific to individual SKUs and set up alerting on budget overages.

Want to learn more? Join the FinOps Excellence Community

Dive deeper into the world of FinOps by joining our Finops Excellence community. Here, like-minded professionals come together to share best practices, exchange insights, and stay updated on the latest FinOps trends. Join today!