Git Product home page Git Product logo

optimize-serverless-google-cloud's Introduction

Optimize Java Apps in Cloud Run - Google Cloud

This material dives into the features of optimized, modern Java applications deployed in Cloud Run in the Google Cloud. It is intended to be a living repo where new optimizations will constantly be added.

Why this work

App Optimization for Cloud Run should always be framed in the larger context of production readiness of a service for a Cloud Run PROD environment.

There are multiple aspects to writing effective services for serverless environments and they all revolve around the combination of service design, implementation, testing, configuration with environment configuration and optimization best practices.

Optimization is shifted-left and part of the service dev lifecycle from the beginning.

How to think about production-readiness

You generally think that a production-ready cloud service must be:

  • Stable and Reliable
  • Scalable and Performant
  • Fault Tolerant without any single point of failure
  • Properly Monitored
  • Documented and Understood
  • Properly secured

How to think about optimizations

Optimizing any app for Cloud Run requires a balance of different aspects to be considered, therefore it is important to always have a clear picture of what it is that you are optimizing for:

  • start-up time
  • execution latency
  • resource consumption (memory & CPU)
  • concurrency
  • image size
  • easy maintainability
  • lower costs

Outcomes

This material contains lessons learned from participation in various projects or publicly available knowledge and documentation.

You can:

  • revisit the production-readiness checklist as you build the service and review it before deploying services to a Production serverless environment
  • use the material as a starting point for optimization workshops or discussions around production readiness

Materials:

  • production-readiness checklist
  • app w/complete set of services (code, config, environment setup) following best practices
  • alternate versions of the services showing what is happening if you don't do this

Service production-readiness checklist

Production Readiness Checklist

The App

A set of services is provided to illustrate the different aspects, following this simple architecture: App

Project & Source Code

Source code recommendations can be grouped into the following distinct categories:

Project

  • Java - Use latest LTS version
    • Java 17 currently
    • Better performance, security and resource consumption are achieved by simply building and running the app with the latest Java LTS release
  • Maven - Use the latest version
    • 3.8.5 currently
  • Gradle - Use the latest version
    • 7.4.2 currently
  • Dependency management - Use a bill-of-materials(BOM) for managing dependencies and versions consistently for libraries that work together without linkage errors

Spring

  • Use the latest version of Spring - 2.7.0 currently
    • Spring releases constantly fix issues and CVEs in the frameworks and their dependencies
  • Use Spring profiles for environment specific configuration segregation
  • Do NOT include Developer tools in Production build
  • Use lazy initialization - potentially not useful if using min-instances, as init could have occurred
  • Avoid class scanning by limiting or avoiding class scanning
    • Improvements are app dependent
  • Avoid nested library archives JARs - valid for OpenJDK (Hotspot)
    • Building native images with GraalVM eliminates the problem as only classes retained during ahead-of-time compilation will be included in the app image

Application source code

  • Externalize application configuration - do NOT hard-code configs or package config files into images!

Java Virtual Machine optimizations

  • Always set the garbage collector
    • Very important for longer running, smaller footprint Cloud Run services
    • OpenJDK (Hotspot VM)
      • G1 GC set as default for Java 17 for a server class machine - defined as >=1,792 MB RAM and >= 2CPUs
      • Serial GC is automatically set for <1,1791 MB
      • The algorithm used for setting the garbage collector
    • GraalVM
      • Serial GC is set by default for low memory footpring and small Java heapsizes
      • GC implementations
  • Use container-aware versions when deploying a Java app in Cloud Run or a Kubernetes-based environment
    • container awareness is important as it allows deployments to cloud orchestration systems to limit container resources via CPU and memory quotas
    • Java 17 and Java 11 are container aware since general availability (GA)
    • Java 8 is container aware since version 8u202
  • Understand JVM memory usage
  • Catch and handle properly Internal errors - 5XX under the container runtime contract
  • Improve start-up time using application class-data sharing
  • Find the optimal thread stack size through profiling, to reduce heap consumption
  • Use Native Java images for containers

Testing

  • Test the app with containers - use Testcontainers
    • Use Google emulators for Google managed services
    • Use open-source test containers for Postgres, MySQL and SQLServer for relational databases running in CloudSQL

Build and Packaging

  • Use cloud-native buildpacks to build the container images
  • Minimize container images by using optimized container images

Observability

Operations and Resiliency

Caching

Database

Security

Documentation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.