Course map

Topic 184 / Core

Serverless architecture

Chapter 11.2 · Compute: VMs, Containers, Serverless

What and why

Even a container (→ 180) needs something keeping at least one instance running to answer requests instantly, which means paying for idle capacity between bursts. Serverless (Function-as-a-Service) removes that: the platform runs your code only in response to an event, scales the number of concurrent executions automatically — including to zero — and bills per invocation/duration rather than per running instance. Builds on: → 180 (Docker and containers).

Diagram

Container service Serverless / FaaS
Idle cost pays for the running instance zero — nothing runs between invocations
Scaling unit instance, thresholds you configure per-invocation, automatic
Startup fast if already running cold start if no warm instance exists
State between requests can hold it in memory assume none — state must live elsewhere
Execution time effectively unbounded platform-enforced maximum

How it works

  1. Code is deployed as a function bound to a trigger — an HTTP request, a message on a queue (→ 106), a schedule, a storage event.
  2. On a trigger, the platform allocates an execution environment, runs the function, and returns the result.
  3. If no warm environment exists, the first invocation pays a cold start — provisioning and initializing an environment before the function can run, adding tens to hundreds of milliseconds (or more, depending on runtime and package size) before useful work begins.
  4. The platform scales concurrent executions automatically with trigger volume, including down to zero when there's no traffic — the source of both the "zero idle cost" and "automatic scaling" promises, and why serverless suits spiky, event-driven workloads.
  5. Execution time and resource limits are capped by the platform — a function needing to run longer than that maximum, or hold significant in-memory state across invocations, is working against the model, not with it.
  6. Because any invocation might land in a fresh environment, all state that must persist between invocations belongs in an external store (a database, a cache, object storage) — the function itself must be treated as stateless (→ 91).

Practical example

(numbers illustrative). A nightly batch job that processes uploaded files runs for roughly 2 minutes once a night. Keeping a container instance warm 24 hours to serve that 2 minutes of work means paying for roughly 99.9% idle time. As a function triggered by the nightly schedule, cost is close to just those 2 minutes/day, at the price of a cold start adding perhaps a few hundred milliseconds to the job's start — irrelevant for a 2-minute batch job, but a real problem for a user-facing API with a tight p99 target (→ 100).

When to use it

  • Use serverless for spiky, event-driven, or infrequent workloads — glue code, scheduled jobs, webhook handlers — where idle cost would otherwise dominate.
  • Use serverless when automatic scale-to-zero and scale-up matter more than control over the exact runtime environment.
  • Avoid serverless for latency-critical request paths that can't tolerate occasional cold starts, workloads that run continuously anyway (no idle time to save), or anything needing long-running in-memory state or execution beyond the platform's time limit.
  • Failure scenarios: a function assuming an in-memory cache survives between invocations and silently losing it on a cold start; a latency-sensitive endpoint built as a function, passing load tests against warm instances, then missing its SLO (→ 203) in production because cold starts weren't load-tested; a long chain of functions calling functions becoming unobservable and expensive at the seams (→ 122 exists partly for this).
  • Simpler alternative: for a workload that runs continuously at steady volume, a small always-on container is often simpler to reason about, and sometimes cheaper, than a function paying per-invocation overhead at high constant volume — serverless earns its keep on spikiness and idle time, not as a default for everything.

Practice

(decision kind, 15–20 min, paper exercise). For four workloads (a nightly report job, a login endpoint with a 100 ms p99 target, a webhook receiver called a few times a day, a video-transcoding pipeline running minutes per job), decide serverless vs. a container service for each, justifying with idle cost, cold-start tolerance, and execution-time limits specifically. Done when every "avoid serverless" answer names the specific constraint it violates.

Key takeaway

Serverless trades control over the runtime for zero idle cost and automatic scale-to-zero, at the cost of cold starts and a hard requirement that functions stay stateless. Done when you can: pick serverless vs. a container service for a workload using its idle-time and latency profile, not habit. Used next in → 186 (CI/CD deploys functions the same pipeline deploys containers), → 191 (the FaaS name varies most by provider).

References

None — this topic makes no external claims.

Source directory

Search the guide