Where does cold start time go in a Java Lambda?

A Java cold start is a series of phases, and each one is paid for in CPU time. Lambda allocates CPU in proportion to configured memory, so a low memory setting slows every phase below.

  • Runtime and JVM startup. Lambda creates the execution environment, starts the JVM, and bootstraps the runtime.
  • Class loading. The JVM loads, verifies, and links classes as code first touches them. Large dependency trees such as AWS SDKs, JSON libraries, and ORMs make this phase grow.
  • Dependency injection and framework init. Spring-style containers scan the classpath, build bean graphs, and often use reflection. This is usually the largest single phase.
  • First-request JIT warm-up. Code that has just loaded runs in the interpreter until the JIT compiles hot paths. The first few requests are slower even after init has finished.
Phase Provisioned concurrency SnapStart
JVM/runtime startup Removed (done ahead of time) Replaced by snapshot restore
Class loading Removed Removed for classes loaded before the snapshot
DI/framework init Removed Removed (captured in snapshot)
JIT warm-up Remains unless warmed during init Remains unless primed before the snapshot

The two features solve different problems. Provisioned concurrency keeps initialized environments running, so you pay to keep them idle. SnapStart makes each new environment cheaper to create, but each one still pays restore time.

Illustrative estimate (assumed numbers, not a benchmark): Cold latency = JVM + class loading + framework + JIT penalty. With assumed values of 300 + 900 + 1,800 + 400 ms, the total is 3,400 ms. SnapStart replaces the first three terms with restore time. Assuming 400 ms for restore, cold latency becomes 400 + 400 = 800 ms.

 JAVA LAMBDA COLD START: PHASES IN ORDER

 +-------------+   +-------------+   +-------------+   +-------------+
 | JVM/runtime |-->|   Class     |-->| DI/framework|-->| First-req   |
 |  startup    |   |  loading    |   |    init     |   | JIT warm-up |
 +-------------+   +-------------+   +-------------+   +-------------+
 |<----------------- init phase ------------------>|   |<- invoke -->|

 PROVISIONED CONCURRENCY: envs pre-initialized and kept warm (pay idle)
 +-------------------------------------------------+   +-------------+
 |          done before traffic: REMOVED           |-->|  REMAINS*   |
 +-------------------------------------------------+   +-------------+

 SNAPSTART: snapshot taken after init, restored for each new env
 +-------------------------------------------------+   +-------------+
 |  replaced by SNAPSHOT RESTORE (paid each env)   |-->|  REMAINS**  |
 +-------------------------------------------------+   +-------------+

 *  unless warmed during init      ** unless primed before the snapshot

 +--------+      +-----------+      +----------------------------------+
 | Memory |----->| CPU share |----->| speed of every phase shown above |
 +--------+      +-----------+      +----------------------------------+

Provisioned concurrency moves the JVM, class loading and framework init phases ahead of traffic. SnapStart swaps those phases for a per-environment snapshot restore. Both leave JIT warm-up unless code is warmed or primed, and memory size sets the CPU speed of every phase.

How do provisioned concurrency and SnapStart work and how are they billed?

Provisioned concurrency and SnapStart attack that latency in different ways, and you pay for them in different ways.

How it works in the request path

Every invocation goes to an execution environment, which is an isolated microVM running your runtime and code. If no idle environment is available for the version or alias being invoked, Lambda must create one, and that creation is the cold start. The two features change what happens at that point.

  • Provisioned concurrency changes when environments are created. When you configure it on an alias or version, Lambda creates that many environments in advance and runs your full init phase in each. Incoming invocations are routed to these pre-initialized environments first. Only when all of them are busy does Lambda create on-demand environments, which go through the normal cold start.
  • SnapStart changes how environments are created. When you publish a version, Lambda runs init once, takes a snapshot of the initialized microVM's memory and disk state, and caches it. When a new environment is needed for that version, Lambda restores it from the snapshot, runs any registered after-restore hooks, and then hands the request to your handler. Init code does not run again in restored environments.

In short, provisioned concurrency removes the cold start by doing the work early and keeping the result running. SnapStart shortens the cold start by doing the work once and copying the result.

Provisioned concurrency

Provisioned concurrency keeps a set number of execution environments initialized and waiting. Your init code runs ahead of time, so a request routed to one of these environments skips the cold start completely. You are billed for the configured concurrency, multiplied by memory, for every second it is enabled. That charge applies whether the environments serve traffic or sit idle. Invocations that land on provisioned environments are billed separately at a duration rate distinct from on-demand pricing. Requests above the configured level spill over to on-demand environments, and those requests pay the full cold start again. The trade-off is that you pay for capacity in exchange for latency that holds up to a ceiling you choose.

