Serverless Apps on Google Cloud: more dev, less ops
- 2. About Me: Joseph Lust
Engineer
■ Building the web for two decades
■ Cloud native since 2011
■ Currently building mabl ML cloud on GCP
■ Co-organizer GDG Cloud Boston Meetup
me@joelust.com
- 6. it’s all about opportunity cost
■ Provisioning takes time
■ Boilerplate takes time
■ You’re not running a hosting company
6
- 7. how developers deploy code
2014
2007 2013
VPS
Machine
Image
Docker
Containers
war File
AWS
Lambda
7
2000
Container Era
Serverless Era
Firebase
CF
Serverless functions
GCF
2016 2017
AF
- 9. other serverlessish GCP services
■ Serverless
▲ Scaling Clusters - Kubernetes Engine
▲ Kafka - Pub/Sub
▲ Mongo/EventSource - Firebase
▲ File Server - Cloud Storage
■ Not purely serverless, but close
▲ Tomcat/Jetty - App Engine
▲ Spark Streaming - Dataflow
9
Cloud
Pub/Sub
Cloud
Dataflow
Kubernetes
Engine
ML Engine
- 10. event driven all the way
■ Only run code on events
▲ Cloud Storage blob changes
▲ Pubsub message arrival
▲ HTTP endpoint requests
▲ DB/Auth/Conversion changes
■ No machines consuming 2% CPU all day
■ Very low latency operations
■ No more corn jobs!
10
- 13. gcf is the cheapest function provider
Vendor FREE req/mo req/$
Google Cloud Functions 2M 5M
Firebase Cloud Functions 2M 2.5M
AWS Lambda 1M 5M
Azure Functions 1M 5M
13
See full details in Google Spreadsheet calculator
* Also see execution time, bandwidth, and memory costs
- 14. gcf cheapest counting free monthly allowances
14
* Also see execution time, bandwidth, and memory costs
- 15. gcf differentiators
■ Guaranteed CPU per memory allotment
■ Easy to use UI and tools
■ Stackdriver Logging & Error Reporting
Source: GCP Pricing, FB Pricing, AWS Lambda Pricing, Azure Pricing
15
- 19. microservices solve many pains
■ Minimize code size
▲ Can scale servers, not developers’ minds
■ Minimize deploy speed/code
■ Minimize blast radius
■ Decoupling, Coherence, Singleness of Purpose
19
- 20. how do you decouple services and handle discovery
■ We Don’t Do Discovery
■ Event Driven Queues
▲ HTTP endpoints for external services
▲ Blob change events for files
▲ Message queue events for everything else
■ No Daemons
▲ Don’t waste time spinning
20
- 22. how do you handle changing microservice/models?
■ Versioned Payloads over the wire
▲ New service can handle older versions
■ Add Consumers, then Suppliers
■ Upgrade, don’t replace
■ Lazy read repair
▲ Pessimistic data upgrade and transform
▲ Upgrade path from old version
22
- 23. how do make slow deploys fast?
■ Multi-repo git layout
■ File change exclusion masks
■ Deploy functions in parallel
■ Harness CI & CD (e.g. Cloud Build)
23
- 24. how do you handle GCP outages?
■ Durable Pubsub subscriptions
■ No microservices on fast path
▲ Backend processing
▲ Keep the UI and lights on via Firebase
24
- 25. request costs round up
■ Requests billed per 100ms
■ Increments round up
■ Faster functions are not always cheaper
▲ 17ms → 100ms
▲ 101ms → 200ms
25
Run Time:
17ms
Billed Time: 100ms
Wasted
Time
- 26. minimizing your request costs
■ Batch multiple fast work units to fill the time (e.g. 100ms chunks)
26
Work Unit:
17ms
Billed Time: 100ms
Work Unit:
17ms
Work Unit:
17ms
Work Unit:
17ms
Work Unit:
17ms
Wasted
Time
- 27. minimizing your request costs
27
■ Use a middle man to shield file events from high memory loads
▲ Filter/light weight checks in middle man
▲ Send heavy lifting to larger function
■ Different inputs to same service have different resource needs
▲ e.g. 90% requests need 128MB ram, 10% need 512GB
▲ e.g. 80% requests complete in 30s, 20s take 2min
- 29. maximizing performance
■ Tune your CPU & Memory to the job
▲ Profile your microservices - CPU or memory bound?
■ More memory runs faster
▲ Greater CPU dedicated to larger memory allocation functions
▲ 128MB → 200 MHz
▲ 2048MB → 2.4GHz
▲ CPU/Memory costs increase linearly
■ Cache expensive objects between requests
▲ PubSub connections
▲ Firebase Admin instances
▲ Login tokens
29
- 31. track everything, from bits to cents
■ Metrics for every execution
▲ Every Byte of memory use
▲ Every Processor Cycle of runtime
▲ Every Byte of egress
▲ Every Invocation
■ Direct Billing to BigQuery export
▲ Easy to explore and query
▲ Easy to visualize with Data Studio
31
- 35. ■ scaling to zero no longer important
■ you have a constant load
▲ Dedicated instance
when to move from Serverless to Servers
35
$
Load (rps)
Cloud Function
Dedicated
n1-standard-1
- 36. ■ scaling to zero no longer important
■ you have dynamic load
▲ Kubernetes Deployment
▲ Knative function
▲ App Engine Service
when to move from Serverless to Servers
36
$
Load (rps)
Cloud FunctionKubernetes
Deployment
- 37. billing tips
■ Label functions on deploy
▲ Labels propagated to billing
▲ Can track Memory, CPU, Invocation, Ingress, Egress costs per function
■ Don’t forget the egress bandwidth
▲ Keep your data stores in the same region (Iowa)
▲ GCF only deployable to Iowa
37
gcloud beta functions deploy <your_fn>
--labels=”app_name=<your_fn>”
...
- 39. embrace serverless for
■ For low latency, event driven demands
■ For decoupled, cohesive, independent services
■ For painless deployment and auto-scaling
■ For reduced opex and oppex
39
- 41. gcp serverless resources
■ FB CF Examples Repo: github.com/firebase/functions-samples
■ GCF Examples: github.com/GoogleCloudPlatform/nodejs-docs-samples
■ Jason Polites (great) Examples: github.com/jasonpolites/gcf-recipes
■ I/O 2018 CF Presentations
▲ Get Started Building Serverless Applications: youtu.be/JenJQ6gc14U
▲ Serverless on Google Cloud: youtu.be/iPQUdb0kssE
▲ Serverless Compute on Google Cloud: youtu.be/Y1sRy0Q2qig
▲ CI/CD in a Serverless World: youtu.be/iyGHW4UQ_Ts
▲ Building and Deploying Microservices: youtu.be/oALEthV9z_U
41
@lustcoder