# How CubeSandbox Achieves Sub-60ms Sandbox Startup Using KVM MicroVMs

> Discover how CubeSandbox uses KVM MicroVMs to achieve sub-60ms sandbox startup by restoring memory snapshots, bypassing slow boot sequences and BIOS initialization.

- Repository: [Tencent Cloud/CubeSandbox](https://github.com/TencentCloud/CubeSandbox)
- Tags: internals
- Published: 2026-07-10

---

**CubeSandbox achieves sub-60ms sandbox startup by restoring Firecracker MicroVMs from pre-built memory snapshots rather than performing a full boot sequence, eliminating BIOS initialization and kernel startup latency.**

CubeSandbox, an open-source project by TencentCloud, provides a lightweight sandbox runtime built on KVM-based **Firecracker MicroVMs**. While traditional virtual machines require seconds to initialize, CubeSandbox achieves **sub-60ms sandbox startup**—often reaching sub-6ms in production environments—through a tightly optimized stack that bypasses conventional boot processes. The architecture relies on pre-captured memory snapshots, pre-warmed network interfaces, and zero-copy storage clones to instantiate isolated workloads almost instantly.

## Firecracker MicroVM Foundation with micro_http

CubeSandbox builds upon the Firecracker hypervisor, utilizing its **micro_http** API for VM lifecycle management. Unlike standard QEMU-based virtualization, Firecracker provides a minimal device model and a fast HTTP/JSON control interface that eliminates the overhead of traditional VMMs.

In [`hypervisor/vmm/Cargo.toml`](https://github.com/TencentCloud/CubeSandbox/blob/main/hypervisor/vmm/Cargo.toml), the project declares its dependency on the `micro_http` crate, which provides the lightweight HTTP server that accepts VM configuration and snapshot commands. The API schema, documented in [`hypervisor/docs/api.md`](https://github.com/TencentCloud/CubeSandbox/blob/main/hypervisor/docs/api.md), accepts a single `PUT /snapshot` request containing the memory and disk state, allowing the VMM to instantiate a fully configured guest without traversing a boot loader.

## Pre-Built Snapshot Restoration

The core optimization for rapid startup is the **snapshot** mechanism, which captures a running VM’s memory and disk state once, then clones it for every new sandbox. This eliminates the need for BIOS post, GRUB initialization, and kernel boot.

In [`CubeMaster/pkg/templatecenter/template_image.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeMaster/pkg/templatecenter/template_image.go), the platform creates immutable snapshots of prepared sandbox templates. When a new sandbox is requested, [`CubeMaster/pkg/templatecenter/snapshot_ops.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeMaster/pkg/templatecenter/snapshot_ops.go) orchestrates the restoration by sending the snapshot metadata to the Firecracker VMM.

The VMM implementation in [`hypervisor/vmm/src/vmm.rs`](https://github.com/TencentCloud/CubeSandbox/blob/main/hypervisor/vmm/src/vmm.rs) handles this via the `load_snapshot` method:

```rust
#[derive(Deserialize)]
struct Snapshot {
    memory: MemoryState,
    disks: Vec<DiskState>,
}
let snapshot: Snapshot = serde_json::from_slice(&body)?;
vmm.load_snapshot(snapshot)?;

```

This approach means the guest kernel is already initialized; the VMM simply maps the saved memory pages into the guest’s address space.

## Immediate VCPU and Memory Mapping via EPT

CubeSandbox leverages **Extended Page Tables (EPT)** to map snapshot memory pages directly into the guest’s physical address space (GPA). Rather than copying the entire memory image at startup, Firecracker uses lazy page faulting to populate page tables on demand. Untouched pages remain shared with the snapshot backing file, reducing the memory footprint and initialization time to near zero.

This mechanism is visible in the VMM’s memory management logic, where the snapshot restoration path establishes the EPT mappings before the VCPU begins execution, ensuring the guest sees a fully initialized memory image immediately upon VCPU resume.

## Pre-Warmed TAP Device Pool

Network interface creation typically dominates VM startup latency due to `ioctl(TUNSETIFF)` calls and interface configuration. CubeSandbox eliminates this by pre-allocating a pool of **TAP devices** at the network agent’s startup.

In [`network-agent/internal/service/tap_lifecycle.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/network-agent/internal/service/tap_lifecycle.go), the agent executes a warm-up routine at initialization:

```rust
fn warmup_tap_pool(num: usize) -> Result<()> {
    for _ in 0..num {
        let tap = create_tap()?;
        TAP_POOL.push(tap);
    }
    Ok(())
}

```

When a sandbox starts, [`network-agent/internal/service/tap_fd_provider.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/network-agent/internal/service/tap_fd_provider.go) hands over a pre-opened file descriptor from this pool, avoiding the ~60ms latency penalty of creating TAP devices on demand. This ensures the network path is ready before the guest OS begins execution.

## Zero-Copy Disk Clones with Reflink

Storage initialization is optimized using **copy-on-write (COW)** cloning via the `FICLONE` ioctl, which creates a reflink-based copy of the snapshot disk image. This operation updates only metadata, sharing the same data pages between the template and the new sandbox until a write occurs.

The implementation in [`cubecow/src/engine/reflink.rs`](https://github.com/TencentCloud/CubeSandbox/blob/main/cubecow/src/engine/reflink.rs) probes for reflink support and performs the lightweight clone, meaning the sandbox’s block device appears instantly without copying gigabytes of data. This zero-copy approach ensures that disk provisioning adds only microseconds to the startup time.

## Vsock Control Plane for Health Checks

CubeSandbox uses **vsock** (virtual socket) channels for the control plane, providing a low-latency, doorbell-style communication path between the host and guest. The host-side vsock listener in [`hypervisor/virtio-devices/src/vsock/unix/muxer.rs`](https://github.com/TencentCloud/CubeSandbox/blob/main/hypervisor/virtio-devices/src/vsock/unix/muxer.rs) multiplexes connections before the sandbox exists, allowing the guest agent to connect immediately upon boot.

In [`CubeMaster/pkg/templatecenter/snapshot_runtime_ref.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeMaster/pkg/templatecenter/snapshot_runtime_ref.go), the platform uses this vsock channel to verify sandbox readiness via a lightweight health check ping. Because vsock bypasses the TCP stack and kernel networking layers, the latency is measured in microseconds rather than milliseconds, ensuring the startup signal propagates instantly.

## Bootstrap Warm-Up Gate

To prevent immediate termination of short-lived sandboxes, the **Cube-Lifecycle-Manager** implements a bootstrap warm-up period. During this window, the sweeper ignores idle-timeout checks for freshly created sandboxes.

The logic in [`cube-lifecycle-manager/internal/sweeper/sweeper.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/cube-lifecycle-manager/internal/sweeper/sweeper.go) ensures that the sub-60ms startup window is not cut short by background cleanup routines, giving the guest agent sufficient time to report health via the vsock channel before the sandbox enters the standard lifecycle management流程.

## SDK Implementation: Starting a Sandbox

The Go SDK abstracts these optimizations into a single API call. In [`sdk/go/sandbox.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/sdk/go/sandbox.go), the `StartSandbox` method marshals the request into the Firecracker micro_http snapshot command:

```go
ctx := context.Background()
client, _ := cubesandbox.NewClient("<cube-api-endpoint>")
req := &cubesandbox.StartSandboxRequest{
    TemplateID: "my-template-v1.0",
    Env: map[string]string{
        "HELLO": "world",
    },
}
resp, err := client.StartSandbox(ctx, req)
if err != nil {
    log.Fatalf("sandbox start failed: %v", err)
}
fmt.Printf("sandbox %s ready on vsock port %d\n", resp.SandboxID, resp.VsockPort)

```

This high-level call triggers the snapshot restoration, TAP device attachment, and vsock health check verification, completing the entire sequence in under 60ms.

## Summary

- **CubeSandbox** achieves sub-60ms startup by restoring Firecracker MicroVMs from pre-built snapshots rather than booting from scratch.
- **Pre-warmed TAP pools** eliminate network interface creation latency by handing out pre-opened file descriptors.
- **Zero-copy disk clones** via reflink/FICLONE provide instant block devices without data duplication.
- **EPT memory mapping** allows immediate VCPU execution by lazily populating page tables from the snapshot.
- **Vsock control plane** enables microsecond-level health checks, confirming readiness faster than traditional network stacks.
- **Bootstrap warm-up gates** prevent premature termination during the critical startup window.

## Frequently Asked Questions

### What is the difference between CubeSandbox and traditional KVM VMs?

Traditional KVM VMs boot through a full BIOS/UEFI sequence, kernel initialization, and userland startup, typically taking seconds. CubeSandbox skips this entirely by restoring a pre-captured memory snapshot via Firecracker, achieving sub-60ms startup times. The VM is "woken" rather than booted, with the VMM mapping existing memory pages via EPT.

### How does the snapshot mechanism work in CubeSandbox?

The platform captures a template VM’s memory and disk state using [`CubeMaster/pkg/templatecenter/template_image.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeMaster/pkg/templatecenter/template_image.go) and stores it as a snapshot. When starting a new sandbox, [`CubeMaster/pkg/templatecenter/snapshot_ops.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeMaster/pkg/templatecenter/snapshot_ops.go) sends this snapshot to the Firecracker VMM, which restores the guest state via `vmm.load_snapshot()`. This bypasses the bootloader and kernel initialization entirely.

### Why is the TAP device pool necessary for sub-60ms startup?

Creating TAP devices via `ioctl(TUNSETIFF)` can take approximately 60ms, which would consume the entire startup budget. By pre-creating a pool of 500+ TAP devices in [`network-agent/internal/service/tap_lifecycle.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/network-agent/internal/service/tap_lifecycle.go), CubeSandbox hands over an existing file descriptor in microseconds, eliminating this bottleneck.

### What prevents a sandbox from being terminated immediately after creation?

The [`cube-lifecycle-manager/internal/sweeper/sweeper.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/cube-lifecycle-manager/internal/sweeper/sweeper.go) implements a bootstrap warm-up period that exempts newly created sandboxes from idle-timeout checks. This ensures the guest agent has sufficient time to connect via vsock and report health before the lifecycle manager evaluates the sandbox for termination.