SnapStart

SnapStart moves initialization to the moment you publish a version. Lambda runs your init phase once, snapshots the memory and disk state of the initialized environment, and caches the snapshot. Cold starts then restore from that snapshot instead of re-running init. There is no idle capacity charge. Restore is not free, though. Each cold start still pays restore time. The first few invocations after a restore are often slower because JIT-compiled code, lazily loaded classes and network connections are not yet warm. Depending on the runtime, there may also be charges for snapshot caching and restore.

Aspect Provisioned concurrency SnapStart
When init runs Ahead of traffic, per environment Once, at version publish
Idle cost Billed while enabled None for idle capacity
Cold start behaviour None up to configured level; full cold start above it Restore overhead on every cold start
First-invocation latency Warm Can be slower after restore
Applies to Published versions or aliases Published versions or aliases

Neither option applies to $LATEST. Both require a published version or an alias pointing to one. Runtime support, architecture support, compatibility with other Lambda features and current pricing all change over time, so check the current AWS documentation before you commit to either option.

How much does provisioned concurrency vs SnapStart cost for a real traffic pattern?

Before you price anything, fix the workload. The numbers below are invented to show cost shape. Replace every price variable with the current figure for your region from the Lambda pricing page.

Illustrative estimate (assumed numbers, not a benchmark)

  • Java function, 2 GB memory, 200 ms average duration
  • Diurnal curve: 10 req/s overnight, 50 req/s at peak, 25 req/s average
  • 30-day month = 2,592,000 s, so 64.8M requests/month
  • Concurrency ≈ request rate × duration: about 2 at baseline and 10 at peak

Invoked compute = 64.8M × 0.2 s × 2 GB = 25.92M GB-s.

Provisioned compute = instances × 2 GB × 2,592,000 s. That is 51.84M GB-s when sized to peak and 10.37M GB-s when sized to baseline.

Option Monthly cost formula Cold starts
On-demand 64.8M × P_req + 25.92M × P_gbs On every scale-out
PC sized to peak (10) 64.8M × P_req + 51.84M × P_pc + 25.92M × P_pc_dur Only above 10 concurrent
PC sized to baseline (2) 64.8M × P_req + 10.37M × P_pc + (PC-served GB-s × P_pc_dur) + (spillover GB-s × P_gbs) Above 2 concurrent, which covers most of the daytime ramp
SnapStart On-demand formula + P_snap charges, if any apply to your runtime Present but shorter, because they restore from a snapshot

Sizing to peak reserves twice the GB-s the function actually uses. That idle reservation is the cost you pay to remove cold starts. Sizing to baseline cuts the reservation by about 80% but puts the cold starts back exactly when traffic rises.

The shape also depends on the workload. A flatter curve, longer durations or different price ratios can reverse the ranking, so run the formulas with your own traffic data before you decide.

When does provisioned concurrency pay off, and how do you calculate break-even utilization?

Because the provisioned charge accrues whether or not requests arrive, its cost per invocation depends on how much of the configured capacity you actually use. A simple model makes the relationship explicit. Let C be the provisioned rate per GB-second of configured capacity, d the reduced duration rate you pay while provisioned environments execute, D the on-demand duration rate, and U utilization (busy GB-seconds divided by configured GB-seconds). Each busy GB-second then costs C/U + d. On-demand costs D. Provisioned concurrency is cheaper on compute alone when U > C / (D − d). Plug in current prices for your region and architecture rather than a remembered rule of thumb. This calculation ignores latency entirely, so treat it as the floor of the decision, not the whole of it.

Measure U instead of estimating it. In CloudWatch, the ProvisionedConcurrencyUtilization metric on the alias or version gives the ratio directly. You can also divide ProvisionedConcurrentExecutions by the configured value. Use the average statistic over hourly periods across at least a full weekly cycle, because a daily mean hides a quiet night that drags the true figure down.

Provisioned concurrency fits steady, high-volume traffic with a measured latency requirement that SnapStart or on-demand cannot meet. Avoid it for spiky, low-volume, or overnight-quiet workloads, where idle hours outweigh the cold starts you remove. Also watch ProvisionedConcurrencySpilloverInvocations. If capacity is sized to the average, peak requests overflow to on-demand environments and still hit cold starts, so you pay for capacity and still miss the latency target.

from datetime import datetime, timedelta, timezone
import boto3

FUNCTION, ALIAS = "orders-api", "live"  # placeholder names
C = 0.0000041667   # provisioned concurrency rate (configured capacity)
d = 0.0000097222   # duration rate while provisioned environments execute
D = 0.0000166667   # on-demand duration rate

cw = boto3.client("cloudwatch")
end = datetime.now(timezone.utc)
start = end - timedelta(days=7)  # one full weekly cycle, so quiet nights and weekends count
dims = [{"Name": "FunctionName", "Value": FUNCTION},
        {"Name": "Resource", "Value": f"{FUNCTION}:{ALIAS}"}]

def hourly(metric, stat):
    resp = cw.get_metric_statistics(
        Namespace="AWS/Lambda", MetricName=metric, Dimensions=dims,
        StartTime=start, EndTime=end, Period=3600, Statistics=[stat])
    return [p[stat] for p in resp["Datapoints"]]

util = hourly("ProvisionedConcurrencyUtilization", "Average")
U = sum(util) / 168 if util else 0.0

spill = sum(hourly("ProvisionedConcurrencySpilloverInvocations", "Sum"))

break_even = C / (D - d)
print(f"Weekly utilization U = {U:.1%}, break-even = {break_even:.1%}")
print("Cheaper on compute alone" if U > break_even else "On-demand is cheaper on compute")
print(f"Spillover invocations this week: {int(spill)}")

A boto3 script that computes a week of hourly provisioned concurrency utilization for an alias, compares it against the C / (D − d) break-even threshold using placeholder rates, and reports spillover invocations that indicate undersized capacity.

How can you reduce provisioned concurrency cost with Application Auto Scaling?

Provisioned concurrency is billed per allocated environment for every second it exists, whether or not it serves requests. Lowering the allocation when traffic is low is the most direct way to cut that cost. Application Auto Scaling can do this on a published version or alias. You register the scalable target with the lambda service namespace, the lambda:function:ProvisionedConcurrency dimension, and a resource ID of the form function:<name>:<alias>, with minimum and maximum capacity. You then attach one or both policy types, using the CLI (register-scalable-target, put-scheduled-action, put-scaling-policy) or the equivalent infrastructure-as-code resources.

Scheduled scaling

A scheduled action changes minimum and maximum capacity at fixed times, for example raising the floor before business hours and dropping it overnight. This works when load follows the clock. It fails when traffic arrives off-schedule: a batch job runs late, a marketing push lands at midnight, or a time-zone shift moves the peak. The schedule cannot detect any of these, so the extra requests spill over to on-demand environments and hit cold starts.

Target tracking

A target-tracking policy on the LambdaProvisionedConcurrencyUtilization metric adds or removes environments to hold a utilization target, often around 0.7. This policy reacts to load, but it reacts late. CloudWatch alarms must see a sustained breach before scale-out begins, and each new environment then has to initialize before it can serve traffic. A fast ramp outruns this loop, and a sudden spike is over before new capacity is ready.

A common pattern is to use scheduled actions to set a predictable floor and let target tracking handle moderate drift above it. Either way, auto scaling saves money by running less headroom, and less headroom means more spillover risk. Watch spillover invocations after you enable it, and raise the floor if they climb.

RID="function:orders-api:live"
DIM="lambda:function:ProvisionedConcurrency"

aws application-autoscaling register-scalable-target \
  --service-namespace lambda --resource-id "$RID" --scalable-dimension "$DIM" \
  --min-capacity 5 --max-capacity 100

aws application-autoscaling put-scheduled-action \
  --service-namespace lambda --resource-id "$RID" --scalable-dimension "$DIM" \
  --scheduled-action-name weekday-morning-floor \
  --schedule "cron(30 7 ? * MON-FRI *)" --timezone "America/New_York" \
  --scalable-target-action MinCapacity=40,MaxCapacity=100

aws application-autoscaling put-scheduled-action \
  --service-namespace lambda --resource-id "$RID" --scalable-dimension "$DIM" \
  --scheduled-action-name nightly-floor \
  --schedule "cron(0 20 ? * * *)" --timezone "America/New_York" \
  --scalable-target-action MinCapacity=5,MaxCapacity=100

