Skip to content

Reduce pod startup latency and foreground page-loading delays #15803

Open

Description

@jingyugao

Reduce pod startup latency and foreground page-loading delays

We are working on reducing the time from an incoming request to a usable application, including page-loading delays after background restore. There are two workstreams. I'm not sure if there is any work

1. Move more initialization into pod prewarming

Pod prewarming is already implemented in our orchestration layer. We want to move as much initialization as possible out of the request path.

In particular, we want to prepare a sandbox using --no-root-container before checkpoint files are available, and provide those files later when restoring the application container. This would allow sandbox preparation and checkpoint fetching to happen in parallel.

Related work and discussion: PR #15551.

2. Prioritize demand-driven page loading during background restore

Applications should be able to load pages they currently need without unnecessary delays from speculative background reads. We propose addressing this in two separate changes.

2a. Fix scheduling stalls when reader capacity is available

AsyncPagesFileLoad.main() currently calls:

apfl.ar.Wait(completions[:0], 1 /* minCompletions */)

whenever reads are in flight, even if the reader still has spare capacity. New demand-driven requests cannot interrupt this wait.

We reproduced this on master with the following sequence:

  1. Use a reader with capacity for two concurrent reads.
  2. Start a main MemoryFile read and deliberately block it.
  3. Register another MemoryFile and access one of its unloaded pages.

The access enters the priority queue but cannot complete until the unrelated main MemoryFile read is released, despite one reader slot remaining available.

Expected behavior: newly requested pages should be eligible for submission whenever reader capacity is available, without waiting for an unrelated read to complete.

2b. Reserve reader capacity for demand-driven reads

After fixing the scheduling issue, limit the number of slots occupied by speculative background reads so they cannot consume all reader capacity.

Background read sizes or total in-flight bytes may also need bounds to reduce interference with demand-driven reads. Background loading should continue making progress while giving application accesses priority.

These changes should be evaluated separately: the first fixes a scheduling issue; the second introduces a policy for sharing I/O capacity between application demand and background loading.

Looking further ahead, could we use profiles collected from production workloads to automatically identify which memory pages the application is likely to need first, and prioritize loading those pages during restore? This could help load the initial working set before the application faults on it, reducing stalls while the remaining pages load in the background.

Is anyone already working on these areas? If so, could you share the current progress and any related issues, PRs, or design discussions? I'd like to understand what's already underway and where I can help.

Is this feature related to a specific bug?

No response

Do you have a specific solution in mind?

No response

