Skip to content
Aesthetic Infra

    Speeding up EC2 and Lambda boot time

    Hieu Vu

    16 min read
    On this page

    Both Lambda and EC2 are slow at boot because they build an environment and run initialization before they can handle the first job. Every speed-up feature does one of three things: move that work off the request path, reuse a captured result, or remove work entirely.

    Goal and situation

    After this post you can explain why boot is slow, pick one feature for a specific situation, and predict when it stops helping. Prerequisite: a basic grasp of Lambda and EC2 Auto Scaling.

    Simple

    Whenever a guest arrives and nobody is ready, the guest waits while people get prepared. Speeding up means preparing before the guest arrives.

    Operations engineer

    A ticket-booking API runs on Lambda and a web fleet on EC2 Auto Scaling. At 9 a.m. traffic jumps five-fold; the first request and any new instance are the slow parts. Constraint: the cost of holding idle capacity must stay low.

    Architect

    Three levers, each with its own price. Prepare ahead (Provisioned Concurrency, warm pool) pays for idle capacity. Capture (SnapStart, a prebuilt AMI) pays with state constraints. Do less (optimize INIT) pays with engineering effort.

    Picture it as

    A restaurant at peak has to light the fire and prep before it cooks the first dish; a kitchen already prepped serves instantly. Lambda and EC2 at scale-out do the same: build an environment, run INIT or user data, then handle work. A snapshot or a prepared pool skips that part.

    Real lifeAWS
    Light the fire and prepBuild environment plus INIT (Lambda), boot plus user data (EC2)
    Keep a chef standing at the stationProvisioned Concurrency: environments pre-run INIT; you pay even when the room is empty
    Freeze a pot that is already preppedSnapStart: capture an INIT-complete environment, reheat it on demand
    Staff resting in the next roomWarm pool: initialized instances sit Stopped or Hibernated waiting for a shift

    Mechanism: where the time goes

    ServicePhase 1Phase 2 (usually longest)Phase 3
    LambdaBuild environment (load code, start runtime)INIT: code outside the handlerHandler
    EC2 Auto ScalingLaunch plus OS bootUser data, install dependenciesApp start plus health check

    Case A · Plain Lambda cold start

    A Java ticket-booking API at 9 a.m. with no free environment. Illustrative numbers: 500 ms to build the environment, 1800 ms INIT, 200 ms handler.

    Total ≈ 2.5 s

    3 / 3

    First request · no free environment

    1. A request arrives but no execution environment is free; Lambda loads the code and starts the runtime in 500 ms, on the request path.
    2. The INIT phase runs code outside the handler: load classes, create SDK clients, open connections. This case is 1800 ms, 72 percent of the total and the most expensive part.
    3. The handler runs for 200 ms. The user waits 2.5 seconds in total even though the business logic only needs 0.2 seconds.

    Takeaway: the 1800 ms INIT is the main target because it repeats on every new environment and has nothing to do with business logic.

    Case B · EC2 scale-out with no warm pool

    A web fleet needs more instances at 9 a.m. Illustrative numbers: 40 s OS boot, 90 s user data, 50 s app plus health check.

    InService ≈ 180 s

    3 / 3

    New instance · no warm pool

    1. An alarm triggers scale-out; the Auto Scaling group calls EC2 RunInstances from the AMI in the launch template, and the instance boots the OS in 40 seconds.
    2. User data installs packages, pulls artifacts, writes data to disk: 90 seconds. This is the part a prebuilt AMI or a warm pool can compress.
    3. The app starts and passes its health check; only then is the instance InService. About 180 seconds total while traffic keeps climbing.

    Takeaway: the 90 s of user data does not depend on load, so doing it after traffic arrives is wasted. That is exactly what a warm pool or a prebuilt AMI targets.

    Experiment: change one condition, predict the result

    The original slider lab is not part of the blog, so predict first, then compare. The case diagrams above have a Play button that replays each phase in proportion to its time.

    Set Provisioned Concurrency to 5 while 10 requests arrive at once: 5 requests still cold start because only 5 environments are pre-run, and the slowest request is still about 2.5 seconds, unchanged from having PC off. The value of PC is that the other 5 requests drop to 0.2 seconds. Turn SnapStart on too and those 5 fall to about 0.5 seconds.

    Push the request count to 20 with PC = 10 and enable SnapStart: PC = 10 cannot lower the slowest request while SnapStart is off, because request 11 onward still goes through a full cold start.

    The declarative model: requests and instances arrive at once with no environment reuse; 500 ms to build, 200 ms handler, 300 ms restore, and warm starts of 60 s (Stopped) or 40 s (Hibernated) are all illustrative assumptions. The model ignores cost, Lambda's own environment reuse, and the time to capture a snapshot.

    Five solution cases: when they help, when they stop

    CaseConditionBranch pointResult
    C · SnapStartJava Lambda, heavy INITRestore a snapshot instead of re-running INIT0.5 s instead of 2.5 s
    D · Warm poolLong EC2 boot, predictable loadHeavy work already done while parkedabout 60 s instead of 180 s
    E · PC exceededPC = 2, three requestsThird request has no ready environmentRequest 3 still takes 2.5 s
    F · Pool drainedNeed 3 instances, pool has 2Third instance is not in the poolFull cold start of 180 s
    G · SnapStart stale stateConnection opened during INITOld connection is invalid after restoreFailure, then fixed by reconnecting

    Case C · SnapStart shortens a Java cold start

    The same Java function as Case A, now with SnapStart on a published version. Supported on Java 11+, Python 3.12+ and .NET 8+ (check the current list).

    Total ≈ 0.5 s

    4 / 4

    At publish (once)

    First request after publish

    1. You publish a version: Lambda runs INIT right then, with no request waiting.
    2. Lambda captures a Firecracker microVM snapshot (memory plus disk) of the INIT-complete environment, encrypts and caches it.
    3. The first request arrives: Lambda restores the snapshot instead of re-running INIT, 300 ms.
    4. The handler runs for 200 ms. Total 0.5 seconds instead of 2.5 in Case A, an 80 percent drop in this illustrative example.

    Takeaway: INIT still runs, but once at publish instead of on every cold start. Java is not billed extra for SnapStart; other runtimes pay for the cache and restore.

    Case D · Warm pool shortens scale-out

    The same web fleet as Case B, now with a warm pool of Stopped instances. The root device must be EBS.

    InService ≈ 60 s

    4 / 4

    Background · instance in the warm pool

    At scale-out · taken from the pool

    1. Before peak, the warm pool launches instances and runs all the user data while a lifecycle hook holds them back; the heavy work finishes in the background.
    2. The instances are stopped. In the Stopped state you pay only for EBS and Elastic IP, not for the instance.
    3. Scale-out happens: an instance starts again from the pool, 35 seconds, without re-running the heavy install.
    4. The app starts and passes health check: 25 seconds. InService after about 60 seconds instead of 180 in Case B.

    Takeaway: you pay the EBS cost of a Stopped instance to buy back 120 seconds on every scale-out. The AWS docs warn: if the first boot is not meaningfully slow, you do not need a warm pool.

    Case E · Requests exceed Provisioned Concurrency

    PC = 2, three concurrent requests. Assumption: all three arrive at once.

    Req 3: 2.5 s

    3 / 3

    Requests 1–2 · within Provisioned Concurrency

    Request 3 · beyond PC = 2

    1. You set Provisioned Concurrency to 2: Lambda pre-runs INIT for 2 environments before peak. Requests 1 and 2 arrive together, each taking a ready environment and only running the handler, 200 ms.
    2. Request 3 arrives while both environments are busy. It exceeds PC, so Lambda builds a new environment the normal way: build plus INIT.
    3. Its handler finishes after 2.5 seconds. PC only covers the number of environments you configured; the overflow still pays a cold start.

    Takeaway: size PC to the peak concurrency you want to protect, measured by metric, not to the average. The overflow still pays a cold start.

    Case F · Warm pool runs dry mid-spike

    Three instances needed, the warm pool has 2. The same cold start happens if an Availability Zone runs out of capacity.

    Instance 3 ≈ 180 s

    2 / 2

    Instances 1–2 · from the warm pool (pool = 2)

    Instance 3 · warm pool drained

    1. The spike needs 3 new instances, but the warm pool holds only 2. Instances 1 and 2 come from the pool: warm start, InService after about 60 seconds.
    2. The pool is empty, so the instance launches straight into the Auto Scaling group: a full cold start, and instance 3 is up after 180 seconds.

    Takeaway: the default pool size is max capacity minus desired capacity; if the peak is larger, set MaxGroupPreparedCapacity or accept the remainder coming up slowly.

    Case G · SnapStart reuses an old connection, then fixes it

    The function opens a DB connection during INIT. Per the AWS docs, a connection open at INIT is not guaranteed to survive a resume from snapshot.

    Total ≈ 0.7 s

    5 / 5

    Request after restore · unfixed code

    Request after restore · fixed code

    1. The code opens the DB connection during INIT, so that connection is in the snapshot. A request arrives, Lambda restores the snapshot in 300 ms.
    2. The handler uses the connection from the snapshot. Its state is not guaranteed after restore, so the call fails or hangs (600 ms in this example).
    3. The fix: same 300 ms restore, but reopen the connection right after resume, in the handler or an after-restore hook.
    4. Add 200 ms to open a fresh connection immediately after restore, before any business work.
    5. The handler uses the fresh connection and finishes, 0.7 seconds total. SnapStart is still far faster than 2.5 seconds, but you must handle the state the snapshot does not preserve.

    Takeaway: reopen connections in the handler or an after-restore hook; do not use the hostname as a unique identifier; avoid pinning a fixed source port. Symptom: connection errors only on the first request after restore, not in a warm environment.

    The speed-up toolbox

    LambdaCuts which phaseTrade-offUse when
    SnapStartINIT into a restoreOnly a few runtimes; must handle post-restore stateJava, Python, .NET; heavy INIT, bursty load
    Provisioned ConcurrencyBoth build and INITYou pay while idle; covers only what you configureYou need steady latency on predictable load
    Reduce INIT workINITCode effort, no infrastructure costAlways worth doing first, every runtime
    Understand INIT billingDoes not cut timeAWS standardized INIT billing for managed runtimesWhen frequent cold starts inflate cost
    EC2Cuts which phaseTrade-offUse when
    Warm pool (Stopped)Boot plus user dataEBS and EIP cost; needs EBS root; no weighted mixed groupsLong boot, sudden scale-out
    Warm pool (Hibernated)Boot, user data and app initMust meet hibernation conditions; RAM saved to EBSApp in-memory init takes very long
    Instance reuse policyThe next scale-outYou must manage pool sizeLoad rises and falls many times a day
    Prebuilt AMI (golden AMI)User data, install dependenciesRebuild the AMI when versions changeFixed install steps, no pool needed
    T family: CPU credit (different kind)Does not cut bootCPU burst while running; throttled when credits run outLow load with short spikes; not a boot solution

    When to use it, when not to

    Worth investing in speed

    • The first request touches a latency SLA (a user-facing API).
    • INIT or user data is longer than the actual work.
    • Load spikes suddenly but is predictable.
    • You measured and cold starts dominate p99.

    Not yet, or choose something else

    • Background jobs that tolerate a few slow seconds, skip it.
    • Steady load with few cold starts: optimizing INIT is enough.
    • Uncertain load you will not pay to wait on: use SnapStart or accept it.
    • Already-fast boot: a warm pool only adds cost.
    SituationChooseWhy
    Java API, high p99 after deploy, tight budgetSnapStart, then optimize INITNo SnapStart fee for Java; cuts the repeated INIT
    API needs fixed latency during work hoursProvisioned Concurrency on a scheduleCovers exactly the peak concurrency
    EC2 web boot 3 minutes, daily 9 a.m. spikeWarm pool Stopped (or golden AMI)Do the heavy work ahead, pay only EBS
    Instance holds a lot of data in RAM after initWarm pool HibernatedRAM is saved and reloaded

    Common myths

    MythReality
    SnapStart works for every runtimeJava, Python, .NET; check the current list
    Provisioned Concurrency ends all cold startsOnly within the number of environments you set
    SnapStart preserves all stateConnections, hostnames and random values can go stale
    Keep the warm pool Running for speedThe docs advise against it: billed like a running instance
    A warm pool guarantees every new instance is warmWhen it drains, launches are cold
    T-family CPU burst boots fasterBurst is CPU while running; it does not shorten boot

    Self check

    Question 1

    A Lambda function on Java 17 with Spring takes 3 seconds on the first request after each deploy. Load is erratic; the team wants lower latency but will not pay to hold capacity. What do you pick first?

    Answer: enable SnapStart on a published version. Java is supported, INIT runs once at publish, and Java is not billed extra for SnapStart. Provisioned Concurrency works but breaks the idle-cost constraint; raising the timeout only masks deadline errors; moving to EC2 changes the problem instead of solving it.

    Question 2

    A function sets Provisioned Concurrency to 50. Peak has 120 concurrent requests. What is true of the 70 that exceed it?

    Answer: they run on new environments and pay a cold start as if PC were off. Only 50 environments are ready; the rest must build one, like request 3 in Case E. PC does not grow with load, and the overflow is not rejected.

    Question 3

    An Auto Scaling group has a warm pool of 4 Stopped instances. A spike needs 10 more. What happens?

    Answer: 4 warm-start and 6 cold-start. With 4 in the pool, only 4 come from it; the other 6 launch straight into the group, like Case F. The pool does not grow itself, and Auto Scaling does not skip requests.

    Handoff task

    An ECS service on EC2 takes 4 minutes to load a 2 GB model; traffic triples at 9 a.m. daily. What do you choose and why?

    Grading criteria: recognize that the heavy part (loading the model) does not depend on load and can be done ahead; pick a warm pool, a golden AMI with the model baked in, or scheduled scaling before 9 a.m., and state the cost trade-off; state the limits that a drained pool cold-starts and that ECS needs its own config so instances do not join the cluster too early.

    Core terms

    TermMeaning
    Cold startA request waits while a new environment is created and initialized before the handler runs
    INITThe Lambda phase that runs code outside the handler: load libraries, create clients, open connections
    SnapStartCapture a Firecracker microVM snapshot after INIT at publish; new environments restore from it
    Provisioned ConcurrencyThe number of Lambda environments pre-run and kept warm; you pay even while idle
    Warm poolA group of initialized instances held beside the Auto Scaling group in Stopped, Running or Hibernated state
    Lifecycle hookA pause point that lets you run custom work before an instance is stopped or put into service
    InServiceThe state where an instance belongs to the group and receives traffic

    Sources and unverified parts

    Slow boot is the cost of doing everything only after the load arrives. Each speed-up moves part of that work earlier, or removes it. But preparing ahead only covers what you actually prepared. Measure first, then pick the cheapest lever that covers the peak.

    Written by

    Hieu Vu

    Software engineer writing about cloud infrastructure, DevOps and reliable systems.