Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Going Serverless Effectively Using Cloud Run

Going Serverless Effectively Using Cloud Run

This deck is presented at Google DevFest Bandung 2019.

Aditya Satrya

December 07, 2019
Tweet

More Decks by Aditya Satrya

Other Decks in Technology

Transcript

  1. 5

  2. 6

  3. 7

  4. Serverless Compute Options 8 Serverless Functions Serverless Applications Serverless Containers

    Cloud Functions App Engine Cloud Run Deploy snippet of code Deploy app code Deploy Containers
  5. 10 Cloud Functions Good fit for: • Event-driven applications •

    “Glueing” systems together • Simple API Bad fit for: • Stateful systems • Large, complex APIs • Require a high level of control • Need custom runtimes or binaries
  6. 12 App Engine Good fit for: • Stateless applications •

    Rapidly developing CRUD-heavy applications • App composed of a few services • Deploying complex APIs Bad fit for: • Stateful systems • Slow startup time • Require protocol other than HTTP
  7. 13 Cloud Run Good fit for: • Stateless services that

    are easily containerized • Event-driven applications • Require custom dependencies Bad fit for: • Highly stateful systems • Require protocols other than HTTP
  8. 14 Cloud Function App Engine Cloud Run Deployment unit Code

    snippet App code Container Custom URL No Yes Yes Run any language No No Yes Background process No Yes No
  9. Disadvantages of Serverless 16 ▷ Testing and debugging become harder

    ▷ may cost more to run long-running processes ▷ Performance may be affected ▷ Vendor lock-in is a risk
  10. 24

  11. 27 400 clients are making 3 requests per second to

    a Cloud Run Concurrency = 1 Concurrency = 80
  12. Limitation of concurrency settings considerations 28 ▷ CPU and memory

    usage per request ▷ Application design: ◦ can handle multiple requests at the same time? ▷ Lower concurrency ~ lower scaling performance
  13. Autoscaling 29 Max simultaneous requests = concurrency per instance *

    max instances Limitation considerations: • cost control • compatibility with backing resources
  14. Container Runtime Contract 30 ▷ Stateless ▷ Listen for HTTP

    request on $PORT ▷ Startup times < 4 mins ▷ No CPU outside request scope
  15. Cloud Run: Use Cases 31 Public • Website • API

    endpoint • Mobile/IoT backend • Webhook Private • Microservices • Async tasks
  16. On Migrating Existing Service 33 Modify your code to: ▷

    Listen on the port defined by PORT environment variable ▷ Remove reliance on local filesystem for persistent storage ▷ Add a Dockerfile ▷ Send logs to stdout, stderr, or /var/log
  17. Writing Effective Service 34 ▷ Avoiding background activities ▷ Deleting

    temporary files ▷ Do not let service crash on errors
  18. Optimizing Performance 35 ▷ Minimize image size ◦ Use lean

    base image (exp: alpine, scratch) ◦ Multi stage build image ▷ Minimize entrypoint execution time ▷ Use dependency wisely ▷ Use lazy initialization for global variables
  19. Fine-Tuning Concurrency 36 ▷ Find the maximum stable concurrency level:

    ◦ Set expected_concurrency value ◦ Load test your system ◦ If poorly, reduce expected_concurrency ◦ If well, increase expected_concurrency ◦ Continue iterating until you find the maximum stable concurrency ▷ Also increase memory_limit when increasing expected_concurrency
  20. Demo 1: Code, Build, Deploy easily using Cloud Build and

    Cloud Run 38 ▷ Create Python Hello World application ▷ Create Dockerfile ▷ Build and push image using Cloud Build ▷ Deploy to Cloud Run ▷ Access your production-ready live app on provided domain
  21. Demo 2: Find stable concurrency level using Load Testing 39

    ▷ Set max instance = 1 ▷ Load test with 2500 concurrent users ▷ See number of users vs response time, it’s not stable ▷ Set max instance = 1000 ▷ Load test with 2500 concurrent users ▷ See number of users vs response time, it’s stable
  22. References 40 ▷ https://cloud.google.com/serverless-options/ ▷ https://cloud.google.com/hosting-options/ ▷ https://bravenewgeek.com/serverless-on-gcp/ ▷ https://cloud.google.com/appengine/docs

    ▷ https://cloud.google.com/functions/docs/ ▷ https://cloud.google.com/run/docs/ ▷ https://github.com/ahmetb/cloud-run-faq ▷ https://cloud.google.com/blog/products/gcp/time-to-hello-world-vms-vs-containers -vs-paas-vs-faas ▷ https://cloud.google.com/blog/products/gcp/choosing-the-right-compute-option-in- gcp-a-decision-tree ▷ https://docs.microsoft.com/en-us/azure/architecture/guide/technology-choices/com pute-decision-tree ▷ https://www.cloudflare.com/learning/serverless/why-use-serverless/ ▷ https://medium.com/@PaulDJohnston/when-not-to-use-serverless-jeff-6d054d0e7098 ▷ https://www.youtube.com/watch?v=xVuuvZkYiNM&t=2538s ▷ https://medium.com/@imrenagi/ekstraksi-informasi-e-ktp-dengan-google-cloud-funct ion-dan-cloud-vision-api-4655db21d084