No Servers? No, Just Other People's Servers.
Serverless: You write code (Function). Cloud Provider manages infrastructure (OS, patching, scaling).
Key Traits:
- Event-Driven: Code sleeps until triggered (HTTP, S3 upload, Queue message).
- Ephemeral execution: Local state may survive a warm invocation but is not durable; put required state in an external store.
- Scale to zero: On-demand compute can fall to no active invocations. Storage, provisioned capacity and other services can still incur charges.
Architecture Patterns
1. Web API (API Gateway + Lambda)
Replaces EC2 + Nginx.
2. Async Processing (S3 + Lambda)
Image resizing pipeline.
- User uploads
photo.jpgto S3 Bucketraw-images. - S3 sends event
ObjectCreatedto Lambda. - Lambda resizes image and saves to
processed-images.
3. Fan-Out (SNS + multiple Lambdas)
- User registers.
- Publish "UserCreated" to SNS Topic.
- Lambda A (Email Service): Sends Welcome Email.
- Lambda B (Analytics): Updates Dashboard.
The Cold Start Problem ❄️
The biggest drawback.
- Request arrives: AWS finds no running container.
- Download Code: Pulls your zip file (50MB) from S3.
- Start Container: Spins up Firecracker MicroVM.
- Init Runtime: Starts Python/Node/Java process.
- Execute Handler: Runs your code.
Latency: Depends on package size, runtime, initialization and platform configuration. Measure cold and warm invocation distributions separately.
Mitigation:
- Warming pings: May reuse an environment but do not guarantee capacity or eliminate cold starts.
- Provisioned Concurrency: Pay to keep instances warm.
- Micro-Frameworks: Don't use heavy frameworks (Spring/Django). Use lightweight ones (Flask/Express/Go).
Limitations
| Feature | Serverless (Lambda) | Containers (Fargate/K8s) | Virtual Machines (EC2) |
|---|---|---|---|
| Max Runtime | 15 minutes (Hard limit) | Unlimited | Unlimited |
| Disk Space | Configurable ephemeral storage; check current quotas | Depends on attached storage | Depends on attached storage and quotas |
| Connection Limits | Massive concurrency kills DBs (Connection Pooling needed) | Controlled scaling | Controlled scaling |
| Cost | Depends on duration, memory and invocations | Depends on provisioned capacity and utilization | Depends on instance/storage commitments and utilization |
Code Example: AWS Lambda (Python)
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Users')
def lambda_handler(event, context):
"""
Triggered by API Gateway.
event: { "body": "{\"name\": \"Alice\"}" }
"""
# Avoid logging complete request bodies or credentials.
try:
# Parse Input
body = json.loads(event.get('body') or '{}')
if not isinstance(body, dict):
return {"statusCode": 400, "body": "Expected JSON object"}
name = body.get('name')
if not isinstance(name, str) or not name.strip():
return {"statusCode": 400, "body": "Missing name"}
# Business Logic
user_id = save_user(name)
return {
"statusCode": 200,
"body": json.dumps({"message": "Success", "id": user_id})
}
except (json.JSONDecodeError, TypeError):
return {"statusCode": 400, "body": "Invalid JSON"}
except Exception as e:
print(f"Storage error: {type(e).__name__}")
return {"statusCode": 500, "body": "Internal Error"}
def save_user(name):
item = {"pk": name, "status": "active"}
table.put_item(Item=item)
return name
Interview Tips 💡
- "When NOT to use Serverless?" — Long-running tasks (>15m), WebSocket servers (need stateful connections), Heavy GPU tasks (Training), High-frequency trading (latency variance).
- "Idempotency" — Retry behavior depends on the event source and invocation mode. Duplicate events are possible; use stable idempotency keys and atomic deduplication, not an unprotected check-then-write.
- "Vendor Lock-in" — Moving Lambda logic to Google Cloud Functions requires rewriting infrastructure code (Terraform helps, but logic is tied to SDKs).
Worked decision: a burst that exhausts the database
Assume 500 function instances each open ten database connections: the application can attempt 5,000 connections even though individual functions look healthy. Set concurrency against the downstream budget, use an appropriate proxy or pool, and queue work that can wait. Compare the cost of provisioned concurrency with the latency target rather than sending warming requests and assuming the problem is solved.
The Python fragment above requires a Users DynamoDB table and IAM permissions. Its name-keyed overwrite is a storage demonstration, not user registration or a complete idempotent API. AWS documents duplicate-event handling in its Lambda application design guidance.
Related Concepts
- Microservices
- Distributed Tracing (X-Ray)
- Message Queues (SQS)
Keep reading
Caching Strategies
A breakdown of where to place your cache and how to keep it in sync with your database.
Database Sharding
How to split a massive database across multiple servers. Horizontal scaling strategies, challenges (Joins, ACID), and real-world algorithms used by Instagram, Vitess, and CockroachDB.
Event Sourcing & CQRS
Comprehensive guide to Event Sourcing and Command Query Responsibility Segregation (CQRS) patterns, covering immutable event logs, state reconstruction, read/write separation, and real-world implementations in banking, e-commerce, and audit systems.
File Synchronization: Versions, Conflicts and Recovery
A worked file-sync design: immutable blocks, version checks, offline conflicts, and safe cleanup. Includes a clearly scoped Dropbox engineering reference.
Horizontal Scaling (Scaling Out)
The definitive guide to adding more servers to your infrastructure pool and testing the bottlenecks that limit growth.
Microservices Architecture
An architectural style that structures an application as a collection of loosely coupled, independently deployable services.