Learn

Navigate through learn topics

Serverless Computing

Understanding serverless architecture, functions and event-driven computing

Last updated: 8/15/2025

Learn how serverless computing eliminates infrastructure management, allowing you to focus purely on code while paying only for what you use.

What is Serverless?

The Core Concept

Running code without managing servers

Serverless doesn't mean there are no servers - it means you don't have to think about them. It's like using electricity: you plug in your device without worrying about the power plant.

Real-world analogy: Serverless is like taking a taxi instead of owning a car. You don't worry about maintenance, parking, or insurance - you just pay for the journey when you need it.

Serverless Functions

Function as a Service (FaaS)

Code that runs on demand

Functions are small pieces of code that execute in response to events. They start up, do their job and shut down automatically.

// Example AWS Lambda function
exports.handler = async (event) => {
  // Process the incoming event
  const name = event.name || 'World';
  
  // Return a response
  return {
    statusCode: 200,
    body: `Hello, ${name}!`
  };
};

Key characteristics:

  • Stateless execution
  • Automatic scaling
  • Pay per invocation
  • Event-driven triggers

Common Triggers

What makes functions run

  • HTTP requests: API endpoints
  • Database changes: When data is added/modified
  • File uploads: Processing images or documents
  • Scheduled tasks: Cron jobs and timers
  • Message queues: Processing background jobs

Serverless Platforms

Major Providers

AWS Lambda

Amazon's serverless compute service

The pioneer of serverless computing, supporting multiple languages and deep AWS integration.

Use cases:

  • API backends
  • Data processing pipelines
  • Real-time file processing
  • IoT data handling

Vercel Functions

Frontend-focused serverless

Optimised for web applications with seamless Next.js integration.

// api/hello.js in a Vercel project
export default function handler(req, res) {
  res.status(200).json({ message: 'Hello from Vercel!' });
}

Cloudflare Workers

Edge computing at scale

Runs code at the network edge, closer to users for ultra-low latency.

Unique features:

  • Runs in 300+ locations globally
  • Sub-millisecond cold starts
  • WebAssembly support

Google Cloud Functions

Google's event-driven compute

Integrates well with Google services and supports many runtime languages.

Azure Functions

Microsoft's serverless offering

Strong enterprise features with excellent Visual Studio integration.

Serverless Databases

Database as a Service

Databases that scale automatically

Just like serverless functions, serverless databases handle scaling, backups and maintenance automatically.

Popular Options

Supabase

  • PostgreSQL-based
  • Real-time subscriptions
  • Built-in authentication

PlanetScale

  • MySQL-compatible
  • Branching for database schemas
  • Automatic sharding

DynamoDB

  • NoSQL key-value store
  • Single-digit millisecond latency
  • Infinite scaling

Fauna

  • Globally distributed
  • ACID transactions
  • Multi-region consistency

Event-Driven Architecture

Understanding Events

Building reactive systems

In serverless, everything is triggered by events. Your code responds to things happening rather than constantly running and checking.

// Event-driven order processing
async function processOrder(orderEvent) {
  // 1. Validate payment
  await validatePayment(orderEvent.paymentId);
  
  // 2. Update inventory
  await updateInventory(orderEvent.items);
  
  // 3. Send confirmation email
  await sendEmail(orderEvent.customerEmail);
  
  // 4. Trigger shipping
  await createShippingLabel(orderEvent.address);
}

Message Queues

Decoupling services

Queues let different parts of your system communicate without direct connections.

Common patterns:

  • SQS (Simple Queue Service): One-to-one messaging
  • SNS (Simple Notification Service): One-to-many broadcasting
  • EventBridge: Complex event routing

Serverless Architectures

API Gateway Pattern

Managing HTTP endpoints

API Gateway acts as the front door for your serverless functions, handling routing, authentication and rate limiting.

Client → API Gateway → Lambda Function → Database
                    ↓
              Authentication
                    ↓
              Rate Limiting

Microservices Pattern

Small, focused services

Each function handles one specific task, making the system modular and maintainable.

Example e-commerce architecture:

  • User service (authentication)
  • Product service (catalogue)
  • Order service (processing)
  • Payment service (transactions)
  • Notification service (emails/SMS)

Event Sourcing

Storing events, not state

Instead of storing the current state, store all events that led to it. Perfect for audit trails and debugging.

