Jan 22, 20264 min read

Serverless Functions: What are they and when to use them

What is Serverless and should you use it when building microservices?

Serverless functions (Function as a Service, or FaaS) are event-triggered code snippets that run without server management. You write a function, deploy it, and the cloud provider handles scaling, availability, and infrastructure. Common platforms include AWS Lambda, Google Cloud Functions, Azure Functions, and Vercel Functions. This guide covers when to use serverless functions, their limitations, and alternatives for backend development.

What Are Serverless Functions?

Serverless functions, or Function as a Service (FaaS), allow developers to execute code in response to events without managing underlying servers. They are stateless, event-driven, and automatically scale with demand.

When and Why to Use Serverless Functions?

When:

  • Rapid Prototyping: Quick deployment without server setup.
  • Event-Driven Scenarios: Such as reacting to file uploads, database changes, etc.
  • Variable Workloads: Managing spikes or drops in traffic.
  • Microservices Architecture: Building independent, scalable services.

Why:

  • Cost Efficiency: Pay only for execution time.
  • Ease of Management: No need to handle servers.
  • Automatic Scaling: Scales with demand.
  • Rapid Development: Focus on coding, not infrastructure.

How Do Serverless Functions Change Application Design?

Using serverless functions often requires a shift in application design:

  • State Management: Being stateless, serverless functions require different strategies for managing state.
  • Shorter Running Time: They are designed for short, reactive tasks. This means it is difficult to design for business logic requiring long-running tasks.
  • Event-Driven Architecture: The architecture revolves around reacting to events.
  • Integration with Other Services: Often used with other cloud services for full functionality.

Pros and Cons on Serverless Functions

Pros:

  • Cost-Effective: Pay for usage.
  • Rapid Deployment: Less focus on infrastructure.
  • Scalability: Automatic scaling.
  • Integration: Easy integration with other cloud services.

Cons:

  • Cold Starts: Initial latency in execution.
  • State Management: Challenges in managing state.
  • Limited Execution Time: Not suitable for long-running processes.
  • Vendor Lock-in: Potential reliance on specific cloud providers.

What Are Step Functions?

Step functions coordinate the components of distributed applications and microservices. They provide a way to sequence serverless functions and other services, creating visual workflows to build applications quickly.

What are Lambda Functions?

Serverless Lambda functions (an AWS offering) provide an environment to run code in various languages in reaction to triggers. They offer all the benefits of serverless architecture, enabling scalable, event-driven, cost-effective applications.

Limitations and Alternatives

While serverless functions work well for simple, event-driven tasks, they have significant limitations for backend applications:

LimitationImpact
Cold starts100ms-2s latency spikes on first invocation
Execution timeoutAWS Lambda: 15 min max; Vercel: 10s-300s depending on plan
Connection limitsDatabase connections are difficult to pool
No WebSocketsLong-lived connections require workarounds
State managementFunctions are stateless by design

For backend APIs that need persistent connections, background jobs, or low latency, consider alternatives like containers (Fargate, Cloud Run) or dedicated compute.

Encore: Flexible Deployment Without Lock-in

Encore provides a development platform that lets you write backend code once and deploy to the most appropriate infrastructure:

  • Serverless when it makes sense: Cloud Run, Fargate for cost-effective scaling
  • Containers when you need control: Kubernetes, ECS for persistent workloads
  • Same code, any target: No code changes required when switching deployment models, since the infrastructure is provisioned from the code

Encore also includes built-in primitives, such as cron jobs and SQL databases, that eliminate common serverless workarounds:

import { CronJob } from "encore.dev/cron"; import { SQLDatabase } from "encore.dev/storage/sqldb"; // Cron jobs run reliably without timeout limits new CronJob("daily-report", { schedule: "0 9 * * *", endpoint: generateDailyReport, }); // Database connections are pooled automatically const db = new SQLDatabase("app", { migrations: "./migrations" });

For teams using Vercel for their frontend but needing a robust backend, see Encore vs Vercel for Backends.

Conclusion

Serverless functions mark a significant advancement in cloud computing, enabling scalable, event-driven applications with cost efficiency. While there are notable pros and cons, understanding your specific needs and constraints will guide the most effective use of serverless functions. Integrating technologies like Encore can provide an even more flexible, adaptable, and future-proof approach to modern development.

Install Encore
brew install encoredev/tap/encore &&
encore app create
Copy

Ship without waiting on Terraform

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 4.8 · Encore MCP connected~/orders
Infra from codereading the code…
SQLDatabaseorders · postgres
Topicorders · pub/sub
Bucketdeclared in code
not running yet