Our blog

Don't Get Burned: Cost-Effective Serverless with Google Cloud Run

Linkedin logoX logo
Cost-Effective Serverless with Google Cloud Run
Cost-Effective Serverless with Google Cloud Run

Serverless architecture is revolutionizing application development, offering unparalleled scalability and cost efficiency. Google Cloud Run, a fully managed serverless platform, takes this a step further, by allowing you to deploy and scale containerized applications without the need to manage infrastructure. However, the very advantages of this aspect can lead to unexpected costs if not approached strategically. This article dives into essential practices for building cost-effective serverless applications on Cloud Run to ensure you get all of the benefits without breaking the bank.

1. Understand the Billing Model: Pay for What You Use (rounded up to the nearest 100 millisecond, literally)

Cloud Run operates on a granular pricing model that only charges you for the resources you actually use during the execution time of your code. What does that mean in practice? No charges for idle instances. However, note that there are different pricing models for Tier 1 and Tier 2 regions. Also, remember about:

  • Request-based billing: You pay for each request handled by your application, with pricing tiers based on factors such as request duration, allocated memory, and regional location.
  • Free tier: Google Cloud offers a generous free tier for Cloud Run, so you can experiment and run small applications for free.

Billing model for Cloud Run

Pic. 1: Billing model for Cloud Run

Key takeaway: Optimize your code for short execution times and choose the right memory allocation for your workload to minimize costs.

2. Embrace Autoscaling: The Art of Right-Sizing

Cloud Run dynamically adjusts the number of container instances based on incoming traffic. This ensures your application can handle traffic spikes while scaling down to zero instances when idle, saving you precious dollars. To enhance the effect, think about:

  • Concurrency: Define the maximum number of requests a single container instance can handle simultaneously. Higher concurrency reduces cold starts, but can lead to underutilized resources. Find the sweet spot for your application.
  • Minimum instances: While setting a minimum number of instances (e.g., 1) eliminates cold starts and ensures responsiveness, you will be charged even if there is no traffic. Use this feature strategically—consider traffic patterns and cost implications.

Key takeaway: Mastering autoscaling is essential to finding the perfect balance between performance and cost. Monitor them continuously, analyze your traffic patterns, and experiment with different concurrency and minimum instance settings to fine-tune your configuration.

3. Optimize Cold Starts: Speed is Money

Cold starts, or the time it takes to initialize a new container instance, impact both performance and cost. While Cloud Run handles much of the heavy lifting, you can still optimize your application to minimize cold start duration:

  • Minimize dependencies: Keep your container images small and dependencies minimal to reduce startup time. Keep in mind that each library and dependency adds to the size of your image and ultimately increases cold start time.
  • Use global variables wisely: Avoid complex initialization routines that run on every cold start. Consider lazy loading or initializing resources only when needed.
  • The minimal number of instances minimizes the number of cold starts, but, as you already know, comes at an ongoing cost. Take a moment to address this issue, considering your budget and application needs first and foremost.

Key takeaway: A fast startup time not only improves the user experience but also reduces unnecessary resource consumption, which ultimately saves you money.

4. Mind Your Revisions: Inactive Doesn't Mean Free

Cloud Run allows you to manage different versions of your service through revisions. While this is great for testing and rollbacks, it can lead to unexpected costs if not managed carefully.

  • Inactive, tagged revisions consume resources: Even if a revision is not serving traffic, it still consumes resources and incurs costs, especially when minimum instances are configured.
  • Implement a cleanup strategy: Regularly delete old and unused revisions to avoid unnecessary costs. You can automate this process using scripts or tools such as Cloud Functions.
  • Use traffic tags strategically: While tagging revisions for traffic splitting and A/B testing is helpful, be aware of the costs involved. Ensure that tagged revisions with minimum instances are truly necessary for your testing and deployment strategies.

Cloud Run revisions

Pic. 2: Cloud Run revisions

Key takeaway: Don't overlook inactive revisions. Proactively manage and clean up your revisions to avoid unexpected costs and keep your serverless budget in check.

