Serverless Function
A serverless function is a unit of code deployed to a cloud platform that executes in response to events — HTTP requests, database changes, or scheduled triggers. The platform handles provisioning, scaling, and infrastructure. You pay only for actual execution time.
How Serverless Functions Work
- A request arrives (HTTP, event, schedule)
- The platform provisions an execution environment (or reuses a warm one)
- Your function code runs
- The response is returned
- The environment may be kept warm for subsequent requests or shut down
Popular platforms: Vercel, AWS Lambda, Google Cloud Functions, Cloudflare Workers, Netlify Functions.
The Timeout Problem
Every serverless platform enforces execution timeout limits:
| Platform | Default Timeout | Max Timeout |
|---|---|---|
| Vercel (Hobby) | 10s | 10s |
| Vercel (Pro) | 60s | 300s |
| AWS Lambda | 3s | 900s |
| AWS API Gateway + Lambda | 30s | 30s |
| Google Cloud Functions | 60s | 540s |
If your function exceeds the timeout, the platform kills it. Any in-progress work is lost.
Serverless Functions and Task Queues
Task queues solve the fundamental serverless timeout constraint. Instead of doing slow work inside the function, create a task and respond right away:
// Function runs for ~50ms, not 5 minutes
export async function POST(req) {
const task = await aq.tasks.create({
callbackUrl: 'https://slow-api.example.com/process',
payload: await req.json(),
webhookUrl: `${process.env.APP_URL}/api/on-complete`,
retries: 3,
});
return Response.json({ taskId: task.id, status: 'processing' });
}
The function stays well within timeout limits. AsyncQueue handles the slow work outside the serverless execution environment, free from timeout constraints.
Trade-offs
| Advantage | Limitation |
|---|---|
| Zero infrastructure management | Timeout limits |
| Auto-scaling to zero | Cold start latency |
| Pay-per-execution pricing | Stateless between invocations |
| Scales up automatically | Vendor-specific constraints |