Mhonn YbañezSRE, DevOps, AI Engineer
 Today I Learned
Read the full page

What are serverless functions, and is serverless actually popular?

Posted
Tags
serverless, aws, architecture

A serverless function is code that runs only when an event arrives, on servers you never size, billed by the request and the millisecond. It's widely used for glue code, APIs, and spiky work. Teams move off it when the load gets steady and heavy, the same cost tradeoff as serverless databases.

In my serverless databases post, I compared serverless database compute to serverless functions, and realized I'd only ever touched functions as one box in my Kubernetes post. I knew the name, not how they actually run. I also wanted to check a feeling I had: is serverless actually popular in production, or mostly a demo thing?

The question I had

What exactly is a serverless function, and is serverless really used in production?

What I found

A handler and a trigger

A serverless function (also called FaaS, functions as a service) is two things:

  • A handler: one function in your code that takes an event and returns a result.
  • A trigger: what calls it. An HTTP request, a message on a queue, a file landing in a bucket, a row changing in a database, a schedule.
export async function handler(event) {
  const order = JSON.parse(event.body);
  // validate, save, send a receipt...
  return { statusCode: 201, body: JSON.stringify({ id: order.id }) };
}

No server, no port, no process to keep running. You upload the code, point a trigger at it, and the provider runs it when something happens. Examples: AWS Lambda, Google Cloud Functions, Azure Functions, Cloudflare Workers.

What happens on a request

  1. Cold start. No environment is ready, so the provider creates one: a small sandbox, your runtime, your code. Then your setup code runs (imports, opening clients). This is the slow part.
  2. Run the handler. The event goes in, the result comes out.
  3. Stay warm. The environment isn't thrown away right away. The next request can reuse it and skip straight to step 2.
  4. Scale out. In Lambda, one environment handles one request at a time. Ten requests at once means ten environments. A thousand means a thousand.
  5. Go away. After a while with no requests, the environments are shut down, and the next request pays the cold start again.

"Stateless" doesn't mean everything gets wiped between calls. A warm environment keeps its globals and temp files. It means you can't count on that, because the next request might land on a brand new one.

Limits worth knowing

Each provider has hard limits, and they change over time. For Lambda at the time of writing:

  • Run time: 15 minutes max per call.
  • Memory: up to about 10 GB, and CPU scales with the memory you pick.
  • Payload: around 6 MB for a request or response when called directly.

Anything longer or heavier doesn't fit, which is a big hint about what functions are for.

How you pay

Per request, plus compute time (the memory you picked times how long it ran). When nothing happens, you pay nothing. That's the whole pitch, and the same idea as scale-to-zero in serverless databases.

Fixing cold starts

  • Keep some warm: Lambda's provisioned concurrency keeps a set number of environments ready. It works, but you pay for them while idle, which undoes part of the pay-per-use idea.
  • Make start-up smaller: fewer dependencies, smaller packages, less work in setup code.
  • Snapshot the start-up: Lambda SnapStart (starting with Java, now some other runtimes) runs the setup once, saves a snapshot, and resumes from it.

Like with databases, cold starts only hit some requests, so they show up in the p99, not the average.

The database connection problem

This is where functions and databases bump into each other. A traditional Postgres expects a few long-lived connections. A thousand function environments, each opening its own, hit the database's shared connection ceiling fast, the same ceiling from my connection pool post.

The usual fixes:

  • A connection proxy in between, like Amazon RDS Proxy, which pools the connections.
  • HTTP-based database drivers, offered by some serverless databases, so a function doesn't hold a connection open at all.
  • Databases that don't use connections that way, like DynamoDB, which is called over HTTP.

Functions vs serverless containers

When the work doesn't fit a function's limits, there's a middle ground: serverless containers like Google Cloud Run, AWS Fargate, and AWS App Runner. You bring a container image instead of a handler, it can run longer, and some (like Cloud Run) can handle many requests per instance. You still don't manage servers.

So is serverless popular?

Yes, but for certain jobs more than others. Industry surveys report that a large share of organizations on AWS use Lambda. Where it shines:

  • Glue between services: a file lands, resize it. A queue gets a message, process it. A row changes, notify someone. Queues and functions fit well together, the "work that can wait" side from my message broker post.
  • APIs with uneven traffic: quiet at night, busy at lunch.
  • Scheduled jobs: a nightly cleanup doesn't need a server running all day.
  • New products: no idea what the load will be yet.

The well-known counterexample is from Amazon itself. In 2023, a Prime Video team wrote that they moved one audio and video monitoring service from a pipeline of serverless pieces (Step Functions and Lambda) to a single containerized service, and cut that service's cost by over 90%. It got passed around as "even Amazon is leaving serverless", but it was one service with steady, very heavy load, where calling many small pieces per video frame added cost. The lesson was fit, not that serverless is bad.

When it fits, when it doesn't

FitsFits less
Spiky or unpredictable trafficSteady, heavy load around the clock
Short, event-driven workLong-running jobs past the time limit
Small teams that want no servers to runStrict latency where cold starts can't be tolerated
Glue between managed servicesChatty workloads that call many small pieces per request

Takeaway

A serverless function is a handler that only exists while there's work, and you pay by the request. It's popular for glue, APIs, and spiky work, and it stops making sense when the load is steady and heavy. That's the same tradeoff I found with serverless databases: you're paying a premium to never size a server, and it's worth it until the traffic is predictable enough that sizing it yourself is cheaper.