5. Leverage Committed Use Discounts: Maximize Savings for Predictable Workloads

While the pay-as-you-go nature of Cloud Run provides great flexibility, predictable workloads can benefit significantly from Committed Use Discounts (CUDs). Google Cloud offers two types of CUDs for Cloud Run:

a. Compute Flexible Committed Use Discounts (Flexible CUDs)

  • Ideal for: Predictable spend on Cloud Run services with CPU always allocated (such as minimum instances) or Cloud Run Jobs.
  • Flexibility: Applies to all projects within a Cloud Billing account, providing more flexibility than traditional CUDs.
  • Discount: 28% discount for a one-year commitment and 46% discount for a three-year commitment.
  • Billing: Commit to a minimum hourly spend on eligible resources. This becomes your monthly commitment fee, billed regardless of actual usage. Usage in excess of the commitment is charged at on-demand rates.

b. Committed Use Discounts (CUDs)

  • Ideal for: Predictable spend on all Cloud Run services and jobs within a specific region.
  • Scope: Applies to all projects within a specific region and Cloud Billing account.
  • Discount: 17% discount for a one-year commitment. (Note: Choosing a three-year term does not provide a higher discount, but acts as a bundle of three one-year terms.)
  • Billing: Commit to a minimum hourly spend on all Cloud Run resources in the selected region. This becomes your monthly commitment fee, billed regardless of actual usage. Usage in excess of the commitment is charged at on-demand rates.

Example commitment level

Pic. 3: Example commitment level

Key Considerations for CUDs: CUDs are most beneficial for applications with consistent resource consumption. Analyze your historical usage data to determine if your workload justifies a commitment. Evaluate your long-term plans and budget constraints when choosing between one- and three-year commitments. Keep in mind that CUDs cannot be canceled once purchased. Google Cloud applies CUDs before Flexible CUDs. Be sure to optimize your CUD usage before leveraging Flexible CUDs for additional savings. Monitor your CUD usage regularly to maximize your investment. Adjust your commitment levels as needed based on the evolving needs of your application.

6. Monitor, Analyze, Optimize: The Continuous Cost-Saving Loop

Cloud Run Monitoring provides invaluable insights into your services, empowering you to identify performance bottlenecks and optimize costs. This is not a one-time task but an ongoing process.

  • Track key metrics: Closely monitor request latency, error rates, number of instances, memory usage, and network traffic. Pay close attention to spikes or unusual patterns that could indicate potential cost optimizations.
  • Set up alerts: Configure alerts based on predefined thresholds for critical metrics. This allows you to proactively address potential issues before they escalate into costly problems.
  • Analyze logs: Dive deep into application logs to identify performance bottlenecks, errors, and areas for optimization. Cloud Logging provides powerful tools for analyzing and visualizing log data to help you pinpoint cost-saving opportunities.

Key Cloud Run metrics

Pic. 4: Key Cloud Run metrics

Key takeaway: Continuous monitoring and analysis are crucial for identifying cost optimization opportunities and ensuring the long-term efficiency of your serverless applications. Review your monitoring data regularly, analyze trends, and adjust your application and infrastructure configurations accordingly.

Summary

By following these best practices and embracing a cost-conscious mindset throughout the development lifecycle, you can harness the power of Cloud Run functions to build scalable, resilient, and highly cost-effective serverless applications. However, it's essential to remember that Cloud Run is just one piece of the overall Google Cloud serverless puzzle. Determining the ideal serverless solution for your specific workload requires careful consideration of various factors that are beyond the scope of this article. If you feel you need a helping hand with taming your Cloud Run costs and optimizing your spend on Google Cloud, reach out to us! We're here to help you find the best solutions for your needs.

Published:

Author:

Mateusz Mazur

Cloud Advisor

Related or similar posts

Modern SOC Management with IaC and CI/CD Processes

Modern SOC Management with IaC and CI/CD Processes

Mateusz Mędel

blog