पाठ 29 / 32

Common Pitfalls and How to Avoid Them

यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।

Recognise the recurring serverless failure modes, from recursive triggers to timeout chains and lock-in, and design them out.

लेखक: · 15 साल से वेब डेवलपर

The mistakes that hurt in production

A short list covers most serverless incidents. Recursive loops: a function triggered by a bucket or queue it also writes to. Timeout chains: the front door gives up before the function, which gives up before its database call; set timeouts from the inside out, each shorter than its caller's. Unbounded scale: a burst creates thousands of instances that exhaust database connections or a partner's rate limit. Ignoring duplicates: at-least-once delivery without idempotency double-charges users. Silent drops: asynchronous failures with no DLQ simply vanish. Granularity mistakes: one giant function with every dependency has slow cold starts and broad permissions, while a separate function for every tiny piece of logic becomes a deployment and debugging maze. Lock-in surprises: event shapes and bindings are provider-specific, so isolate them in a thin adapter around portable business logic. Each of these is cheap to prevent at design time and expensive to find later.

Stopping before the timeout hits

The handler checks the remaining time before each job and hands unfinished work back (for example to a queue) instead of being killed halfway through. The fake context loses 1.5 seconds per check.

// Stop starting new work when the invocation is about to time out.
export const handler = async (event, context) => {
  const done = [];
  for (const job of event.jobs) {
    if (context.getRemainingTimeInMillis() < 2000) {
      return { done, requeue: event.jobs.slice(done.length) }; // hand the rest back
    }
    done.push(job);
  }
  return { done, requeue: [] };
};

// Fake context whose clock drops 1500 ms per check.
let left = 6000;
const ctx = { getRemainingTimeInMillis: () => (left -= 1500) };
console.log(await handler({ jobs: ['j1', 'j2', 'j3', 'j4', 'j5'] }, ctx));

Output:

{ done: [ 'j1', 'j2' ], requeue: [ 'j3', 'j4', 'j5' ] }

Set timeouts from the inside out

Example: database query timeout 3 s, function timeout 5 s, API gateway timeout 10 s, client timeout 12 s. Each layer can then report a clean error before the layer above gives up, instead of everyone timing out at once with no useful message.

The leaking overhead tank

An overflowing tank on the roof that refills itself from the same pipe it drains into never stops, and the electricity bill shows it. A recursive trigger is that tank. Fix the plumbing at design time; do not wait for the bill.

त्वरित जाँच: An API gateway times out at 29 s, the function at 60 s, and its database call has no timeout. What is the main problem?

  • The database call is too fast for the function
  • Nothing; longer inner timeouts are always safer
  • The client may get a gateway timeout while the function keeps running and billing with no one waiting
  • The function will be retried automatically by the gateway
Answer

The client may get a gateway timeout while the function keeps running and billing with no one waiting — Inner timeouts should be shorter than outer ones. Otherwise work continues after the caller has given up, wasting money and hiding the real error.