All exams← All AWS Developer Associate (DVA-C02) notes

Refactoring & optimisation: AWS Developer Associate (DVA-C02) revision notes

Revision notes

Why this topic matters

DVA-C02 tests whether you can spot performance and cost problems in code and infrastructure, then fix them using the right AWS feature rather than brute-force scaling. Exam questions usually describe a slow, expensive, or throttled app and ask what change fixes it cheapest/fastest.

Lambda optimisation

  • Increasing memory also increases proportional CPU and network - a common fix for slow functions is simply raising memory, not just code changes.
  • Provisioned Concurrency removes cold starts by keeping execution environments warm - use for latency-sensitive APIs.
  • Keep the handler lean: initialise SDK clients and DB connections OUTSIDE the handler function so they persist across warm invocations.
  • Lambda SnapStart (Java, and now other runtimes) caches a snapshot of an initialised execution environment to cut cold-start time drastically.
  • Use environment variables and Lambda layers to share code/dependencies instead of duplicating in every deployment package - smaller packages deploy and cold-start faster.

DynamoDB optimisation

  • Design partition keys for even distribution - hot partitions throttle throughput even under provisioned capacity.
  • Use DynamoDB Accelerator (DAX) for microsecond read caching in read-heavy workloads.
  • Switch to on-demand capacity mode for unpredictable traffic; use provisioned with auto scaling for steady, predictable traffic - cheaper at scale.
  • Batch operations (BatchGetItem, BatchWriteItem) reduce round trips versus single-item calls.
  • Use sparse indexes and projections to avoid pulling unnecessary attributes.

Caching layers

  • ElastiCache (Redis/Memcached) offloads repeated reads from RDS/DynamoDB - biggest win for read-heavy relational workloads.
  • CloudFront caches at edge locations, cutting origin load and latency for static and semi-static content; use cache behaviours and TTLs per path pattern.
  • API Gateway has its own response caching (per stage, TTL configurable 0-3600s) - reduces backend Lambda/API invocations for repeated identical requests.

Code-level and messaging optimisation

  • Use SQS to decouple and buffer bursty workloads so downstream services process at their own pace - prevents overload rather than requiring bigger instances.
  • Use asynchronous invocation or Step Functions for long-running workflows instead of synchronous blocking calls.
  • X-Ray traces reveal exactly which downstream call or segment is the bottleneck - always the first tool the exam expects you to reach for when diagnosing 'why is this slow'.
  • CloudWatch Lambda Insights and Application Signals give system-level metrics (memory used vs allocated, cold start counts) to right-size functions.

Common mistakes

  • Assuming more compute always fixes it - often it is a hot partition key or missing cache, not raw power.
  • Forgetting SDK client re-use across invocations, causing needless re-initialisation cost on every warm call.
  • Ignoring API Gateway/CloudFront caching and hitting the backend for identical repeated GET requests.
  • Using on-demand DynamoDB for steady, predictable heavy traffic where provisioned + auto scaling is cheaper.

Key facts

Flashcards

What is the single most common fix for a slow Lambda function on the exam?
Increase the memory allocation - CPU and network scale proportionally with memory
tap to reveal
How do you eliminate Lambda cold starts for a latency-sensitive API?
Enable Provisioned Concurrency
tap to reveal
What Lambda feature caches an initialised execution environment to speed up cold starts?
Lambda SnapStart
tap to reveal
Where should you initialise a DynamoDB or S3 SDK client in a Lambda function?
Outside the handler, so it is reused across warm invocations instead of recreated each time
tap to reveal
What DynamoDB feature gives microsecond read latency via in-memory caching?
DAX - DynamoDB Accelerator
tap to reveal
When should you choose DynamoDB on-demand capacity over provisioned?
When traffic is unpredictable or spiky; provisioned with auto scaling is cheaper for steady predictable load
tap to reveal
What causes DynamoDB throttling even when provisioned capacity looks sufficient?
A hot partition key - uneven key distribution concentrates traffic on one partition
tap to reveal
What is the TTL range for API Gateway stage-level response caching?
0 to 3600 seconds, configurable per stage
tap to reveal
What AWS service decouples a bursty producer from a slower downstream consumer?
SQS - it buffers messages so consumers process at their own pace
tap to reveal
What is the first tool to reach for when diagnosing why a distributed request is slow?
AWS X-Ray - it traces the request across services to isolate the bottleneck segment
tap to reveal
How do you cut repeated identical GET requests hitting your backend origin?
Use CloudFront edge caching or API Gateway response caching with an appropriate TTL
tap to reveal
What DynamoDB API calls reduce round trips for multiple items?
BatchGetItem and BatchWriteItem
tap to reveal
What is a common Lambda packaging mistake that slows cold starts?
Bundling large, unused dependencies instead of trimming the package or using Lambda layers for shared code
tap to reveal
Why use Step Functions or async invocation instead of synchronous Lambda calls for long workflows?
It avoids blocking callers and timeouts on long-running processes, letting each step run and retry independently
tap to reveal
Practice questions

