Lesson 32 / 32

Mixed Self-Check

Test yourself on scenarios that mix lifecycle, state, edge concurrency, caching and failure handling, then fix what you missed.

By · 15 years in web development

Five quick scenarios to reason through

Answer each before reading the hint. (a) Your thumbnail function's bill jumped tenfold overnight. Hint: does it write into the prefix that triggers it? (b) Users are sometimes charged twice after a timeout. Hint: at-least-once delivery without an idempotency key. (c) A logged-in dashboard showed another user's name. Hint: a shared cache stored a response that should have been private. (d) p50 latency is 40 ms but p99 is 2 s, only after quiet periods. Hint: cold starts; check Init Duration. (e) An SQS consumer reprocesses the same nine good messages whenever one bad message appears. Hint: partial batch failure reporting is off. If any hint surprised you, reread the matching section: (a) Section 3, (b) Section 4, (c) Section 6, (d) Sections 2 and 9, (e) Section 3. Then try the code below, which is a bug specific to edge runtimes.

Spot the bug: a global on a busy isolate

An isolate can serve several requests concurrently. While request one awaits its database call, request two overwrites the shared global, and request one is audited as the wrong user.

let currentUser; // module scope: shared by every request this isolate serves

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function handle(userId, dbDelayMs) {
  currentUser = userId;
  await sleep(dbDelayMs); // pretend database call
  return `profile for ${userId}, audited as ${currentUser}`;
}

// Two requests overlap inside the same isolate.
const results = await Promise.all([handle('asha', 50), handle('vikram', 10)]);
console.log(results.join('\n'));

Output:

profile for asha, audited as vikram
profile for vikram, audited as vikram

The fix

Keep request data in local variables or pass it as arguments: here, delete the currentUser global and use the userId parameter directly. Globals on edge runtimes are shared by concurrent requests, and on Lambda they are shared across sequential ones, so either way they must never hold per-request state.

Quick check: A Lambda function behind API Gateway takes 200 ms when warm but sometimes 3 s after a quiet hour. Logs show an Init Duration on the slow calls. What is the most targeted fix if those slow calls are unacceptable?

  • Shrink the bundle and init work, and add provisioned concurrency if it is still too slow
  • Add a dead-letter queue
  • Set `Cache-Control: private` on the response
  • Reduce the function's reserved concurrency to 1
Answer

Shrink the bundle and init work, and add provisioned concurrency if it is still too slow — Init Duration marks cold starts. Smaller packages and lighter init reduce them; provisioned concurrency removes them for the configured capacity. A DLQ, caching headers and lower concurrency do not address it.