What is Serverless
Serverless is an architecture where you delegate server management to cloud providers and focus only on code execution. It doesn’t mean “no servers” but rather “you don’t need to think about servers.”
Benefits: No infrastructure management, automatic scaling, pay only for what you use
Types of Serverless
FaaS (Function as a Service)
Execute functions via event triggers.
// AWS Lambda example
export const handler = async (event) => {
const name = event.queryStringParameters?.name || 'World';
return {
statusCode: 200,
body: JSON.stringify({ message: `Hello, ${name}!` })
};
};
BaaS (Backend as a Service)
Provides backend functionality like authentication, databases, and storage as services.
| BaaS Example | Category |
|---|---|
| Firebase Authentication | Auth |
| Firestore | Database |
| AWS S3 | Storage |
| Algolia | Search |
How FaaS Works
flowchart LR
Event --> Startup["Container startup"]
Startup --> Exec["Function execution"]
Exec --> Response
Response --> Stop
Parallel execution: Functions A, B, C can run simultaneously
Scaling: 0 → 1 → 100 → 1000 → 0 (automatic)
Event Sources
| Event | Trigger | Function |
|---|---|---|
| HTTP Request | API Gateway | Lambda |
| File Upload | S3 Event | Lambda |
| Schedule | CloudWatch | Lambda |
| Message | SQS/SNS | Lambda |
| Database Change | DynamoDB | Lambda |
Major FaaS Platforms
| Service | Provider | Features |
|---|---|---|
| AWS Lambda | Amazon | Market leader, rich integrations |
| Cloud Functions | GCP integration, Firebase integrated | |
| Azure Functions | Microsoft | .NET affinity |
| Cloudflare Workers | Cloudflare | Edge execution, low latency |
| Vercel Functions | Vercel | Frontend integration |
Cold Start
When a function runs for the first time, container startup takes time.
| Start Type | Flow | Notes |
|---|---|---|
| Cold start | Request → Container startup (100-1000ms) → Function execution → Response | Initial latency |
| Warm start | Request → Function execution → Response | Container reuse |
| After idle | Container stops → Next request is cold start |
Cold Start Mitigation
| Strategy | Description |
|---|---|
| Provisioned Concurrency | Pre-start containers |
| Regular warmup | Keep containers warm with periodic execution |
| Lightweight runtime | Node.js, Python (faster than Java) |
| Dependency optimization | Reduce package size |
// Initialize connections outside handler (will be reused)
const db = new Database();
export const handler = async (event) => {
// db is reused
return await db.query('SELECT * FROM users');
};
Pricing Model
Pay-per-use Example (AWS Lambda)
Cost = Request count × Execution time × Memory
| Metric | Value |
|---|---|
| Requests | 1 million/month |
| Average execution time | 200ms |
| Memory | 512MB |
| Cost | ≈ $0.20 + $3.33 = $3.53/month |
Free tier: 1M requests, 400,000 GB-seconds/month
Comparison with Traditional Servers
| Model | Cost Calculation |
|---|---|
| Traditional Server | 24 hours × 30 days = 720 hours running → Always charged (even when idle) |
| Serverless | Charged only for actual execution time → No charge when idle |
Use Cases
Good Fit
- ✓ Event-driven processing (file processing, webhooks)
- ✓ Irregular batch processing
- ✓ API backends (especially low-medium traffic)
- ✓ Parts of microservices
- ✓ Prototyping, MVPs
Not a Good Fit
- ✗ Consistently high traffic (cost inefficient)
- ✗ Long-running processes (timeout limits)
- ✗ Ultra-low latency required (cold starts)
- ✗ Stateful processing
- ✗ WebSocket (partial support)
Limitations
| Limit | AWS Lambda | Cloud Functions |
|---|---|---|
| Max execution time | 15 min | 9 min (HTTP), 60 min (event) |
| Memory | 128MB-10GB | 128MB-32GB |
| Payload size | 6MB (sync), 256KB (async) | 10MB |
| Concurrent executions | 1000 (default, can increase) | Limited |
Implementation Patterns
API Gateway + Lambda
flowchart LR
Client --> Gateway["API Gateway<br/>(Auth, rate limiting, caching)"]
Gateway --> Lambda
Lambda --> DynamoDB
Event Processing Pipeline
flowchart TB
S3_1["S3 (image upload)"] --> Lambda["Lambda<br/>(thumbnail generation)"]
Lambda --> S3_2["S3 (save thumbnail)"]
S3_2 --> SNS["SNS (notification)"]
Scheduled Execution
// serverless.yml
functions:
dailyReport:
handler: handler.generateReport
events:
- schedule: cron(0 9 * * ? *) // Daily at 9am
Frameworks
| Framework | Features |
|---|---|
| Serverless Framework | Multi-cloud support |
| AWS SAM | AWS official, CloudFormation integration |
| SST | TypeScript, local development |
| Terraform | Unified IaC management |
Summary
Serverless is an architecture that reduces infrastructure management burden and lets you focus on development. It’s particularly suited for event-driven processing and irregular workloads. However, there are considerations like cold starts and execution time limits. It’s important to choose between serverless and traditional servers based on your use case.
← Back to list