Cold Starts

Understanding Cold Starts

The first-run delay

When a function hasn't run recently, it needs to "warm up" - loading code and establishing connections. This initial delay is called a cold start.

Typical cold start times:

  • Node.js: 100-300ms
  • Python: 200-400ms
  • Java: 500-1500ms
  • Rust/Go: 50-100ms

Mitigation Strategies

Keep functions warm:

// Scheduled ping to prevent cold starts
exports.warmer = async (event) => {
  if (event.source === 'warmer') {
    return { statusCode: 200, body: 'Function warmed' };
  }
  // Regular function logic
};

Optimise bundle size:

  • Use tree shaking
  • Minimise dependencies
  • Lazy load when possible

Provisioned concurrency:

  • Pre-warm a set number of instances
  • Higher cost but guaranteed performance

Cost Optimisation

Pricing Models

Understanding serverless costs

Typical charges:

  • Number of requests
  • Execution duration
  • Memory allocation
  • Data transfer

Optimisation Tips

Reduce execution time:

// Bad: Sequential operations
const user = await getUser(id);
const orders = await getOrders(user.id);
const preferences = await getPreferences(user.id);

// Good: Parallel operations
const [user, orders, preferences] = await Promise.all([
  getUser(id),
  getOrders(id),
  getPreferences(id)
]);

Right-size memory:

  • More memory = faster CPU
  • Find the sweet spot for cost/performance

Cache frequently accessed data:

  • Use environment variables for config
  • Cache database connections
  • Store computed results

Monitoring and Debugging

Observability Tools

Seeing what's happening

CloudWatch (AWS):

  • Logs, metrics and traces
  • Custom dashboards
  • Alerting

Datadog:

  • Distributed tracing
  • Performance monitoring
  • Error tracking

Debugging Strategies

Structured logging:

console.log(JSON.stringify({
  level: 'info',
  message: 'Order processed',
  orderId: order.id,
  userId: user.id,
  timestamp: new Date().toISOString()
}));

Correlation IDs: Track requests across multiple functions

Local testing:

  • Serverless Framework
  • SAM CLI
  • Functions Framework

Use Cases

Perfect for Serverless

APIs and webhooks

  • Variable traffic
  • Stateless operations
  • Quick response times

Data processing

  • Image resizing
  • Video transcoding
  • ETL pipelines

Scheduled tasks

  • Reports generation
  • Cleanup jobs
  • Data synchronisation

IoT backends

  • Sensor data ingestion
  • Device management
  • Real-time processing

Not Ideal for Serverless

Long-running processes

  • Video rendering (hours)
  • Large batch jobs
  • WebSocket connections (sometimes)

High-performance computing

  • Scientific simulations
  • Machine learning training
  • Real-time gaming

Stateful applications

  • Desktop applications
  • Traditional databases
  • Session-heavy apps

Best Practices

Security

  • Use environment variables for secrets
  • Implement least-privilege IAM roles
  • Validate all inputs
  • Enable encryption at rest

Performance

  • Minimise cold starts
  • Optimise bundle sizes
  • Use connection pooling
  • Cache when possible

Development

  • Use infrastructure as code
  • Implement CI/CD pipelines
  • Write unit tests
  • Monitor everything

Getting Started

Your First Function

Step 1: Choose a platform

  • Vercel for web apps
  • AWS Lambda for general purpose
  • Cloudflare Workers for edge computing

Step 2: Start simple

// A basic HTTP endpoint
export default async function(req, res) {
  const { name = 'World' } = req.query;
  res.json({ message: `Hello, ${name}!` });
}

Step 3: Deploy and iterate

  • Deploy with one command
  • Monitor performance
  • Add features incrementally

The Future of Serverless

Emerging Trends

Edge computing: Running functions closer to users for ultra-low latency

WebAssembly: Running any language at near-native speed

Serverless containers: Best of both worlds - containers without management

AI/ML integration: Serverless inference and model serving

Summary

Serverless computing represents a paradigm shift in how we build and deploy applications. By removing infrastructure concerns, it lets developers focus on writing business logic while the platform handles scaling, availability and maintenance.

Key takeaways:

  • Pay only for what you use
  • Automatic scaling from zero to millions
  • No infrastructure management
  • Event-driven by design
  • Perfect for variable workloads

Start small, experiment often and let the platform handle the heavy lifting!