aws application-autoscaling put-scaling-policy \
  --service-namespace lambda --resource-id "$RID" --scalable-dimension "$DIM" \
  --policy-name pc-utilization-70 --policy-type TargetTrackingScaling \
  --target-tracking-scaling-policy-configuration '{
    "TargetValue": 0.7,
    "PredefinedMetricSpecification": {
      "PredefinedMetricType": "LambdaProvisionedConcurrencyUtilization"
    }
  }'

AWS CLI commands that register a Lambda alias's provisioned concurrency with Application Auto Scaling, use scheduled actions to set a time-based floor, and add a target-tracking policy at 0.7 utilization to handle moderate drift above that floor.

How do you implement SnapStart safely in a Java Lambda?

SnapStart applies only to published function versions, never to $LATEST. Enabling it on the function configuration is therefore not enough: the deployment pipeline must publish a version on every release and point an alias at it, and callers and event sources must invoke the alias. Publishing now runs your initialization code and takes the snapshot, so publishes are noticeably slower. Rollbacks change too. Moving the alias back to a previously published version is fast because that snapshot already exists, but rebuilding and republishing means waiting for initialization and snapshotting again. Keep old versions around long enough to roll back to them.

The correctness hazards all come from one fact: anything computed during initialization is frozen and copied into every execution environment restored from that snapshot.

  • Random seeds and generated IDs. A seeded PRNG, or an ID minted at init, produces identical sequences or values across environments.
  • Cached credentials and tokens. These can be near expiry, or already expired, when a restore happens hours later.
  • Network connections. Sockets opened before the snapshot are dead after restore.
  • Time-dependent state. Startup timestamps, cache TTLs, and computed deadlines reflect publish time, not restore time.

CRaC-style runtime hooks address this. Register a resource whose beforeCheckpoint closes connections and clears credentials, and whose afterRestore re-seeds randomness, reopens connections, and resets clocks.

These bugs rarely show up locally. A single process never restores from a snapshot. Duplicates only appear when many environments restore from the same snapshot at the same moment, which is exactly what happens during a concurrency spike.

Once the hooks are in place, move more expensive work into initialization, such as class loading, dependency injection wiring, and JIT warm-up paths. Every piece of work done there is captured once in the snapshot instead of being repeated on each cold start.

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.net.http.HttpClient;
import java.security.SecureRandom;
import java.time.Instant;
import org.crac.Core;
import org.crac.Resource;

public class OrderHandler implements RequestHandler<String, String>, Resource {
    private HttpClient http;          // pooled connections must not survive the snapshot
    private SecureRandom random;      // per-environment randomness
    private volatile String token;    // cached credential, fetched lazily
    private Instant startedAt;        // time-dependent state

    public OrderHandler() {
        http = HttpClient.newHttpClient();
        random = new SecureRandom();
        startedAt = Instant.now();
        Core.getGlobalContext().register(this); // runs at publish, before the snapshot
    }

    @Override
    public void beforeCheckpoint(org.crac.Context<? extends Resource> ctx) {
        http = null;   // drop the client so no pooled sockets get frozen into the snapshot
        token = null;  // never ship a credential that may be expired at restore time
    }

    @Override
    public void afterRestore(org.crac.Context<? extends Resource> ctx) {
        random = new SecureRandom();      // fresh instance per restored environment
        http = HttpClient.newHttpClient(); // new connections from this environment
        startedAt = Instant.now();         // clock reflects restore time, not publish time
    }

    @Override
    public String handleRequest(String input, Context context) {
        if (token == null) token = "placeholder-token"; // fetch real credential here
        long requestId = random.nextLong(); // unique across environments after restore
        return "order-" + Long.toHexString(requestId);
    }
}

A Java Lambda handler registers a CRaC Resource that drops connections and credentials before the snapshot and re-creates randomness, clients and timestamps after each restore.

Who should use provisioned concurrency or SnapStart?

Both features are for teams whose Lambda functions have a cold start that someone actually notices. That usually means synchronous, caller-facing paths: HTTP APIs behind an API gateway or load balancer, functions invoked directly by other services that are waiting on the response, and request chains where one slow hop pushes a whole user request past its timeout. Java functions are the most common case because JVM startup, class loading and framework init make their cold starts long. Some other managed runtimes can also use SnapStart, but which runtimes are supported changes over time, so confirm yours in the current documentation.

  • SnapStart suits teams running Java services on supported runtimes who want shorter cold starts without paying for idle capacity, and who can adopt versioned deployments and audit init code for snapshot-unsafe state such as seeds, credentials, connections and timestamps.
  • Provisioned concurrency suits teams with a defined latency target, typically a p99 or max-latency objective, and traffic steady enough to keep configured environments busy. It also fits functions that cannot use SnapStart, because of the runtime or a feature incompatibility, or because init state cannot be made safe to snapshot.
  • Neither is usually worth it for asynchronous and batch workloads such as queue consumers, stream processors, scheduled jobs and event-driven pipelines. There, a cold start adds a few seconds to work nobody is waiting on, and the extra deployment complexity or idle charges buy little.

