Volume 4 · Chapter 3 — Compute and Containers

Serverless Compute with Lambda

Goutham Mayaluri 10 min read AWSCloudLambdaContainers

3.1 Introduction

Serverless compute takes the managed-service idea to its conclusion: you provide code, and AWS runs it without you managing any servers at all. AWS Lambda is the core service - it runs your function in response to events, scales automatically from zero to massive concurrency, and charges only for the time your code actually executes. Serverless is a frequent interview topic because it changes how you think about compute (event-driven, stateless, pay-per-use) and because knowing when it fits - and when it doesn’t - is a mark of good judgment.

3.2 What Lambda Is

Lambda runs a function - a piece of code - in response to a trigger, without you provisioning or managing servers. You upload the code, configure its memory and timeout, give it an IAM role, and specify what triggers it; AWS handles everything else - provisioning capacity, running the code, and scaling. It scales automatically and independently per request: if a thousand events arrive at once, Lambda runs many concurrent executions; if none arrive, nothing runs and nothing is billed. You pay only for the number of requests and the compute time consumed, rounded to the millisecond, so idle costs nothing. This is a fundamentally different model from a server that runs continuously waiting for work.

3.3 Event-Driven Execution

Lambda is inherently event-driven - functions run in response to triggers from across AWS. An object uploaded to S3 can trigger a function to process it; a message arriving on a queue can trigger a function to handle it; an API Gateway request can invoke a function to serve an HTTP endpoint; a scheduled event can run a function periodically; a database stream can trigger a function on data changes. This makes Lambda the natural glue for event-driven architectures: each function does one job in response to one kind of event, scales on its own, and composes with others through events. The mental shift is from “a server handling everything” to “small functions reacting to specific events.”

flowchart LR
  S3[S3 upload] --> L1[Lambda: process file]
  Q[SQS message] --> L2[Lambda: handle transaction]
  API[API Gateway request] --> L3[Lambda: serve endpoint]
  Sched[Scheduled event] --> L4[Lambda: nightly job]
  L1 & L2 & L3 & L4 --> Scale[(Auto-scales per event; zero cost when idle)]

3.4 Statelessness, Cold Starts, and Limits

Lambda functions are stateless - each invocation is independent and keeps no local state between runs, so any state must live in external stores like a database, cache, or object storage. This is what lets Lambda scale freely. Two practical considerations matter. Cold starts: when a function scales up or hasn’t run recently, there’s a brief startup delay to initialize the execution environment before the code runs, which adds latency to those first invocations; this is manageable and can be mitigated but is a factor for latency-sensitive paths. And limits: functions have a maximum execution duration and memory, so Lambda suits short-to-medium tasks rather than long-running processes. Understanding these bounds is how you decide whether a workload fits serverless.

3.5 When to Use Serverless

Lambda fits event-driven, spiky, or intermittent workloads especially well - processing uploads, handling queue messages, running scheduled jobs, serving APIs with variable traffic - because it scales automatically and costs nothing when idle, removing both server management and idle spend. It’s less suited to long-running processes that exceed its duration limits, workloads needing very consistent ultra-low latency where cold starts matter, or steady high-throughput workloads that might be cheaper on always-on committed capacity. The senior framing is to reach for serverless when the workload is event-driven and variable and you want zero operational overhead and zero idle cost, and to use containers or instances when you need long-running processes, fine-grained control, or steady high throughput - matching the model to the workload rather than defaulting either way.

3.6 Banking Production Scenario

A bank uses Lambda for event-driven pieces of its platform. When a customer uploads a document to S3, a Lambda function is triggered to validate and process it. Transaction messages landing on a queue trigger functions that perform fraud checks and enrichment, scaling automatically with volume and costing nothing between bursts. A nightly scheduled event runs a reconciliation function. Each function is stateless, holds an IAM role granting only what it needs, reads secrets at runtime, and writes results to databases and queues. For the core, latency-sensitive transaction path with steady high throughput, the bank keeps container-based services rather than Lambda, reserving serverless for the event-driven, variable work where its automatic scaling and zero idle cost shine.

3.7 Troubleshooting: Lambda Issues

Lambda problems have characteristic signatures. Functions timing out are hitting the duration limit - either the work is too long for Lambda (move to a container) or an external call is slow (fix the dependency or raise the timeout within limits). Latency spikes on first calls are cold starts - mitigated with provisioned concurrency for latency-sensitive functions. “Access denied” from a function means its IAM role lacks a permission. Functions failing under load may be hitting concurrency limits or overwhelming a downstream database that can’t scale as fast - the fix is concurrency controls and a queue to buffer, or a scalable data store. Lost state between invocations is expected - state must be external. The diagnostic pattern is to check duration and memory against the workload, the IAM role, concurrency and downstream capacity, and to confirm no reliance on local state.

