Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Serverless and Edge Functions

Serverless Functions: The Basics

Learn what serverless functions are, how they start and stop, why they must be stateless, what cold starts are, and how requests flow through functions on Vercel and Netlify.

Static files cannot send an email, check a password or read a database. When you need real code, both platforms offer serverless functions: small pieces of backend code that you deploy along with your site. You do not run a server. The platform starts your function when a request arrives and shuts it down when it is idle.

This lesson explains the model: how functions run, what "stateless" means, what a cold start is, and what limits to expect. The next lessons write real functions for each platform.

What "serverless" really means

There are still servers, but you do not provision, patch or scale them. You supply a handler (a function that takes a request and returns a response) and the platform takes care of everything else.

Life of a function call

Request A visitor calls /api/hello.
Start An instance is started if none is warm.
Run Your handler returns a response.
Idle The instance is paused, then removed.

You pay for work done, not idle time.

1 / 2

The handler contract

Both platforms use web standards. A handler receives a Request object (the same class browsers use for fetch) and returns a Response. If you know fetch, you already know the data types.

JavaScript
export default async (request) => {
  const name = new URL(request.url).searchParams.get("name") ?? "world";
  return Response.json({ message: `Hello, ${name}!` });
};

Because Request, Response and URL are available in current Node.js, you can run such a handler on your own computer with no platform at all. The stand-in below does exactly that: it calls the handler directly and prints the response.

Node.js Calling a handler directly

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Stateless by design

The platform may run many copies of your function at once, and it may throw an instance away at any time. So you cannot rely on memory between requests. A variable at module level might survive for a while on a warm instance, but it is not shared across instances and can vanish.

Keep state somewhere durable: a database, a key-value store, object storage (see the Storage and Data chapter). Module-level variables are fine for caching things that are safe to lose, such as a database connection you reuse or a parsed configuration.

Node.js Why in-memory counters break (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Cold starts and warm instances

The first request after a quiet period may need to create a new instance: load your code, start the runtime, and run top-level code. This delay is a cold start. Later requests reuse the warm instance and are faster. Cold starts are usually small for a few lines of JavaScript, but grow with large dependencies and heavy top-level work.

How to keep them small:

  • Import only what you use; avoid giant libraries.
  • Do slow setup lazily, only when needed, and reuse it across invocations.
  • Choose a region near your data (database and function should be close; both platforms let you pick).
Node.js Cold and warm timing (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Limits to plan for

Every platform restricts functions, and the numbers change by plan and over time, so look them up before relying on them. Typical categories:

Limit What it means for you
Execution time A request must finish within a maximum duration; long jobs need background or queued processing
Memory Large in-memory work can fail; process in chunks
Request and response size Large uploads should go straight to storage using signed URLs
Bundle size Large dependencies slow cold starts and may exceed the limit
Concurrency Many simultaneous requests create many instances; downstream databases need connection limits or pooling
Filesystem Read-only except a temporary directory; do not write files expecting them to persist
Outbound only You make outgoing requests; you cannot listen on arbitrary ports

Security basics

  • Validate input. Never trust the request body or query string.
  • Keep secrets in environment variables, never in code (see Environment Variables).
  • Check authentication inside the function: a function URL is public unless you protect it.
  • Return the right status codes (400 for bad input, 401/403 for auth failures, 500 for your bugs) and avoid leaking error details.
Node.js Validating a request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does it mean that serverless functions are stateless?

    Instances can be created or discarded at any time, so state must be stored outside them.
  2. 2. What is a cold start?

    The first request after idle time may need to initialize an instance, which adds delay.
  3. 3. Which types does a modern handler use for input and output?

    Both platforms support Request/Response, the same types used by fetch.
  4. 4. How should a large file upload be handled?

    Direct-to-storage uploads avoid request size and time limits.

Summary

  • Serverless functions are request handlers the platform starts, runs and stops for you.
  • Handlers use web-standard Request and Response, so you can test them locally.
  • Instances are stateless: keep durable data in a database or storage.
  • Cold starts add latency on the first request; keep bundles small and setup lazy.
  • Respect limits (time, memory, size) and always validate input and protect secrets.

Next: Netlify Functions