What it is
Serverless computing, in its function-as-a-service form, lets you deploy a function and have the platform run it in response to events: HTTP requests through an API gateway, messages on a queue, files landing in a bucket, schedules, database change streams. The platform creates execution environments (instances) on demand, routes each event to one, scales the number of instances with load (down to zero when idle), and bills per invocation and compute time. AWS Lambda, Google Cloud Run functions and Azure Functions are well-known examples; container platforms with per-request scaling share many of the same properties.
Why it exists
Running servers means choosing instance sizes, keeping enough capacity for peaks, patching operating systems and paying for idle machines. Many workloads are spiky or small: a webhook called a few hundred times a day, image processing when users upload, a nightly job. Functions remove the fleet: no capacity to plan for normal operation, no idle cost, and event integrations that make glue code short.
How it works
Per-request scaling. In the common model each instance handles one request at a time (some platforms allow several). The number of instances running at once is therefore the concurrency, and Little's law gives it: concurrency = arrival rate × duration. 400 requests per second at 60 ms need about 24 instances; if a dependency makes each invocation four times slower, the same traffic needs about 100.
Instance lifecycle. A new instance must download the code, start the runtime and run your initialisation code before handling its first request: the cold start. Afterwards the instance stays warm for a while and handles further requests without that cost; idle instances are eventually reclaimed. Cold starts range from tens of milliseconds for small packages on fast runtimes to seconds for large packages, heavy frameworks or functions attached to private networks with slow setup. Mitigations: smaller deployment packages, lazy initialisation, faster runtimes, snapshot-based start-up where offered, and paid pre-warmed capacity (provisioned concurrency, minimum instances).
Limits. Platforms cap concurrency per account and region, and let you reserve or cap concurrency per function. Invocations beyond the cap are throttled: synchronous callers get an error (typically HTTP 429), asynchronous event sources retry later. There are also limits on duration, memory, payload size and burst scaling rate.
State. Instances are disposable and do not share memory. Anything that must survive goes to a database, cache, queue or object store. Instances may be reused, so connections opened during initialisation are kept between invocations.
Downstream systems. Each instance is a separate client. With a relational database, connections follow instance count, and instances that are waiting still count. A burst that a function platform absorbs easily can exceed the database's connection limit. Connection poolers or managed database proxies hold a small set of real connections and lend them to callers per transaction; serverless-oriented databases and HTTP data APIs avoid persistent connections altogether.
When to use it
- Event-driven processing: storage events, queue consumers, change streams, webhooks.
- Spiky or low and unpredictable traffic, where idle servers would cost more than invocations.
- Scheduled jobs and lightweight automation.
- Prototypes and small APIs where operations effort matters more than per-request cost.
When not to
- Steady, high request rates: always-on servers or containers are often cheaper and more predictable.
- Latency-critical paths where cold starts in the tail are unacceptable and pre-warming is too expensive.
- Long-running work beyond the platform's maximum duration (transcoding, large batch jobs), unless split into steps.
- Workloads that need local state, large in-memory caches or long-lived connections such as WebSockets held by the compute itself.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Functions instead of servers | No capacity planning, scale to zero, managed patching | Cold starts, platform limits, per-request pricing |
| Pre-warmed capacity | No cold starts for that many instances | Paying for idle capacity again |
| Per-function concurrency cap | Protects downstream systems | Throttled requests during spikes |
| Connection pooler / database proxy | Connections follow query concurrency | Another component and hop; transaction-level pooling limits some session features |
| Asynchronous invocation via a queue | Smooths bursts, retries built in | Eventual completion, idempotency required |
Alternatives
- Containers on a managed platform with autoscaling (including per-request scaling to zero on some platforms): more control over runtime and concurrency per instance.
- Virtual machines or Kubernetes: best for steady load and specialised needs, at the cost of operating the fleet.
- Queue + worker pool: bounded, predictable concurrency for background work.
Failure modes
- Downstream exhaustion: database connections, third-party rate limits or a small service overwhelmed by a function that scaled out.
- Throttling cascades: one function consuming the account's concurrency starves unrelated functions; reserve concurrency for critical ones.
- Retry storms and duplicates: asynchronous triggers retry failed invocations; without idempotency the same event is processed twice.
- Cold-start spikes after deploys or quiet periods.
- Timeouts at the limit: work that usually fits in the maximum duration fails when a dependency slows.
- Runaway cost: a loop of functions triggering each other or a flood of events bills per invocation.
In production
Measure duration, concurrency, throttles, errors and cold-start rate per function. Set concurrency caps that protect downstream systems and reserve capacity for critical functions. Keep handlers stateless and idempotent, initialise expensive clients outside the handler, and use a pooler, proxy or HTTP-based data access for relational databases. Put queues in front of bursty background work. Configure timeouts shorter than the caller's, and dead-letter destinations for failed asynchronous events. Watch cost per request as traffic grows.
Common misconceptions
- "Serverless means infinite scale." Concurrency limits, burst rates and everything downstream still apply.
- "There are no servers." There are; you do not manage them, and their start-up time shows up as cold starts.
- "Serverless is always cheaper." For idle and spiky workloads, usually; for steady high load, often not.
- "Throttling only happens when traffic grows." A slower dependency raises concurrency at constant traffic.
Interview angle
Use functions for event-driven and spiky parts of a design (upload processing, webhooks, scheduled jobs) and justify it with cost and operations. Show you know the limits: estimate concurrency with rate × duration, discuss cold starts and pre-warming, concurrency caps and throttling, idempotency for retried events, and how you keep a scaled-out function fleet from overwhelming a relational database (pooler, proxy, queue, or a different datastore).
Model assumptions
The simulation is a simplified model, not a benchmark. The function is modelled as a server whose worker count is its concurrency limit (1,000 unless a step lowers it) with no queue, so an invocation over the limit is rejected at once (standing in for HTTP 429); each worker is one instance handling one request at a time. A warm invocation takes 60 ms (lognormal, cv 0.3). Cold starts are approximated by making every invocation take 700 ms for the first two seconds of the step; in reality only each new instance's first request pays. A slow dependency is modelled by making invocations four times slower. The database accepts 40 concurrent connections; waiting more than 150 ms for one fails the request. Without a pooler each invocation holds a connection for 55 ms of its 60 ms run; with a pooler it borrows one only for 15 ms of queries. Instances that are reused but idle, which in reality also hold connections, are not modelled, so real connection counts are higher. Arrivals are random (Poisson). One-way latencies are fixed: users to the gateway 20 ms, 1 ms inside the cloud. Numbers show behaviour and orders of magnitude only.