Interview Question

What is AWS Lambda, and when would you choose serverless over containers or EC2?

AWS Lambda is a serverless compute service that runs your code - a function - in response to events, without you provisioning or managing any servers. You upload the code, set its memory, timeout, and IAM role, and specify what triggers it, and AWS handles provisioning, running, and scaling. Its defining characteristics are automatic per-request scaling and pay-per-use billing: if many events arrive at once Lambda runs many concurrent executions, if none arrive nothing runs, and you pay only for the number of requests and the execution time consumed to the millisecond, so idle costs nothing at all. It’s inherently event-driven - functions are triggered by things like an S3 upload, a queue message, an API Gateway request, a scheduled event, or a database stream - which makes it natural glue for event-driven architectures where small functions each react to a specific event. Functions are stateless, with any state kept in external stores, which is what lets them scale freely. I choose serverless when the workload is event-driven, spiky, or intermittent and I want zero server management and zero idle cost - processing uploads, handling queue messages, running scheduled jobs, or serving APIs with variable traffic - because Lambda scales automatically from zero and costs nothing between bursts, eliminating both operational overhead and idle spend, and it’s ideal for gluing services together reactively. I choose containers or EC2 instead when the workload doesn’t fit that model: long-running processes that would exceed Lambda’s duration limits, latency-sensitive paths where cold-start delay on scale-up is unacceptable without mitigation, steady high-throughput workloads that may be cheaper on always-on committed capacity, or cases needing fine-grained control over the runtime and environment. The trade-offs to weigh are Lambda’s cold starts (a brief startup delay when scaling up or after idle) and its execution-duration and memory limits, which bound it to short-to-medium tasks. So the judgment is to match the model to the workload: serverless for event-driven, variable, intermittent work where automatic scaling and zero idle cost are the priority; containers or instances for long-running, steady-throughput, or control-heavy workloads. A bank, for instance, uses Lambda for event-driven pieces like processing uploads and queue messages, while keeping its steady, latency- sensitive core transaction path on container-based services - reaching for serverless where it clearly fits rather than defaulting to it everywhere.

Interview Question

What are cold starts and statelessness in Lambda, and how do they affect design?

Both are fundamental characteristics of Lambda that shape how you design for it. Statelessness means each invocation of a function is independent and keeps no local state between runs - a function can’t rely on data it stored in memory or on local disk during a previous invocation, because the next invocation may run in a completely fresh environment. This is actually what enables Lambda to scale freely: because no invocation depends on local state, AWS can run as many concurrent executions as needed and spin them up and down at will. The design implication is that any state must live in external stores - a database, a cache, object storage - and the function reads and writes there rather than holding state locally; functions are pure event handlers that take an input, use external state, and produce a result. A cold start is the brief startup delay that occurs when Lambda has to initialize a new execution environment before running your code - which happens when a function scales up to handle more concurrency or when it hasn’t run recently and the environment has been recycled. The environment must be set up and the code initialized before the first request is served, adding latency to those particular invocations; subsequent invocations reusing a warm environment don’t pay that cost. The design impact is on latency-sensitive paths: if a function must respond with very low latency consistently, cold-start delay on scale-up can be a problem, and you mitigate it - for example with provisioned concurrency that keeps environments warm and ready, or by keeping functions lightweight so they initialize quickly. For workloads where a little extra latency on occasional first calls is fine, cold starts are a non-issue. Together these characteristics guide design: build functions to be stateless with all state externalized so they scale cleanly and any invocation can handle any event; keep them focused and fast to initialize to minimize cold-start impact; use provisioned concurrency where consistent low latency matters; and recognize that Lambda’s statelessness and startup behaviour make it excellent for event-driven, scalable work but a poor fit for anything that needs persistent local state or guaranteed ultra-low latency without mitigation. So statelessness forces state into external stores and enables free scaling, and cold starts add first-invocation latency you manage for latency-sensitive paths - both are core to designing Lambda functions correctly.

Chapter Revision Notes

  • Lambda runs code in response to events with no servers to manage; it scales automatically per request and charges only for execution time (zero idle cost).
  • It’s event-driven - triggered by S3, queues, API Gateway, schedules, streams - and is natural glue for event-driven architectures.
  • Functions are stateless (state lives in external stores), which enables free scaling; cold starts add latency on scale-up/after idle.
  • Duration and memory limits suit short-to-medium tasks, not long-running processes.
  • Use serverless for event-driven, spiky, intermittent work; use containers/EC2 for long-running, steady-throughput, or control-heavy workloads.