Serverless Warm Pool Pattern
Keep Lambda functions warm by sending periodic ping events to reduce cold start latency for latency-sensitive workloads.
Overview
A cold start occurs when Lambda creates a new execution environment for a function that has no warm instances. The initialization adds latency: downloading code, starting the runtime, loading dependencies. Cold starts typically add 500ms to 5s depending on runtime and package size.
The warm pool pattern sends periodic lightweight ping events to keep execution environments alive. Lambda reuses warm environments for subsequent invocations, avoiding cold start latency. This is useful for latency-sensitive APIs where a 2-second cold start is unacceptable. For a broader set of mitigations beyond scheduled pings, see the AWS Lambda cold start optimization recipe.
AWS also offers provisioned concurrency, which keeps a pre-defined number of environments warm. The warm pool pattern is a lighter-weight alternative: it uses scheduled pings instead of paying for always-warm instances.
When to Use
- Latency-sensitive APIs where cold start latency degrades user experience
- Functions with heavy initialization (large dependencies, or connection pools like the ones in the serverless DB connection pooling pattern)
- You want to reduce cold starts without paying for provisioned concurrency
- Traffic is sporadic: bursts followed by idle periods where environments would otherwise expire
- You need predictable response times for SLA compliance
Skip it when traffic is steady — frequent invocations keep environments warm on their own — or when the runtime’s cold start is already a few hundred milliseconds and users will never notice it.
Solution
Strategy 1: EventBridge Scheduler Ping (Python CDK)
from aws_cdk import (
Stack,
Duration,
aws_lambda as lambda_,
aws_events as events,
aws_events_targets as targets,
)
from constructs import Construct
class WarmPoolStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs):
super().__init__(scope, construct_id, **kwargs)
# The function to keep warm
api_function = lambda_.Function(self, "ApiFunction",
runtime=lambda_.Runtime.PYTHON_3_12,
handler="api.handler",
code=lambda_.Code.from_asset("lambda/api"),
timeout=Duration.seconds(30),
reserved_concurrent_executions=5, # Limit warm pool size
)
# Warmup function that sends pings
warmup_function = lambda_.Function(self, "WarmupFunction",
runtime=lambda_.Runtime.PYTHON_3_12,
handler="warmup.handler",
code=lambda_.Code.from_asset("lambda/warmup"),
timeout=Duration.seconds(30),
)
# Grant warmup function permission to invoke API function
api_function.grant_invoke(warmup_function)
# Schedule warmup every 5 minutes
warmup_rule = events.Rule(self, "WarmupSchedule",
schedule=events.Schedule.rate(Duration.minutes(5)),
)
warmup_rule.add_target(targets.LambdaFunction(warmup_function))
# Pass the API function name as environment variable
warmup_function.add_environment("TARGET_FUNCTION", api_function.function_name)
warmup_function.add_environment("WARM_COUNT", "5")
Warmup Lambda Handler (Python)
import boto3
import json
import os
lambda_client = boto3.client('lambda')
TARGET_FUNCTION = os.environ['TARGET_FUNCTION']
WARM_COUNT = int(os.environ.get('WARM_COUNT', '5'))
def handler(event, context):
# Send concurrent ping invocations to warm multiple environments
import concurrent.futures
def send_ping(instance_id: int) -> dict:
response = lambda_client.invoke(
FunctionName=TARGET_FUNCTION,
InvocationType='Event', # Async invocation
Payload=json.dumps({
"source": "warmup.ping",
"instanceId": instance_id,
"isWarmup": True,
}),
)
return {"instanceId": instance_id, "status": response['StatusCode']}
with concurrent.futures.ThreadPoolExecutor(max_workers=WARM_COUNT) as executor:
results = list(executor.map(send_ping, range(WARM_COUNT)))
return {
"warmedInstances": len(results),
"targetFunction": TARGET_FUNCTION,
}
API Function with Warmup Detection (Python)
import json
def handler(event, context):
# Detect warmup ping and return immediately
if isinstance(event, dict) and event.get("isWarmup"):
return {"status": "warm", "instanceId": event.get("instanceId")}
# Real request processing
http_method = event.get("httpMethod", "GET")
path = event.get("path", "/")
# Heavy initialization happens here (loaded once, reused for warm invocations)
# Dependencies, DB connections, etc.
return {
"statusCode": 200,
"body": json.dumps({"message": "Request processed", "path": path}),
}
Strategy 2: Provisioned Concurrency (CDK)
from aws_cdk import (
Stack,
Duration,
aws_lambda as lambda_,
)
from constructs import Construct
class ProvisionedConcurrencyStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs):
super().__init__(scope, construct_id, **kwargs)
# Lambda with alias and provisioned concurrency
function = lambda_.Function(self, "ApiFunction",
runtime=lambda_.Runtime.PYTHON_3_12,
handler="api.handler",
code=lambda_.Code.from_asset("lambda/api"),
timeout=Duration.seconds(30),
)
# Create an alias for the current version
alias = lambda_.Alias(self, "LiveAlias",
alias_name="live",
version=function.current_version,
provisioned_concurrent_executions=10, # Always 10 warm instances
)
TypeScript Warmup Handler
import { LambdaClient, InvokeCommand } from '@aws-sdk/client-lambda';
const lambdaClient = new LambdaClient({ region: 'us-east-1' });
const TARGET_FUNCTION = process.env.TARGET_FUNCTION!;
const WARM_COUNT = parseInt(process.env.WARM_COUNT || '5');
export const handler = async (): Promise<{ warmedInstances: number }> => {
const promises = Array.from({ length: WARM_COUNT }, (_, i) =>
lambdaClient.send(new InvokeCommand({
FunctionName: TARGET_FUNCTION,
InvocationType: 'Event', // Async
Payload: JSON.stringify({
source: 'warmup.ping',
instanceId: i,
isWarmup: true,
}),
}))
);
const results = await Promise.all(promises);
return {
warmedInstances: results.length,
};
};
API Handler with Warmup Check (TypeScript)
interface WarmupEvent {
source: string;
instanceId: number;
isWarmup: boolean;
}
interface ApiEvent {
httpMethod: string;
path: string;
body?: string;
}
type HandlerResult =
| { status: string; instanceId: number }
| { statusCode: number; body: string };
export const handler = async (event: WarmupEvent | ApiEvent): Promise<HandlerResult> => {
// Detect warmup ping
if ('isWarmup' in event && event.isWarmup) {
return { status: 'warm', instanceId: event.instanceId };
}
// Real request processing
const { httpMethod, path } = event as ApiEvent;
return {
statusCode: 200,
body: JSON.stringify({ message: 'Request processed', path }),
};
};
Explanation
Lambda reuses execution environments between invocations. After an invocation completes, the environment stays warm for a few minutes (typically 5-15 minutes, varies). If a new invocation arrives within that window, Lambda reuses the warm environment, skipping initialization.
The warm pool pattern exploits this by sending periodic pings. Each ping invokes the function with a special payload (isWarmup: true). The function detects the warmup event and returns immediately without doing real work. This keeps the environment alive.
The number of concurrent pings determines the warm pool size. If you send 5 concurrent pings, Lambda creates 5 warm environments. Subsequent real requests are distributed across these warm environments.
Provisioned concurrency is the managed alternative. AWS keeps a specified number of environments always warm, regardless of traffic. It costs more but provides guaranteed warm capacity without the need for ping events.
Two other options cover the same ground and are worth knowing before you build anything. Lambda SnapStart snapshots a pre-initialized environment when you publish a function version and restores from it on cold start — it removes most of the init cost with no recurring pings, on the runtimes that support it (Java first, and newer Python and .NET versions). And if you already deploy with the Serverless Framework, the community serverless-plugin-warmup packages this exact ping approach as a plugin, so you do not have to hand-roll the scheduler and warmup function.
Variants
| Approach | Mechanism | Cost | Best For |
|---|---|---|---|
| EventBridge ping | Scheduled Lambda invokes target | Low (only invocation cost) | Sporadic traffic, cost-sensitive |
| Provisioned concurrency | AWS-managed warm instances | Higher (pay for reserved) | Predictable traffic, SLA-bound |
| Lambda SnapStart | Pre-initialized snapshot restored on cold start | Low (per-restore fee) | Heavy-init runtimes that support it |
| serverless-plugin-warmup | Plugin creates the scheduled warmer for you | Low (only invocation cost) | Teams already on Serverless Framework |
| CloudWatch alarm ping | Alarm triggers warmup on traffic spike | Low | Reactive warmup before burst |
| Self-ping (recursive) | Function schedules its next ping | Low | Single-function warmup |
| API Gateway warmup | Endpoint that triggers warmup on first request | Low | On-demand warmup |
Best Practices
- Set the ping interval to 3-5 minutes — Lambda environments expire after 5-15 minutes of inactivity. Ping every 5 minutes to keep environments warm. Pinging more frequently wastes invocations.
- Use async invocations for pings — set
InvocationType: 'Event'so the warmup function does not wait for the target to respond. This reduces warmup function execution time and cost. - Detect and skip warmup events in the target — check for
isWarmup: trueat the top of the handler and return immediately. Do not execute business logic for warmup pings. - Match warm count to expected concurrency — if your API typically handles 10 concurrent requests, warm 10 instances. Warming more than needed wastes money; warming fewer leaves cold starts.
- Consider provisioned concurrency for production — provisioned concurrency is more reliable than pings. If cold starts are unacceptable, pay for provisioned concurrency and use pings as a fallback.
Common Mistakes
- Pinging too frequently — pinging every minute wastes invocations. Lambda environments stay warm for several minutes. Ping every 5 minutes.
- Not detecting warmup events — if the target function processes warmup pings as real requests, it may trigger side effects (database writes, API calls). Always check for
isWarmupfirst. - Warming only one instance — a single ping keeps one environment warm. If traffic requires 10 concurrent instances, send 10 concurrent pings.
- Using warm pool for low-traffic functions — if the function is rarely invoked, the warmup cost exceeds the cold start cost. Only warm functions with frequent, latency-sensitive traffic.
- Ignoring provisioned concurrency — for production APIs with strict SLAs, provisioned concurrency is more reliable. Pings can miss the window if traffic arrives between pings.
Troubleshooting
- Pings fire but cold starts persist: the ping interval is longer than the environment’s idle lifetime, or real traffic needs more environments than
WARM_COUNTprovides. Shorten the schedule to 3 minutes and raise the concurrent ping count to match observed peak concurrency. - Warmup pings run the real handler: the target is not seeing the
isWarmupflag — an event source like API Gateway or an SNS envelope can reshape the payload. Log the raw event once and confirm the flag survives your trigger’s envelope. - Warm pool works but p99 still spikes: bursts above the pool size still cold-start. Size
WARM_COUNTto the burst, not the average, or move the strict portion of traffic onto provisioned concurrency. - Warmup function times out: sequential invokes serialise the wait. Use
InvocationType: 'Event'so calls are async, and fan them out with a thread pool orPromise.allas in the examples above. - Invocation cost higher than expected: a 1-minute schedule triples the invoke count of a 5-minute one for no benefit. Also check that failed pings are not being retried into extra billable invocations.
Production Notes
- The cost is small but not zero: 5 pings every 5 minutes is about 1,440 invocations a day per function — negligible for one function, but multiply it before rolling the pattern out across a fleet.
- Alarm on the warmup function itself: if the scheduler rule or the
lambda:InvokeFunctionpermission breaks, the pool quietly goes cold and cold-start latency returns without an error anywhere in the API’s own metrics. - Measure before and after: compare the share of invocations reporting init duration in Lambda REPORT logs, and watch p50/p99 — the effect of warming should be visible in numbers, not assumed.
- Revisit the choice quarterly: AWS keeps moving this space (SnapStart, managed instances, changing idle lifetimes). When a managed option covers your case for less, retire the ping loop instead of letting it run forever.
Key Takeaways
- The warm pool is a scheduler plus a flag: a 5-minute EventBridge rule and an
isWarmupearly return are the entire mechanism. - Size the pool to concurrent requests, not request volume — one ping keeps exactly one environment warm.
- Warming reduces cold starts; it does not eliminate them. Strict SLAs still belong on provisioned concurrency or SnapStart.
- Every warmup ping must short-circuit before business logic, or the pattern causes real side effects like duplicate database writes.
Common Production Pitfalls
- Keeping the ping schedule running after traffic stabilizes — steady traffic keeps environments warm on its own, so the scheduler becomes pure waste.
- Counting warmup invokes in latency and error dashboards — tag
isWarmupinvocations so p99 and error-rate alarms reflect real traffic only. - Letting the warmup and API functions share one concurrency budget — a burst of pings can throttle real requests if reserved concurrency is set on the target.
- Rolling the pattern out to dozens of functions at once — the per-function cost is small, but the fleet-wide invoke count and the extra scheduler and permission surface are not.
- Missing the IAM edge — the warmup function needs
lambda:InvokeFunctionon the specific target ARNs, or every ping fails silently. - Treating warm state as a cache — anything initialized in a warm environment can outlive a deployment, so keep versioned state out of globals.
See Also
- AWS Lambda execution environment lifecycle — how AWS creates, freezes, and reuses environments.
- Lambda provisioned concurrency — the managed alternative to scheduled pings.
- Improving startup performance with Lambda SnapStart — snapshot-based cold start mitigation.
- serverless-plugin-warmup — the ping pattern packaged as a Serverless Framework plugin.
- Companion repo — the CDK stack and the warmup/API handlers from this pattern as a runnable project.
Frequently Asked Questions
How long does a Lambda environment stay warm?
Lambda does not document the exact duration. It varies from 5 to 15 minutes depending on region, runtime, and AWS capacity. In practice, pinging every 5 minutes keeps environments warm reliably.
What is the difference between warm pool and provisioned concurrency?
Warm pool sends periodic pings to keep environments alive between traffic bursts. Provisioned concurrency keeps a fixed number of environments always warm, managed by AWS. Provisioned concurrency is more reliable but costs more. Warm pool is cheaper but not guaranteed.
Should I use warm pool, provisioned concurrency, or SnapStart?
Try SnapStart first if your runtime supports it — it removes most of the init cost with no recurring invocations. Use provisioned concurrency when you need guaranteed warm capacity on a runtime SnapStart does not cover. Reach for the warm pool when you want the cheapest mitigation and can tolerate the occasional cold start when a ping misses the window.
Does warming work for all runtimes?
Yes, but the benefit varies. Java and .NET have the longest cold starts (2-10 seconds) and benefit most. Python and Node.js have shorter cold starts (200-500ms) and benefit less. For Java, consider SnapStart or provisioned concurrency instead of pings.
Can I eliminate cold starts entirely?
No. Warm pool and provisioned concurrency reduce cold starts but do not eliminate them. If traffic exceeds the warm pool size, new environments are created with cold starts. Scale the warm count or provisioned concurrency to match expected peak concurrency.
How much does the warm pool pattern cost?
Each ping is one Lambda invocation. At 5-minute intervals with 5 concurrent pings, that is 5 invocations every 5 minutes = 1,440 invocations per day. At $0.20 per million invocations, the cost is negligible. The main cost is the compute time for each ping invocation.
Related Resources
Serverless Throttling Pattern
Handle backpressure in serverless using SQS, token buckets, and Lambda concurrency limits to protect downstream services from burst traffic.
PatternServerless Function Composition Pattern
Chain serverless functions via Step Functions or orchestration layers to build multi-step workflows with retries, branching, and state management.
RecipeReduce AWS Lambda Cold Start with Provisioned Concurrency
Minimize Lambda cold start latency using provisioned concurrency, ARM64 Graviton, lighter dependencies, and initialization code optimization.
PatternServerless DB Connection Pooling Pattern
Manage database connections across serverless invocations by using external connection poolers, connection reuse, and lightweight clients to avoid connection exhaustion.