A serverless function is a small, single-purpose piece of backend code deployed to a provider that runs it on demand, scales it automatically, and charges only for actual execution time - no idle server sitting around waiting for the next request.
// A basic serverless function
exports.handler = async (event) => {
const { email } = JSON.parse(event.body);
await sendWelcomeEmail(email);
return { statusCode: 200, body: JSON.stringify({ ok: true }) };
};
Where it fits well
Form handlers, webhooks, image processing, scheduled jobs - short-lived, event-triggered tasks are the sweet spot. A function that needs to hold a persistent connection or run for a long time is a worse fit for the model.
Pairs naturally with JAMstack
A statically hosted front end with serverless functions handling the dynamic pieces has become a genuinely common pattern on our smaller projects this year - no server to provision, patch, or pay for around the clock.
Paying for execution, not idle capacity
exports.handler = async (event) => {
const { name } = JSON.parse(event.body);
return { statusCode: 200, body: JSON.stringify({ message: `Hi ${name}` }) };
};
A traditional server runs - and gets billed - 24/7 whether it's handling traffic or not. A serverless function spins up on request, runs, and shuts down, which for spiky or low-traffic workloads can be dramatically cheaper and requires zero capacity planning.
Cold starts are the tradeoff worth understanding up front
A function that hasn't run recently needs to initialize before handling its first request, adding latency that matters for user-facing endpoints and matters far less for background jobs. Keeping functions small, minimizing heavy imports, and choosing a runtime with faster cold-start characteristics (or accepting a "keep-warm" ping for latency-sensitive endpoints) are the standard mitigations.