Refactoring & optimisation: 5 practice questions

Q1. A Lambda function written in Java shows high latency on the first invocation after a period of inactivity but is fast on subsequent invocations. What is the most cost-effective way to reduce this without changing the runtime?
  • A.  Enable Provisioned Concurrency for the function
  • B.  Switch the function to always run inside a VPC
  • C.  Increase the function timeout value
  • D.  Increase the function's memory allocation to speed up initialization
Show answer & explanation
✓ Answer: A. Enable Provisioned Concurrency for the function
Provisioned Concurrency keeps a specified number of execution environments initialized and ready, eliminating cold-start latency for those invocations. Increasing memory can help but doesn't guarantee the environment stays warm, and increasing timeout only affects how long a function is allowed to run, not startup latency.
Q2. A developer wants Lambda functions to reuse a database connection across invocations to avoid the overhead of establishing a new connection each time. Where should the connection object be initialized?
  • A.  Inside the handler function, on every invocation
  • B.  In a Lambda layer's dependency file
  • C.  Outside the handler function, in the global/init scope
  • D.  In an environment variable
Show answer & explanation
✓ Answer: C. Outside the handler function, in the global/init scope
Code outside the handler runs once per execution environment during initialization and persists across warm invocations, so initializing the connection there allows it to be reused. Initializing inside the handler creates a new connection on every invocation, negating the benefit.
Q3. An application performs a DynamoDB Scan operation on a 50 GB table to find items matching a rarely-changing attribute, causing high read capacity consumption. What is the best refactoring to reduce cost and latency?
  • A.  Enable DynamoDB Streams on the table
  • B.  Increase the table's provisioned read capacity units
  • C.  Switch the table to DynamoDB Standard-Infrequent Access
  • D.  Create a Global Secondary Index on the filtered attribute and use Query instead
Show answer & explanation
✓ Answer: D. Create a Global Secondary Index on the filtered attribute and use Query instead
A Scan reads every item in the table, which is expensive on large tables; a Global Secondary Index lets you Query directly on the attribute, reading only matching items. Increasing provisioned capacity just masks the inefficiency at higher cost rather than fixing the access pattern.
Q4. A serverless application writes 500 individual PutItem calls to DynamoDB in a loop, causing throttling. What change would most improve throughput and efficiency?
  • A.  Switch to on-demand capacity mode without changing the write pattern
  • B.  Increase the Lambda function's memory size
  • C.  Add exponential backoff only, keeping single PutItem calls
  • D.  Use BatchWriteItem to group requests into batches of up to 25 items
Show answer & explanation
✓ Answer: D. Use BatchWriteItem to group requests into batches of up to 25 items
BatchWriteItem reduces the number of round trips by grouping up to 25 write requests per call, improving throughput and reducing overhead compared to many single PutItem calls. Backoff alone helps with throttling retries but doesn't reduce the number of requests being made.
Q5. A Lambda function frequently times out because it makes three sequential HTTP calls to independent external APIs before returning a response. What refactoring reduces total execution time?
  • A.  Increase the function's reserved concurrency
  • B.  Split the function into three separate Lambda functions triggered by CloudWatch Events
  • C.  Make the three calls concurrently using asynchronous requests and await all results
  • D.  Increase the Lambda function's timeout to accommodate all three calls
Show answer & explanation
✓ Answer: C. Make the three calls concurrently using asynchronous requests and await all results
Since the three API calls are independent, issuing them concurrently rather than sequentially reduces total wait time to roughly the duration of the slowest call instead of the sum of all three. Increasing timeout or concurrency does not address the root inefficiency of sequential waiting.
Test yourself
See the AWS Developer Associate (DVA-C02) page

More AWS Developer Associate (DVA-C02) topics