Explain cold starts, warm execution reuse, package size effects, dependency loading, and runtime choices. This section should connect architecture decisions to real latency.
Cold starts happen when a platform has to prepare a fresh execution environment before your function can handle a request or event. That preparation may include container or runtime startup, dependency loading, code initialization, secret retrieval, and network client setup. Warm reuse can hide that work for later invocations, but the first-hit latency is still real on bursty or low-traffic paths.
Cold starts matter most when the function sits on a user-facing request path or when many new instances are created at once during traffic growth. The important lesson is that cold starts are not just a platform quirk. They are influenced by architectural choices such as package size, dependency shape, runtime behavior, and how much work is done during initialization.
flowchart LR
A["Invocation arrives"] --> B{"Warm environment exists?"}
B -->|Yes| C["Run handler"]
B -->|No| D["Provision runtime"]
D --> E["Load code and dependencies"]
E --> F["Initialize clients and config"]
F --> C
What to notice:
Cold starts tend to grow when a function:
This is why function boundary design and dependency discipline matter. A function with one clear purpose usually initializes less than one catch-all handler.
1let dbClientPromise: Promise<DatabaseClient> | undefined;
2
3function getDbClient() {
4 if (!dbClientPromise) {
5 dbClientPromise = createDatabaseClient({
6 endpoint: process.env.DB_ENDPOINT!,
7 });
8 }
9 return dbClientPromise;
10}
11
12export async function handler(event: { userId: string }) {
13 const db = await getDbClient();
14 return db.queryOne("select * from users where id = ?", [event.userId]);
15}
What this demonstrates:
The strongest cold-start optimizations usually come from:
A common anti-pattern is to chase platform-specific tricks before fixing the obvious architectural waste inside the function.
1startup_review:
2 package_size_mb: 8
3 eager_clients:
4 - database
5 - object_storage
6 lazy_load_candidates:
7 - image_processing_library
8 - pdf_renderer
9 user_facing: true
Teams often over-focus on cold starts because they are easy to talk about. But a warm function with chatty dependency calls can still be slower than a colder function with a cleaner data path. Cold-start work should be optimized, not mythologized. The real question is which part of the latency budget matters most for the workload.
A low-traffic admin API sees occasional two-second latency spikes, and the team assumes the only fix is to keep instances permanently warm. What should be challenged first?
The stronger answer is the initialization profile. Before paying to keep capacity warm, the team should inspect package size, startup work, secret loading, and dependency client initialization. Cold-start mitigation is often strongest when waste is removed first and warm-capacity tactics are treated as a last-mile choice.