Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 37 ยท Operating Microservices

Serverless Functions and Microservices

Learn how serverless functions fit into a microservices system: event triggers, scaling, cold starts, limits, security practices and when containers are the better choice.

A microservice normally runs as a long-lived process in a container, and somebody has to decide how many copies to run. Serverless computing removes that decision. You upload a small function; the platform runs it when something triggers it, scales the number of running copies up and down automatically (down to zero), and bills for the time and memory actually used. Servers still exist, but you do not provision or patch them. This model is also called Function as a Service (FaaS).

Serverless is not a replacement for microservices; it is another way to deploy them. A service can be one container, a group of functions behind an API, or a mix. This lesson covers how functions are triggered, how they scale, what goes wrong (cold starts, limits, security) and how to decide between functions and containers (Containers, Docker Compose and Kubernetes).

How a function runs

A function is a stateless handler: it receives an event, does work and returns a result. The platform keeps execution environments (small containers or micro-VMs) and routes events to them. Common triggers are:

  • an HTTP request, usually through an API gateway (API Gateway and Backend for Frontend), which can also run an authorizer function that checks tokens before your code runs;
  • a message on a queue or topic (Asynchronous Messaging and Events);
  • a change in storage (a file uploaded), a database change event, or a timer (cron);
  • another function, as a step in a workflow.

Because the same function runs in many copies at once and any copy may be thrown away, functions must be stateless: keep durable state in a database, cache or object store, never in memory or on local disk. They must also be idempotent, because event sources deliver at least once (The Transactional Outbox and Idempotent Consumers).

Scaling, cold starts and limits

When an event arrives and no idle environment exists, the platform creates one: download the code, start the runtime, run your initialization. That extra delay is the cold start. Later events reuse the warm environment until it has been idle for a while. Platforms also enforce limits: a maximum number of concurrent executions (excess requests are throttled with an error such as HTTP 429), a maximum run time per call, and caps on memory and payload size.

The platform below is a small simulation with virtual time. It shows three cold starts for the first burst, a throttled request when the concurrency limit is reached, warm reuse a moment later, and new cold starts after the environments went idle.

JS Cold starts, warm reuse and throttling

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

No output.

Ways to reduce the pain:

  • Keep functions small and fast to start: fewer dependencies, lazy-load rarely used code, and pick a runtime with quick startup.
  • Initialize outside the handler: open database clients and load configuration once per environment so warm calls reuse them.
  • Reserve warm capacity (provisioned concurrency or minimum instances) for latency-sensitive paths, at a cost.
  • Manage connections efficiently: thousands of parallel functions can exhaust a database's connection limit, so use a connection pool proxy or an HTTP-based data API.
  • Set throttles and concurrency caps deliberately so a burst cannot overwhelm a downstream system; pair with Rate Limiting, Load Shedding and Backpressure and Timeouts, Retries and Exponential Backoff.

Strengths and challenges

Strength Challenge
Pay per use, no idle cost Cost can exceed containers at steady high load
Automatic scaling, including to zero Cold-start latency; unpredictable p99
No servers to patch Execution time, memory and payload limits
Natural fit for event processing Harder local testing and debugging; many small pieces to trace
Fast to ship small features Vendor-specific triggers and APIs cause lock-in

Good fits: webhooks, image and file processing, scheduled jobs, chatbot and assistant back ends, glue between services, spiky or unpredictable traffic, and prototypes. Poor fits: long-running jobs, latency-critical paths that cannot tolerate cold starts, workloads needing persistent connections or large in-memory state, and steady heavy traffic where a reserved container fleet is cheaper.

Security best practices

  • Give each function its own role with least privilege: only the queues, tables and secrets it uses.
  • Validate every input. The event may come from the public Internet or an untrusted queue; injection attacks apply as much as in any service (Securing the Edge and Data).
  • Keep secrets in a secrets manager, not in code or plain environment variables (Configuration and Secrets).
  • Authenticate at the gateway and between services (Authentication and Authorization Between Services).
  • Keep dependencies small and patched: each library is attack surface, and old code stays deployed for a long time.
  • Log and trace every invocation with a correlation ID (Logs, Metrics and Observability); with many short-lived instances, central logs are the only way to see what happened.

For the wider system-design picture of this style, see Event-Driven Architecture.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a cold start?

    When no idle environment exists, the platform loads the code and starts the runtime before your handler runs. Later calls reuse the warm environment.
  2. 2. Why must serverless functions be stateless?

    Environments are ephemeral and many copies run in parallel. Durable state belongs in a database, cache or object store.
  3. 3. A burst of requests exceeds the concurrency limit. What typically happens to the extra requests?

    Platforms cap concurrent executions; excess calls are throttled or queued, so callers need retries with backoff.
  4. 4. Which workload is usually a poor fit for functions?

    Cold starts, limits and per-call billing make functions a weaker choice for steady heavy load with large state.

Summary

  • Serverless runs stateless functions on demand; the platform scales them, including to zero, and bills per use.
  • Triggers include HTTP via a gateway, queues, storage events and timers; handlers must be idempotent.
  • Cold starts, concurrency throttles, run-time limits and connection exhaustion are the main operational issues.
  • Use least-privilege roles, input validation and a secrets manager for security.
  • Functions suit spiky and event-driven work; containers suit steady, latency-critical services.

Next: Authentication and Authorization Between Services.