# Serverless and Functions as a Service

> Run backend code as on-demand functions and managed services, paying per request instead of per server. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/serverless
Last updated: 2026-11-28

Run backend code as on-demand functions and managed services, paying per request instead of per server.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Where Backends Run: Hosting Options](https://www.internhack.in/roadmaps/tracks/backend/hosting-basics), [Connection Pooling](https://www.internhack.in/roadmaps/tracks/backend/connection-pooling)

**Serverless** means you deploy code or use services without managing servers or capacity: the platform runs your code when events arrive and bills per invocation. **Functions as a Service** (AWS Lambda, Google Cloud Run functions, Azure Functions, Cloudflare Workers, Vercel functions) is the most visible part; managed databases, queues and storage complete the picture.

### How it works

- A function is triggered by an **event**: an HTTP request through an API gateway, a new file in a bucket, a message on a queue, a cron schedule.
- The platform starts an instance, runs your handler, and may reuse the warm instance for later events or scale out to thousands in parallel.
- You pay for execution time and requests; idle costs nothing.

### Strengths

Spiky or low traffic, event-driven glue (resize images on upload, process webhooks), scheduled jobs, and teams that want zero server maintenance.

### Constraints to design around

- **Cold starts**: the first invocation on a new instance is slower. Keep bundles small and initialize clients outside the handler.
- **Statelessness**: local memory and disk are temporary; state goes in databases or storage.
- **Time and size limits** on execution and payloads.
- **Database connections**: many parallel instances can exhaust a database, so use a pooler or an HTTP-based database driver.
- **Vendor lock-in** and harder local debugging; frameworks like SST or the Serverless Framework help.
- Costs can exceed a plain server at steady high load.

### Edge functions

Workers and edge runtimes run code in CDN locations close to users with near-zero cold starts, but with restricted APIs (no arbitrary TCP, limited Node compatibility).

You are ready to move on when you have deployed an HTTP function and a queue-triggered function, measured a cold start, and can state when a container would be cheaper.

## Key concepts

- **Event triggers**
- **Cold starts and warm instances**
- **Stateless handlers**
- **Database connections from functions**
- **Edge functions and Workers**
- **Cost model vs containers**
- **Infrastructure as code for serverless**

## Mini project

Build a serverless image pipeline: an upload to object storage triggers a function that creates thumbnails and writes metadata to a database, with an HTTP function to list results.

## Check yourself

Why should you create your database client outside the function handler rather than inside it?

## Free resources

- [What is AWS Lambda?](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) (AWS)
- [Cloudflare Workers documentation](https://developers.cloudflare.com/workers/) (Cloudflare)
- [Serverless Architectures](https://martinfowler.com/articles/serverless.html) (Martin Fowler)

Previous: [Microservices and Service-Oriented Architecture](https://www.internhack.in/roadmaps/tracks/backend/microservices-soa) · Next: [Twelve-Factor Apps and Cloud-Native Habits](https://www.internhack.in/roadmaps/tracks/backend/twelve-factor-apps)

## More in Architecture and Design Patterns

- [Monoliths and Modular Monoliths](https://www.internhack.in/roadmaps/tracks/backend/monolith-modular)
- [Microservices and Service-Oriented Architecture](https://www.internhack.in/roadmaps/tracks/backend/microservices-soa)
- [Serverless and Functions as a Service](https://www.internhack.in/roadmaps/tracks/backend/serverless)
- [Twelve-Factor Apps and Cloud-Native Habits](https://www.internhack.in/roadmaps/tracks/backend/twelve-factor-apps)
- [Service Mesh](https://www.internhack.in/roadmaps/tracks/backend/service-mesh)
- [Design Patterns for Backend Code](https://www.internhack.in/roadmaps/tracks/backend/design-patterns-gof)
- [Domain-Driven Design](https://www.internhack.in/roadmaps/tracks/backend/domain-driven-design)
- [CQRS and Event Sourcing](https://www.internhack.in/roadmaps/tracks/backend/cqrs-event-sourcing)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