Activity

  1. added
    type: enhancement
    on Oct 15, 2026
  2. EtiennePerot commented on Oct 15, 2026

    @EtiennePerot
    Member

    Regarding (1), is there anything else that needs to be implemented beyond what's already in-flight as PRs?
    Regarding (2), no one is looking at this right now but I agree we need better heuristics than the current way to prioritize background page loading. It's not clear to me that we should do aggressive background loading at all if not needed by the application. The current background-loading pattern was optimized around restoring inference servers with large GPU memory footprints, which need to be restored serially into GPU VRAM, hence the linear left-to-right scan. It's not the best pattern for most applications.

    IMO should definitely have a knob here that at least supports the "aggressive linear scan" strategy (today's behavior) and the "do nothing unless a fault happens" strategy (the other extreme end of the spectrum, i.e. full lazy-loading). There are probably legitimate strategies that exist in the middle of that spectrum worth experimenting with, but creating that spectrum seems like the right first step to begin with.

  3. jingyugao commented on Oct 15, 2026

    @jingyugao
    ContributorAuthor

    @EtiennePerot Thanks, that makes sense. Here is how our prewarming works and why passing the checkpoint later helps.

    (1) Prewarming and deferred checkpoint binding

    We keep a pool of unassigned Pods. A resume request claims a compatible Pod and supplies its checkpoint; the application does not run while waiting in the pool.

    Today, our adapter prepares the sandbox with runsc create, then holds sandbox start until the checkpoint is available and replaces it with runsc restore.

    The pause container is the sandbox's root container. Its start is part of RunPodSandbox, which kubelet waits for before creating application containers. Our wait therefore leaves application-container preparation on the request path.

    With no-root and deferred checkpoint binding, we can finish the empty sandbox stage without a checkpoint, let kubelet create the application container, and wait at application start instead:

    flowchart TD
        subgraph Current
            A[Prewarm sandbox] --> B[Wait at sandbox start]
            B -->|Request + checkpoint| C[Return from RunPodSandbox]
            C --> D[Create application container]
            D --> E[Start / restore application]
        end
        subgraph Deferred
            F[Prepare no-root sandbox without checkpoint] --> G[Return from RunPodSandbox]
            G --> H[Create application container]
            H --> I[Wait at application start]
            I -->|Request + checkpoint| J[Restore application]
        end
    
    Loading

    We implemented this on top of #15551 in this branch. Our Pods currently have one application container; with several, blocking the first start would prevent kubelet from reaching later containers and needs further handling.

    We compared three configurations on a real k3s node with systrap, with 30 samples each in balanced, shuffled order:

    Configuration Median claim-to-response latency P95
    Pause; checkpoint at sandbox start 223.7 ms 254.2 ms
    No-root only; checkpoint at sandbox start 205.1 ms 234.7 ms
    No-root + deferred checkpoint + application precreation 140.4 ms 210.5 ms

    Deferred binding saved another 64.6 ms over no-root alone and was faster in 29/30 paired rounds. No-root alone showed an 18.6 ms median reduction, but that comparison was too noisy to claim a stable gain. The overall reduction was about 83 ms this run and 62 ms in an earlier run.

    These are preliminary results with the same runsc binary and image, cached checkpoints, background restore, and an 8 MiB probe limited to 0.5 CPU / 128 MiB. Both no-root configurations used the same checkpoint. Timing ends after restored-memory validation and a response-file write; it excludes checkpoint download and the application API/controller path, and is not an HTTP benchmark.

    (2) Start with fully lazy page loading

    I agree with first adding an option to disable speculative background reads and load pages only on access. The application could warm itself with representative requests or by touching its working set before accepting traffic.

    Over time, we could collect access profiles and bring useful strategies into gVisor, including bounded read-ahead and automatically choosing which pages to load first from production profiles. The foreground scheduling stall can be fixed independently.

  4. jingyugao commented on Oct 16, 2026

    @jingyugao
    ContributorAuthor

    Following up on (2): our k8s experiments support starting with an explicit demand-only page-loading mode. We would like to pursue this instead of rate-limiting internal speculative reads as proposed in #15721. Pages would still load on demand; optional prefetch would be controlled independently by orchestration.

    We tested master 0e89e080 against a local demand-only variant, using a real Codex app-server workload on k8s/systrap. The Pod also contained an idle process holding 500 MiB of fully written anonymous memory; pages.img was 561.4 MiB. There were no Pod CPU or memory requests/limits.

    Each configuration had 10 trials in randomized paired rounds, using the same image and immutable checkpoint. All checkpoint files started nonresident in host page cache, verified with mincore. Application/rootfs caches remained warm; VM backing-storage caches were not controlled. Every configuration used the same initialize → thread/resume → turn/start client flow.

    The metric is restore trigger to the first real model-provider request reaching a forwarding proxy. It includes our current pause-container/CRI restore path and the application RPCs; it excludes Pod scheduling, image pull and downstream inference. External prefetch starts concurrently with restore, without application changes or prewarming before the timer starts.

    Configuration Median time to downstream dispatch
    Master internal background loading 666.9 ms
    Demand-only 500.7 ms
    Demand-only + external whole-checkpoint prefetch 551.3 ms
    Demand-only + external profiled-hotset prefetch 466.8 ms

    Hotset prefetch beat master and demand-only in all 10 paired rounds, with median paired savings of 199.8 ms and 35.0 ms, respectively. All 40 real model turns completed with validated replies and completion notifications; restored process identities and the full 500 MiB contents were also checked.

    For the hotset, we recorded checkpoint ranges resident at first dispatch in three separate cold, demand-only training restores, then froze their union before evaluation. The resulting 56.9 MiB footprint includes restore/runtime accesses and readahead, rather than being a precise per-process memory profile. It is specific to this checkpoint. A separate host process warms those ranges into page cache; gVisor still loads requested pages into its MemoryFiles on demand. Both external-prefetch configurations used one worker paced at 512 MiB/s of logical page touches, not a hard device-bandwidth limit. Hotset prefetch finished about 115 ms after the restore trigger.

    The existing priority queue does not fully isolate foreground loading from background work:

    With internal speculative loading disabled, external prefetch does not occupy gVisor's reader slots or run through its page-loader scheduling/completion loop. It can select the early working set and overlap I/O with restoration, leaving gVisor to service demand from a warmer host page cache.

    This does not eliminate shared storage contention: whole-checkpoint external prefetch was slower than demand-only in every paired round. The useful combination here is demand-only loading plus selective, overlapping prefetch. These application-level measurements also do not quantify how much of master's delay comes from the specific Wait gap versus unnecessary I/O and other resource contention.

    This supports exposing demand-only loading as the first policy option, while fixing the foreground scheduler wakeup issue independently. External prefetch provides a way to experiment with working-set selection and pacing without putting speculative reads back into the foreground loader's queue.

    Assisted-by: OpenAI Codex

  5. ayushr2 commented on Oct 17, 2026

    @ayushr2
    Contributor

    cc @nixprime could really use your help here! This looks like really interesting work. Specially the "2. Prioritize demand-driven page loading during background restore" part. I think you'll be best to comment here since you authored a lot of async page loading in gVisor.

  6. added this to the Lightspeed milestone on Oct 22, 2026
  7. jingyugao commented on Nov 3, 2026

    @jingyugao
    ContributorAuthor

    Status update

    Resume latency for Cellbox now meets our goal, so we are pausing latency work. This comment summarizes what we measured and what remains open upstream. We run a public build: upstream ac7b4e0fdc plus #15721, #15722, #16368 and #16744.

    Result

    The test Box has 1 GB of memory in its cgroup, of which 900 MiB is a Go heap that nothing reads after resume. It ran on one k3s node (8 vCPUs, Hyper-V virtual disk) with systrap. These are best-case figures: resumes ran one at a time with no CPU or memory limits, the runtime processes ran at raised priority, and the node was otherwise idle. The client ran on the node.

    Time from the resume request to the first HTTP response, in ms:

    Setup Cached checkpoint (median) Evicted checkpoint (median)
    Warm slot + --defer-checkpoint, --background-load=lazy 61 (54-72, n=30) 71 (n=10)
    Warm slot + --defer-checkpoint, eager background load 83 (n=15) 92 (n=15)
    No warm slot, lazy about 670 about 690
    No warm slot, eager about 820 about 750

    Without a warm slot, most of the time goes to Kubernetes creating the Pod, and that part varies widely. Without #16368 we cannot keep a sandbox ready for an unknown checkpoint, so the last two rows approximate what we would have without these PRs.

    These figures depend on a small working set. A first request that touches a lot of memory takes longer, and so does a slower disk.

    (1) Prewarming and deferred checkpoint binding

    With #15551 merged, #16368 lets a no-root sandbox finish preparation before the checkpoint is known. Orchestration precreates the application container and waits at its start, so one warm slot can serve any compatible checkpoint. It accounts for most of the gain in the table above.

    (2) Demand-driven page loading

    Instead of limiting background reads (2b), we went with the explicit --background-load=eager|lazy suggested in review (#15721). It saves about 20 ms per resume in the table above. With bandwidth limited to 50 MB/s and a dropped page cache, restore to the first ls in headless Chromium went from 6.60 s with one background read to 2.28 s with lazy.

    Two costs remain:

    • A first request that touches a lot of memory takes many serial faults, so it is not faster.
    • The next checkpoint must first read everything still unloaded: 0.4-0.75 s for 170-700 MB with no bandwidth limit.

    We have not sent a separate fix for the wait gap in (2a). Lazy mode removes the speculative reads that exposed it for us, but we have not verified that the gap itself is gone.

    Where the remaining time goes

    With a warm slot, the resume operation takes about 55 ms (median of 40 rounds, 48-62 ms p10-p90). Kernel load is a stable 16 ms. Most of the spread comes from two steps on the host before the restore RPC:

    • Joining the cgroup through systemd: 8.8 ms median (6.4-11.5 ms p10-p90, max 21 ms, n=84). With the systemd cgroup driver, cgroupSystemd.CloneIntoCgroup always fails, so RunInCgroup falls back to Join, which opens a D-Bus connection and asks systemd to attach the process to the scope. The log says CLONE_INTO_CGROUP cannot be used with systemd cgroup ... This slows down gVisor startup and checkpoint/restore. The scope is the sandbox's own, so it already exists at this point.
    • From starting the gofer to the restore RPC: about 5-8 ms. We believe runsc is waiting for the gofer to initialize, but we have not confirmed this.

    Our own fsync of a durable marker cost 3-8 ms. Removing it in a test lowered the median by about 5 ms but did not narrow the spread.

    Two earlier findings, from an instrumented build, are still experiments:

    • In deferred restore, the first signal.Notify in Loader.run took 10-19 ms because Go starts its signal-mask thread. Starting that thread before waiting for the start signal removed the delay.
    • Reducing asyncLoadMapUnit from 32 MiB to 2 MiB lowered the time to first response from about 74 ms to about 66 ms. This was on a small 8 MiB Box only and was noisy.

    PR status

    PR Purpose Status
    #15551 Checkpoint/restore with --no-root-container Merged
    #16368 --defer-checkpoint for prewarming Review comments addressed, awaiting re-review
    #15721 --background-load=eager|lazy Reworked as suggested, awaiting review
    #15722 Fix decommit racing with async reads Updated after review, awaiting re-review
    #16744 Fix panic on runsc start of a sub-container in a restoring sandbox Awaiting review
    #15557 Fix panic from Fragmentation.releaseJob after restore Awaiting review

    #16744 and #15557 do not change latency. Each fixes a panic we hit around restore.

    Where we'd like input

    1. Settling runsc: add restore --background-load=eager|lazy #15721 and runsc: defer checkpoint binding for no-root sandbox prewarming #16368 matters most to us, because we can't use upstream gVisor without them. Is the --background-load shape what you want before we polish it further?
    2. When the systemd unit already exists, would you accept a change that uses CLONE_INTO_CGROUP with the unit's cgroup directory instead of joining through D-Bus? Or is there a reason it must go through systemd?
    3. Should we send the signal-mask thread change as its own PR?
    4. Is profile-guided page loading of interest in-tree? Our earlier experiment warmed a profiled hotset into the host page cache from outside gVisor, which helped but cannot remove serial fault waits. Loading a known working set inside gVisor could. We will not start on this without a signal from you.

    One unrelated problem, in case it is familiar: suspend occasionally fails with prepareSaveDead: invalidated dentries can't be saved. We saw it twice, both times on an open SQLite WAL file in a persistent volume. We have not found the cause and will open a separate issue once we have a reproducer.

  8. jingyugao commented on Nov 5, 2026

    @jingyugao
    ContributorAuthor

    @manninglucas Could you help review or route #15721 and #16368? These are the two changes we most need to move back to upstream gVisor.

    #15721 now uses the suggested --background-load=eager|lazy API, and #16368 has been updated following review, including no-root deferred-restore tests. The latest measurements and remaining limitations are summarized in the status update above.

    @milantracy Could you take another look at #16368 when you have time? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions