{"id":15804990,"url":"https://github.com/ddobrin/optimize-serverless-google-cloud","last_synced_at":"2025-10-23T21:59:07.954Z","repository":{"id":68593064,"uuid":"485155755","full_name":"ddobrin/optimize-serverless-google-cloud","owner":"ddobrin","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-17T14:54:00.000Z","size":727,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-21T08:44:42.760Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ddobrin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-24T22:33:04.000Z","updated_at":"2022-11-15T07:17:00.000Z","dependencies_parsed_at":"2023-02-28T11:31:30.046Z","dependency_job_id":null,"html_url":"https://github.com/ddobrin/optimize-serverless-google-cloud","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ddobrin/optimize-serverless-google-cloud","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddobrin%2Foptimize-serverless-google-cloud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddobrin%2Foptimize-serverless-google-cloud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddobrin%2Foptimize-serverless-google-cloud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddobrin%2Foptimize-serverless-google-cloud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ddobrin","download_url":"https://codeload.github.com/ddobrin/optimize-serverless-google-cloud/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddobrin%2Foptimize-serverless-google-cloud/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272260593,"owners_count":24902391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-26T02:00:07.904Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T02:02:45.341Z","updated_at":"2025-10-23T21:59:07.879Z","avatar_url":"https://github.com/ddobrin.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Optimize Java Apps in Cloud Run - Google Cloud\n\nThis 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.\n\n## Why this work\nApp Optimization for Cloud Run should always be framed in the larger context of production readiness of a service for a Cloud Run PROD environment. \n\nThere 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. \n\nOptimization is `shifted-left` and part of the service dev lifecycle from the beginning.\n\n## How to think about production-readiness \nYou generally think that a **production-ready cloud service must be:**\n* Stable and Reliable\n* Scalable and Performant\n* Fault Tolerant without any single point of failure\n* Properly Monitored\n* Documented and Understood\n* Properly secured\n\n## How to think about optimizations\nOptimizing 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**:\n* start-up time\n* execution latency\n* resource consumption (memory \u0026 CPU)\n* concurrency\n* image size\n* easy maintainability\n* lower costs\n\n## Outcomes\nThis material contains lessons learned from participation in various projects or publicly available knowledge and documentation. \n\nYou can:\n* revisit the production-readiness checklist as you build the service and review it before deploying services to a Production serverless environment\n* use the material as a starting point for optimization workshops or discussions around production readiness\n\nMaterials:\n* production-readiness `checklist`\n* app w/`complete set` of services (code, config, environment setup) following best practices\n* alternate versions of the services showing `what is happening if you don't do this`\n\n## Service production-readiness checklist\n\n![Production Readiness Checklist](images/Main.png)\n\n## The App\nA set of services is provided to illustrate the different aspects, following this simple architecture:\n![App](images/AppArch.png)\n\n\n## Project \u0026 Source Code\nSource code recommendations can be grouped into the following distinct categories:\n\n### Project\n* Java - Use latest LTS version \n    * Java 17 currently \n    * Better performance, security and resource consumption are achieved by simply building and running the app with the latest Java LTS release\n* Maven - Use the latest version\n    * 3.8.5 currently  \n* Gradle - Use the latest version \n    * 7.4.2 currently\n* Dependency management - Use a bill-of-materials(BOM) for managing dependencies and versions consistently for libraries that work together without linkage errors\n    * [Spring Boot dependencies](https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.dependency-management)\n    * [Spring Cloud dependencies](https://spring.io/projects/spring-cloud)\n    * [Spring Native dependencies](https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/#_add_the_spring_native_dependency)\n    * [Google Cloud BOM](https://cloud.google.com/java/docs/bom)\n    \n### Spring\n* Use the latest version of Spring - 2.7.0 currently\n    * Spring releases constantly fix issues and CVEs in the frameworks and their dependencies\n* Use [Spring profiles](https://docs.spring.io/spring-boot/docs/1.2.0.M1/reference/html/boot-features-profiles.html) for environment specific configuration segregation \n* Do NOT include Developer tools in Production build\n    * Leverage Maven profiles for builds and remove [Spring Developer Tools ](https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools)\n* Use [lazy initialization](https://cloud.google.com/run/docs/tips/java#lazy-init) - potentially not useful if using `min-instances`, as init could have occurred\n* Avoid [class scanning](https://cloud.google.com/run/docs/tips/java#class-scanning) by limiting or avoiding class scanning\n    * Improvements are app dependent\n* Avoid [nested library](https://cloud.google.com/run/docs/tips/java#nested-jars) archives JARs - valid for OpenJDK (Hotspot)\n    * Building native images with GraalVM eliminates the problem as only classes retained during ahead-of-time compilation will be included  in the app image\n\n### Application source code\n* Externalize application configuration - do NOT hard-code configs or package config files into images!\n\n## Java Virtual Machine optimizations\n* Always set the garbage collector\n    * Very important for longer running, smaller footprint Cloud Run services\n    * OpenJDK (Hotspot VM)\n        * G1 GC set as default for Java 17 for a `server class machine` - defined as \u003e=1,792 MB RAM and \u003e= 2CPUs\n        * Serial GC is automatically set for \u003c1,1791 MB\n        * The [algorithm used](https://github.com/openjdk/jdk/blob/3121898c33fa3cc5a049977f8677105a84c3e50c/src/hotspot/share/runtime/os.cpp#L1673) for setting the garbage collector\n    * GraalVM \n        * Serial GC is set by default for low memory footpring and small Java heapsizes\n        * [GC implementations](https://www.graalvm.org/22.0/reference-manual/native-image/MemoryManagement/#:~:text=A%20native%20image%2C%20when%20being,them%20is%20the%20memory%20management.) \n* Use [container-aware versions](https://cloud.google.com/run/docs/tips/java#container-aware) when deploying a Java app in Cloud Run or a Kubernetes-based environment\n    * container awareness is important as it allows deployments to cloud orchestration systems to limit container resources via CPU and memory quotas\n    * Java 17 and Java 11 are container aware since general availability (GA)\n    * Java 8 is container aware since version 8u202\n* Understand JVM memory usage\n    * Native memory tracking can't be set via JAVA_TOOL_OPTIONS\n    * [Set start-up arguments](https://cloud.google.com/run/docs/tips/java#jvm-memory) in the container image entrypoint or the Buildpack parameters if in use\n    * Useful - [Java memory calculator](https://github.com/cloudfoundry/java-buildpack-memory-calculator)\n* Catch and [handle](https://cloud.google.com/run/docs/tips/java#handling_sequential_5xx_responses_under_the_container_runtime_contract) properly `Internal errors - 5XX` under the container runtime contract\n    * see also [source code](#source)\n* Improve start-up time using `application class-data sharing` \n    * Hotspot JVM - [Analysis](https://ionutbalosin.com/2022/04/application-dynamic-class-data-sharing-in-hotspot-jvm/), [slides](https://ionutbalosin.com/wp-content/uploads/2022/05/Techniques-for-a-faster-JVM-start-up.pdf) and [source code](https://github.com/ionutbalosin/faster-jvm-start-up-techniques/blob/main/app-dynamic-cds-hotspot/README.md)\n        * Spring Boot - use [shaded JARs](https://cloud.google.com/run/docs/tips/java#appcds-springboot)\n    * Native Images with GraalVM - use Ahead-of-Time (AOT) compilation\n        * Major frameworks have AOT support: \n            * [Spring AOT](https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/#aot)\n* Find the [optimal thread stack size](https://cloud.google.com/run/docs/tips/java#thread-stack) through profiling, to reduce heap consumption\n* Use Native Java images for containers\n\n## Testing \n* Test the app with containers - use Testcontainers\n    * Use Google emulators for Google managed services\n    * Use open-source test containers for Postgres, MySQL and SQLServer for relational databases running in CloudSQL \n\n## Build and Packaging\n* Use cloud-native buildpacks to build the container images\n* Minimize container images by using optimized container images\n\n## Observability\n\n\n## Operations and Resiliency\n\n\n## Caching \n\n## Database \n\n## Security\n* \n\n## Documentation","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddobrin%2Foptimize-serverless-google-cloud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fddobrin%2Foptimize-serverless-google-cloud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddobrin%2Foptimize-serverless-google-cloud/lists"}