logo

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

  1. A request arrives (HTTP, event, schedule)
  2. The platform provisions an execution environment (or reuses a warm one)
  3. Your function code runs
  4. The response is returned
  5. 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:

PlatformDefault TimeoutMax Timeout
Vercel (Hobby)10s10s
Vercel (Pro)60s300s
AWS Lambda3s900s
AWS API Gateway + Lambda30s30s
Google Cloud Functions60s540s

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

AdvantageLimitation
Zero infrastructure managementTimeout limits
Auto-scaling to zeroCold start latency
Pay-per-execution pricingStateless between invocations
Scales up automaticallyVendor-specific constraints