Lesson 2 of 10 Serverless and API Architecture

Architecting Lambda Applications at Scale

This lesson explains the decisions an architect makes once a serverless application grows beyond a prototype: Lambda's concurrency model, its interaction with databases and downstream systems, and its cost profile.

Learning outcomes

After completing this lesson, you will be able to:

  • Calculate required concurrency and describe scaling limits.
  • Protect downstream systems with RDS Proxy, queues and concurrency limits.
  • Explain failure handling for each invocation model.
  • Decide when Lambda is not the cheapest choice.

Concurrency and scaling

Each Lambda execution environment handles one request at a time, so the concurrency a function needs equals its requests per second multiplied by its average duration in seconds. A function receiving 2,000 requests a second that runs for 300 ms needs about 600 concurrent environments. All functions in an account share a Regional concurrency quota, 1,000 by default and raisable, and each function can add up to 1,000 environments every ten seconds, so an extreme burst can be throttled briefly even below the quota.

Several controls shape how a function uses that capacity. The following table summarises them.

ControlPurpose
Reserved concurrencyGuarantees capacity for a critical function and caps it so it cannot starve others or overwhelm a downstream system
Provisioned concurrencyKeeps environments initialised to remove cold starts for latency-sensitive paths; can be scheduled or auto scaled
Lambda SnapStartSnapshots an initialised environment (Java, Python, .NET) to cut cold-start time with no idle cost
Maximum concurrency on SQS mappingsLimits how hard a queue drives a function

Protecting downstream systems

Lambda can scale faster than the systems it calls. Thousands of concurrent functions opening database connections exhaust a relational database within seconds. RDS Proxy pools and shares connections, and also shortens failover times. For non-database dependencies, a queue in front of the function, with maximum concurrency on the event source mapping, converts a burst into a steady stream the dependency can absorb. DynamoDB, which accepts HTTP requests rather than persistent connections, pairs naturally with Lambda.

The following diagram shows the two ways of protecting what Lambda calls.

Protecting downstream systems from Lambda's scaling. For a relational database, RDS Proxy pools and shares connections, so thousands of concurrent functions do not exhaust the database's connection limit. For other dependencies, a queue absorbs the burst, and maximum concurrency on the event source mapping caps how many functions call the dependency at once.
Both designs turn Lambda's ability to scale quickly into a load that the slower system behind it can sustain.
Scenario questions

Protecting a downstream database

Exam skills 1.1.9 · 5.4.2

Stop Lambda's scaling from overwhelming downstream systems.

Log in to try this activity Interactive activities are free with an account.

Invocation models and failure handling

How a function is invoked decides who handles a failure. In a synchronous invocation, from API Gateway, an ALB or an SDK call, the caller waits and receives the error, and decides whether to retry. In an asynchronous invocation, from S3, SNS or EventBridge, Lambda queues the event and retries it twice on failure. It then sends the event to an on-failure destination or dead-letter queue, if one is configured, and otherwise discards it.

With an event source mapping, Lambda polls SQS, Kinesis, DynamoDB Streams or MSK and retries failed batches. Partial batch responses, batch bisection and on-failure destinations isolate the bad records so that they do not block the rest. The following table summarises the models.

ModelSourcesFailure handling
SynchronousAPI Gateway, ALB, SDK callsCaller receives the error and decides whether to retry
AsynchronousS3, SNS, EventBridgeLambda retries twice, then sends to an on-failure destination or DLQ
Event source mapping (poll)SQS, Kinesis, DynamoDB Streams, MSKBatch retries; partial batch responses, bisecting and on-failure destinations isolate bad records

Cost and when not to use Lambda

Lambda bills per request and per GB-second, which is ideal for spiky or low-volume workloads and for anything idle much of the time. For workloads that run at high, steady utilisation around the clock, containers on Fargate or EC2 with Savings Plans can be cheaper. Compute Savings Plans apply to Lambda too, and Graviton (arm64) functions offer better price performance. Work longer than 15 minutes belongs in Step Functions, Lambda durable functions or Fargate tasks.

Sort into groups

Lambda or something else?

Exam skills 1.1.9 · 3.1.2

Decide whether Lambda is a good fit for each workload.

Log in to try this activity Interactive activities are free with an account.
Worked example

Surviving a flash sale

An order API on API Gateway and Lambda writes to Aurora PostgreSQL. During a flash sale, traffic jumps from 50 to 3,000 requests a second; the database runs out of connections and orders fail. The architect changes the design in three ways:

  1. API Gateway writes accepted orders straight to an SQS queue through a service integration and returns 202 Accepted, so the front end never waits for the database.
  2. A worker function consumes the queue with maximum concurrency of 50, so the database never sees more than 50 concurrent writers.
  3. The worker connects through RDS Proxy, which multiplexes those sessions onto a small pool of database connections and survives an Aurora failover gracefully.

Customers receive confirmation by notification once their order is written. The queue's ApproximateAgeOfOldestMessage metric shows how far behind processing is, and an alarm on it tells the team if the backlog grows beyond the promised confirmation time.

Check yourself

When is Lambda not the cheapest compute option?

Show answer

For steady, high-utilisation workloads that run all the time, where containers or EC2 with Savings Plans cost less per unit of work.

Exam Tips

Remember for the exam

  • Concurrency = requests per second x average duration in seconds.
  • To guarantee and cap a function's capacity, use reserved concurrency; to remove cold starts, use provisioned concurrency or SnapStart.
  • When many functions connect to a relational database, use RDS Proxy.
  • To protect a downstream system from bursts, put SQS in front and set maximum concurrency on the mapping.
  • For work over 15 minutes, use Step Functions, Fargate or Batch.
  • Steady 24x7 high utilisation may be cheaper on containers with Savings Plans than on Lambda.