Skip to main content
Cloud basicsLesson 12 of 17

How a Serverless Function Runs

You upload only code and say which event should run it. The platform finds a place to run it, starts more copies when events pile up, and drops them when nothing happens. Follow one function from event → execution environment → result, with AWS Lambda as the example. Google Cloud Run, Azure Functions and Knative on Kubernetes work along the same lines with different limits.

EventHTTP · synchronous
Start—
Concurrency0
Result—
Execution environmentsClientHTTPS requestAPI gatewayHTTP eventQueueMessage eventBucketUpload eventLambda serviceRoutes · scalesEnv 1Runtime + codeEnv 2Runtime + codeEnv 3Runtime + codeCodezip or imageDBState lives hereLogsREPORT linesPOST /ordersInvoke
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Events from HTTP, queues and uploads all start the same function
  • What a cold start does and why warm reuse is faster
  • One request per environment, so concurrency means more environments
  • Concurrency limits, 429 throttling and the timeout ceiling
  • Retries deliver the same event twice, so handlers must be idempotent
  • Pay per request and duration, and when a function is the wrong tool

VM, container on Kubernetes, or function?​

Virtual machineContainer on KubernetesServerless function
You manageOS, runtime, app, scalingImage, manifests, cluster (or a managed one)Code and settings only
Unit of scalingWhole VM (minutes)Pod (seconds)Execution environment per request (milliseconds to seconds)
Idle costPaid while runningNodes paid while runningNear zero (scale to zero)
Run lengthUnlimitedUnlimitedCapped (Lambda: 30 min)
StateLocal disk persistsVolumes (PV) if neededNone between calls, use external stores
Good fitLegacy apps, special OS needsSteady services, many microservicesBursty events, glue code, webhooks, file processing

Knative brings the function model (scale to zero, scaling by concurrency) onto a Kubernetes cluster you run yourself.

Try it yourself​

aws lambda invoke --function-name demo --cli-binary-format raw-in-base64-out \
--payload '{"id": 1}' out.json && cat out.json # Synchronous call
aws lambda get-function-configuration --function-name demo \
--query '[Runtime,MemorySize,Timeout]' # Settings that drive cost and limits
aws logs tail /aws/lambda/demo --since 10m # REPORT lines: Duration, Billed Duration, Init Duration
aws lambda get-account-settings --query 'AccountLimit.ConcurrentExecutions' # Account concurrency limit
aws lambda put-function-concurrency --function-name demo \
--reserved-concurrent-executions 50 # Reserve and cap concurrency for one function
aws lambda put-provisioned-concurrency-config --function-name demo \
--qualifier live --provisioned-concurrent-executions 5 # Keep 5 environments initialized (billed)

Check yourself

  1. Three requests reach a Lambda function at the same moment, and only one warm environment exists. What happens by default?
  2. An upload handler saved an order and then crashed. Lambda retried the event and the customer was charged twice. What is the right fix?
  3. A function keeps login sessions in a global variable, and users get logged out at random. Why?

Further reading: What is AWS Lambda? · Lambda execution environment lifecycle · Lambda concurrency · Asynchronous invocation · Cloud Run concurrency · Knative Serving · Who Owns What (IaaS · PaaS · SaaS) · Until a Model First Starts (Cold Start)