पाठ 6 / 32

Execution Environments and Cold Starts

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

Trace the init, invoke and freeze phases of a function instance and explain what a cold start costs and why globals persist.

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

Init, invoke, freeze, reuse

Each function instance runs in an execution environment (on Lambda, a lightweight microVM). Its life has phases. Init: the platform creates the environment, loads your code, starts the runtime and runs your module's top-level code once. Invoke: the handler runs for one event. After it returns, the environment is frozen and may be thawed for a later event; that reuse is a warm start. If no idle environment exists, because traffic rose or the old one was reclaimed, a new one must go through Init: a cold start. It adds latency ranging from tens of milliseconds to seconds, depending on the runtime, the package size and what your init code does. In the standard Lambda model, an environment processes one event at a time, so 50 simultaneous requests need 50 environments. Global variables survive between warm invocations: great for reusing clients, dangerous for request-specific data.

The life of one execution environment

An event arrives; if no warm environment is free, the platform runs Init (cold start). The handler then runs, and the environment is frozen and kept for reuse.

Four connected stages from left to right: an incoming event, the init phase that creates the environment, the handler invocation, and the frozen environment kept for reuse.
Figure 2.1: event -> init (cold start) -> invoke handler -> freeze and reuse

Tracing a cold start and a warm start

Follow which lines run during Init and which run per invocation. The two await handler(...) lines play the role of the platform delivering two events to the same environment.

console.log('INIT: load config, create clients');
let invocations = 0;

export const handler = async (event) => {
  invocations++;
  console.log(`INVOKE #${invocations} for order ${event.orderId}`);
  return { ok: true };
};

await handler({ orderId: 'A1' });
await handler({ orderId: 'B2' });
  1. Line 1: Top-level code runs once, during Init. This is the cold-start work: loading config and creating clients.
  2. Line 2: Module-level state is created once and lives as long as the environment does.
  3. Line 4: Defining the handler does not run it. Init ends here; the environment is ready.
  4. Line 10: The first event (order A1) arrives and the platform calls the handler.
  5. Line 5: The counter increases inside the handler; the global is shared with later invocations.
  6. Line 6: The handler logs and returns. The environment is then frozen, not destroyed.
  7. Line 11: A second event reuses the same environment: a warm start. Line 1 does not run again.
  8. Line 6: The counter, still in memory, reaches 2. This is why globals are a cache, never per-request data.

Never trust a global with per-request data

Putting the current user's ID or a request's cart in a module-level variable works in testing and then leaks between requests in production. Use globals only for things that are safe to share: SDK clients, parsed config, a loaded model.

त्वरित जाँच: Which code runs only during a cold start?

  • The body of the handler function
  • Code inside a `finally` block in the handler
  • Any `console.log` call
  • Top-level module code outside the handler
Answer

Top-level module code outside the handler — Top-level code runs once during Init. The handler body runs on every invocation, warm or cold.