Skip to content

AWS Lambda

Lambda is a function runner with a control plane in front of it. You upload code, AWS runs it in response to an event, bills you for milliseconds, and throws the environment away eventually. The interview value is not the API — it is knowing where the model breaks.

Lambda runs a function in an isolated execution environment managed by AWS. You pick memory, which also sets CPU, and a timeout up to 15 minutes. Invocations are synchronous (API Gateway, ALB), asynchronous (S3, SNS) with built-in retries, or poll-based through an event source mapping (SQS, Kinesis, DynamoDB Streams). Concurrency is capped per region, so you reserve or provision capacity for critical paths. The environment may be reused, so do not assume a fresh process — but do not assume it stays either. Cold starts, the 15-minute timeout and at-least-once delivery are the three constraints that decide whether Lambda fits.

Each invocation runs in an execution environment: a sandbox with your runtime, your code and a /tmp scratch space. Three phases:

  1. Init — download code, start the runtime, run code outside the handler.
  2. Invoke — call your handler with event and context.
  3. Freeze/teardown — the environment is frozen after the response and may be reused, or destroyed at any time. Treat cached connections as warm, never as guaranteed.
Type Examples Retries Payload
Synchronous API Gateway, ALB, direct Invoke Caller decides 6 MB
Asynchronous S3, SNS, EventBridge 2 automatic, then DLQ 256 KB
Event source mapping SQS, Kinesis, DynamoDB Streams Poller-managed, batched Batched records

The distinction drives error handling. Synchronous callers see the error and decide. Asynchronous invocations retry silently, so a non-idempotent function can double-charge a customer. Event source mappings batch records and retry the whole batch on failure unless you enable partial batch responses.

A cold start is init plus runtime startup before your handler runs. It is worst for JVM and .NET, better for Node and Python, mostly invisible for Go and Rust. Contributing factors: package size, dependency count, VPC attachment (much improved in recent years) and framework initialisation.

Mitigations, in order:

  1. Trim the package. Fewer dependencies and a smaller artefact beat every trick below.
  2. Do less at init. Lazy-load what an invocation may not need; parallelise what it does.
  3. Provisioned concurrency. Pre-initialised environments kept warm. Billed continuously, so use it for latency-critical paths only.
  4. SnapStart. Snapshot of an initialised runtime restored on invoke. Excellent for JVM; check current runtime support, since it expands over time.

Treat these as shapes; verify current numbers before quoting them.

Limit Typical value
Timeout 900 s, default 3 s
Memory 128 MB – 10,240 MB; CPU scales with it
Ephemeral /tmp 512 MB – 10,240 MB, configurable
Deployment package 50 MB zipped, 250 MB unzipped with layers
Container image up to 10 GB
Environment variables 4 KB total
Default concurrency 1,000 per region (soft)

Lambda scales by adding environments, roughly one per in-flight request, up to the account limit.

  • Reserved concurrency carves a slice of the pool for one function and caps it. It protects downstream systems (a database with 100 connections) and isolates other functions from a runaway.
  • Burst scaling is fast but not instant. A spike can return 429 TooManyRequestsException; callers must retry with backoff.

The classic failure: one Lambda per request opening a Postgres connection until max_connections is exhausted. Fix with a pooler (RDS Proxy, PgBouncer), a lower concurrency cap, or a different data access pattern.

Because retries and at-least-once sources exist, every side-effecting function should be idempotent. The pattern is a dedupe key with a conditional write:

idempotent write
def handler(event, context):
key = event["payment_id"] # stable, from the producer
# Conditional put: fails if the key already exists, so retries are safe.
if not claim(key):
return {"status": "duplicate"}
charge_card(event)
table.update_item(Key={"pk": key}, UpdateExpression="SET #s = :done")
return {"status": "charged"}

Add a TTL so the table does not grow forever, and record the outcome, not just the attempt — or a crash mid-flight leaves a permanent “processing” state.

Zip small functions into the artefact. Layers share dependencies but add version coupling and count toward the 250 MB unzipped budget. Container images up to 10 GB suit heavy dependencies, at the cost of slower cold starts. Use infrastructure as code — SAM, CDK, Terraform — for anything real.

  • Work runs long. Anything near 15 minutes, or a naturally multi-hour batch.
  • Traffic is steady and high. At constant load, reserved EC2 or Fargate is cheaper per request.
  • You need a persistent connection. WebSockets with server state, long gRPC streams, shared in-memory caches.
  • Latency budget is tight and unpredicted. p99 will include cold starts.
  • The workload is stateful or CPU-saturated for long periods. You pay for wall-clock time.
  1. Walk me through what happens between a request arriving and your handler running.
  2. How do you stop Lambda from overwhelming an RDS instance?
  3. SQS triggers your function and it throws. What happens to the message?
  4. How do you make a payment function safe against duplicate delivery?
  5. When would you move this off Lambda, and to what?
  • “Lambda is stateless”, then caching in a global. It is sometimes warm; design for both.
  • Secrets in environment variables. Use Secrets Manager or SSM Parameter Store.
  • Assuming synchronous retries. They do not exist; the caller owns them.