Ownership matters too. Provisioned concurrency is a recurring cost line that someone has to size, monitor for spillover, and revisit as traffic changes. SnapStart is mostly an engineering change to code and pipeline, with little ongoing tuning once the hooks are correct.

When should you use provisioned concurrency, SnapStart, or neither?

Use the cold start measurements and traffic shape you already have to decide, roughly in this order:

  1. Confirm cold starts are the problem. If p99 and max latency are acceptable on on-demand, or if cold starts mostly land on asynchronous invocations, stop here.
  2. Reduce init work first. Apply the levers in the next section, then measure again. A smaller init makes both features cheaper and more effective.
  3. Try SnapStart next when the function is on a supported runtime, the workload is latency-sensitive, and traffic is spiky, low-volume, or quiet overnight. Restore time on each cold start is often an acceptable trade for paying nothing for idle capacity.
  4. Use provisioned concurrency when SnapStart's restore time plus post-restore warm-up still misses the latency target, when SnapStart is not available for the function, or when traffic is steady enough that measured utilization clears the break-even threshold from the earlier section. Size it from measured concurrency, and use Application Auto Scaling if load follows a predictable daily pattern.
  5. Use both together only where supported and justified. Check current documentation for whether the two can be combined on your runtime, and treat it as a rare case for very strict latency targets.

Re-run this decision when traffic patterns, memory settings, runtime versions or pricing change. A choice that was right for last year's traffic curve can be the expensive one today.

What are the alternatives, and how do you measure whether either option worked?

Before paying for either feature, cut the initialization work itself. Each of these levers costs engineering time once, while both features bill every month:

Lever Why it helps Recurring cost
Trim dependencies Fewer JARs means fewer classes to load and scan None
Avoid reflection-heavy framework startup Classpath scanning and runtime proxy generation dominate init in many frameworks; compile-time DI avoids them None
Move expensive work out of the handler path Lazy-load rarely used clients and cache configuration so the hot path does only request work None
Tune memory size Lambda allocates CPU in proportion to memory, so init and JIT run faster at higher settings Higher per-ms price, often offset by shorter duration

Both features can hide a slow init that a code change would fix. A 6-second init that SnapStart restores in under a second still slows every deployment and every new snapshot. With provisioned concurrency, the same slow init still hits callers on spillover.

Measuring the result

  • Compare p99 and max latency, not averages. Cold starts are rare enough that they barely move the mean.
  • Track init and restore duration. The REPORT log line records Init Duration, and Restore Duration for SnapStart. Traces show these as separate phases.
  • Count spillover cold starts. Count invocations that still report an init or restore duration above your provisioned level.
  • Compare cost per million requests before and after, including idle provisioned-concurrency charges.

Takeaway: Start with SnapStart for most Java workloads. Use provisioned concurrency when measured, predictable, latency-critical traffic justifies paying for idle capacity.

How OneEnv can help

Disclosure: OneEnv is the product behind this blog.

OneEnv (oneenv.io) is a workspace-based governance platform for the things services share — environment variables, database structure, API routes and validation rules. A change to a shared value is staged on a branch, reviewed once, and then every affected service explicitly approves or rejects it before it goes live, so you can see who a change touches before it ships instead of finding out in production.

Applied to provisioned concurrency vs snapstart, this is where it helps:

Saves time

  • Every service that inherits a shared value is listed in one place, so nobody searches repos and chats to find out who is affected.
  • One review per branch replaces coordinating each team separately; each service still keeps its own approve/reject.
  • Changing a shared variable is one edit and one review instead of editing every service's .env by hand.

Saves cost

  • Fewer production incidents and rollbacks caused by a shared value, route or column changing under a consumer unannounced.
  • Less duplicated configuration to maintain: one shared definition inherited by many services.
  • A wrong or stale value is caught at review time by the services it affects, not by a failed deploy.
  • OneEnv has a free plan (1 workspace, unlimited services and members, full governance), so trying it costs nothing.

These are qualitative benefits, not measured results — how much you save depends on how many services share the value.

Want to try it? Start at oneenv.io.