Serverless Architecture
Functions and managed services that scale to zero and bill per use, with cold starts, limits, state handling and cost crossover points explained.
Intent
Build applications from managed services and functions where the provider handles servers, scaling and availability, and you pay per request or per unit of work. "Serverless" includes FaaS (Lambda, Azure Functions, Cloud Run functions), serverless containers (Cloud Run, Azure Container Apps, Fargate), and serverless data services (DynamoDB, Aurora Serverless, BigQuery, S3).
flowchart LR U[Client] --> APIGW[API Gateway] APIGW --> F1[Function: create order] F1 --> DDB[(DynamoDB)] DDB -- stream --> F2[Function: send confirmation] F2 --> SES[Email service] S3[(Object storage)] -- upload event --> F3[Function: thumbnail] SCHED[Scheduler] --> F4[Function: nightly report]
Characteristics and limits
| Aspect | Notes (version-sensitive) |
|---|---|
| Scaling | Per-request concurrency; account/region concurrency quotas apply |
| Cold starts | Latency on new instances; mitigated by provisioned concurrency, SnapStart (Java on Lambda), min instances (Cloud Run) |
| Duration | Lambda max 15 minutes; long work needs Step Functions or containers |
| State | Stateless functions; state in managed stores |
| Cost | Cheap at low/spiky load; can exceed containers at sustained high utilization |
Java example: AWS Lambda handler
public class CreateOrderHandler implements RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> {
private static final DynamoDbClient DDB = DynamoDbClient.create(); // reused across invocations
@Override
public APIGatewayV2HTTPResponse handleRequest(APIGatewayV2HTTPEvent event, Context ctx) {
var order = Json.parse(event.getBody(), CreateOrder.class);
DDB.putItem(r -> r.tableName("orders")
.item(Map.of("pk", AttributeValue.fromS("ORDER#" + order.id())))
.conditionExpression("attribute_not_exists(pk)")); // idempotent create
return APIGatewayV2HTTPResponse.builder().withStatusCode(201).build();
}
}Real-world use
Event processing (image/video processing, ETL triggers), APIs with spiky traffic, scheduled jobs, chatbots, IoT backends, internal tools. Many startups build entire products on serverless; large enterprises use it for glue and event processing.
Pros and cons
| Pros | Cons |
|---|---|
| No server management; built-in HA | Cold starts and execution limits |
| Scale to zero; pay per use | Vendor-specific APIs and tooling (lock-in) |
| Fast to build event-driven glue | Harder local testing and debugging |
| Fine-grained cost attribution | Cost at sustained high load; many small functions to govern |
When not to use
Long-running, latency-critical, steady high-throughput workloads; workloads needing specialized hardware or long-lived connections; teams without cloud-native observability.
Interview questions
How do you reduce Java cold starts on Lambda?
SnapStart (snapshot restore of initialized execution environments), smaller dependency footprints, avoiding heavy reflection frameworks at startup or using AOT/native images (GraalVM), provisioned concurrency for latency-critical paths, and keeping clients outside the handler for reuse.
When is serverless more expensive than containers?
When utilization is high and steady: per-request pricing exceeds the cost of reserved or right-sized containers. Model cost per million requests at expected load and compare.