Skip to content
Skip to lesson
Back to Topics
LearnSystem Design CapstonesDesign a Multi-Tenant Agent Sandbox & Code Interpreter Platform
🏗️HardSystem Design

Design a Multi-Tenant Agent Sandbox & Code Interpreter Platform

Give agents a place to run generated code with limited authority. Balance fast warm starts with tenant isolation, network controls, and clean resets.

35 min read
Learning path
Step 172 of 199 in the full curriculum

Personalize this lesson

Adapt explanations and teaching visuals to your background and preferred voice.

An agent runs a script that installs a package, creates a chart, and leaves a background process running. The next tenant needs a fresh workspace. Which parts can the service share, and which parts must disappear before that workspace is reused?

In Local Agent Sandboxing, Landlock and Seatbelt restricted processes on one workstation. A multi-tenant service also has to contain an untrusted guest, manage shared host resources, and recover after its controller crashes. We'll follow two sandbox instances, A and B, from one clean template through separate writes and eventual cleanup.

Give each session an owner and a readiness boundary

Treat generated code as untrusted. It can consume memory, spawn processes, inspect its environment, and make every network request its permissions allow. The platform's trusted host agent controls the virtual machine, resource limits, network paths, and artifact export.

Use an authenticated tenant, sandbox ID, and generation number to identify each instance. A retry can refer to an existing instance only after the controller reconciles its status. Reusing an ID while an old process still owns its resources creates a cleanup race.

This design uses lightweight virtual machines called Firecracker microVMs, each with its own guest kernel. A virtual machine monitor (VMM) is the host process that configures the guest and emulates its devices. Linux Kernel-based Virtual Machine (KVM) supplies hardware-assisted execution. The VMM, KVM, host kernel, and management services remain part of the trusted platform.[1][2]

External API connections use Transport Layer Security (TLS). The trusted broker constructs those connections after checking the sandbox's authority.

Authentication and generation reservation precede placement on one trusted host. Its host agent and template manager restore two separate guest kernels through Firecracker VMMs and KVM with the host kernel. A and B share sealed template artifacts but own separate writable memory, scratch files, and generation-scoped command channels over vsock. Their Internet egress passes an externally enforced policy boundary. Approved scoped operations reach an authenticated broker and authorized upstream TLS. Metadata, host management, and other-tenant destinations take a separate deny-and-record branch.
Trace the shared sealed template into two guest kernels with separately owned writes and command channels. Authentication and generation reservation precede placement. Internet egress follows an enforced broker-or-deny split, while commands use vsock. The host agent, template manager, Firecracker VMMs, KVM, host kernel, and authenticated broker remain trusted dependencies. The broker opens authorized upstream TLS.

Sharing the immutable template reduces repeated initialization. Each instance still owns its writable memory, scratch storage, command channel, and capabilities.

For the interactive route, set an initial design target of at most 15 ms at the 99th percentile (p99), measured from gateway acceptance to the guest supervisor beginning the first user instruction. Include placement, queueing after acceptance, restore work, and required readiness checks. Record rejection rate and timeout rate alongside that percentile. Measure cold launch separately for each image profile.

That target needs a workload test with pinned host and guest kernels, Firecracker version, image, snapshot residency, concurrent starts, and user-code entry instrumentation. Firecracker's snapshot documentation makes restore performance dependent on memory, vCPU, and device configuration.[3] A successful snapshot API response and the first executed user statement are different timing endpoints.

The service should support:

  • Separate session state for iterative agent turns, with an explicit expiration and disposal policy.
  • Bounded stdin, stdout, stderr, uploaded files, and exported artifacts.
  • Package installation from approved artifacts or network routes.
  • Resource profiles such as 1–4 virtual CPUs (vCPUs), 512 MiB–8 GiB of configured guest RAM, and a 5 GiB scratch limit.
  • Denial of guest access to host management interfaces, other tenants, and infrastructure metadata.

Those resource profiles are example product limits. MiB and GiB mean 2202^{20} and 2302^{30} bytes. The host also needs memory for VMM overhead, shared pages, page tables, buffers, and the operating system.

Put the guest kernel behind KVM

Ordinary containers isolate processes through namespaces and resource controls while sharing a host kernel. A successful host-kernel privilege escalation can compromise that shared boundary. A microVM gives each tenant a guest kernel, so controlling that guest kernel still leaves the VMM and KVM boundary to cross.[1]

KVM exposes file descriptors and ioctl operations through /dev/kvm. A host VMM typically:

  1. Opens /dev/kvm and uses KVM_CREATE_VM to obtain a VM descriptor.
  2. Registers host userspace memory regions as guest physical memory.
  3. Uses KVM_CREATE_VCPU to obtain vCPU descriptors.
  4. Runs each vCPU through KVM_RUN and handles exits that require userspace device emulation.[4]

Hardware virtualization handles guest execution and address translation. Some exits are handled inside KVM. Others return to the VMM with a userspace exit reason.

The following C fragment shows only that control flow. VM initialization, memory mappings, device handlers, and shutdown coordination are omitted. Check the return value before consulting exit_reason, because an interrupted or failed call has a different outcome.[4]

kvm-vcpu-loop-sketch.c
while (!stop_requested) {
    int rc = ioctl(vcpu_fd, KVM_RUN, 0);
    if (rc < 0) {
        if (errno == EINTR) {
            continue;  /* Recheck stop_requested at the loop boundary. */
        }
        report_run_error(errno);
        break;
    }
    switch (run_struct->exit_reason) {
        case KVM_EXIT_MMIO:
            handle_mmio(run_struct);
            break;
        case KVM_EXIT_IO:
            handle_io_port(run_struct);
            break;
        case KVM_EXIT_HLT:
            handle_guest_halt(run_struct);
            break;
        default:
            stop_for_unhandled_exit(run_struct->exit_reason);
            stop_requested = true;
    }
}
A host VMM opens /dev/kvm, creates a VM descriptor, registers userspace memory, and creates a vCPU descriptor. KVM_RUN on that vCPU has distinct return paths. A negative return with EINTR checks shutdown before retrying; other errors stop. A zero return permits inspecting exit_reason. MMIO and IO go to userspace device handlers and then the loop. HLT follows guest lifecycle policy and can wait for an interrupt. Other unhandled exit reasons stop. Some hardware exits are handled inside KVM and never become a userspace return.
Follow the descriptor tree, then the return-code branch. GPA means guest physical address. A successful KVM_RUN return permits inspecting exit_reason. EINTR rechecks shutdown policy, MMIO and IO need userspace handlers, and HLT follows lifecycle policy. Some hardware exits stay inside KVM. This drawing illustrates the preserved C sketch without executing a guest.

A halt needs the VMM's guest-lifecycle policy. It can mean waiting for an interrupt rather than the end of a session.

Keep the VMM's host authority small

Firecracker has a deliberately limited device surface for lightweight virtual machines. Its Rust implementation helps prevent memory-safety errors, while device emulation, unsafe code, KVM, and host integration still need security review and updates.[2]

Device support changes by version. The Firecracker source checked for this chapter supports VirtIO virtual devices over memory-mapped I/O (MMIO) and optional Peripheral Component Interconnect (PCI) VirtIO transport. That transport support doesn't imply arbitrary PCI-device emulation or device passthrough.[5] Use the matching host, guest, and device configuration rather than a fixed count of emulated devices.

The Jailer prepares a restricted host environment for the Firecracker process. It can create a restricted filesystem view, drop to configured user and group IDs, configure resource-control groups (cgroups), and create a process-ID (PID) namespace. Its --netns option joins a network namespace that the integrator has already created.[6]

Keep Firecracker's default seccomp system-call filters, protect its API socket, and bound serial and process output. The upstream host guide specifically calls out unbounded guest-driven output as a host resource risk.[2]

BoundaryState it separatesRemaining shared dependency
Container namespacesHost processes and selected resource viewsHost kernel and permitted device interfaces
Guest VMGuest kernel and guest address spaceVMM, KVM, host kernel, and hardware
Jailed VMM processHost filesystem, credentials, and process accessRequired host interfaces and their implementations
Network policyPermitted destinations and servicesEnforcement path, address handling, and broker authority

These controls protect different interfaces. Keep their configuration and failure tests separate.

Restore a clean template, then establish readiness

A useful base snapshot contains the initialized guest kernel, language runtime, and a supervisor waiting for work. Create it before tenant data, customer credentials, or user jobs enter the guest.

A Firecracker snapshot includes guest memory and versioned microVM state. Disk backing files are managed separately by the integrator and must remain consistent with the saved state.[3] Authenticate the template's provenance and integrity before loading it, and keep its backing memory file immutable for the restored VM's lifetime.

A clean template is built before tenant work: boot the pinned guest and runtime, observe the supervisor ready, pause and snapshot, then seal consistent memory, versioned state, and disk artifacts. A new generation loads those verified artifacts paused with private mappings. The snapshot API response marks state loaded. The supervisor then resumes only for readiness while user work remains withheld. Generation-bound control, clock, kernel RNG, application state, network, host limits, and ownership checks form an admission gate. Missing or failed checks hold resources and keep admission closed. Successful required checks admit the first user instruction. The 15 ms p99 is a declared design target from gateway acceptance through placement, queueing, restore, and readiness, not measured stage timings.
The snapshot API response establishes loaded state. The supervisor then runs readiness checks while user work remains withheld. Missing or failed checks keep resources held and admission closed. The declared 15 ms p99 design target includes placement, accepted queueing, restore, and required checks from gateway acceptance to the first user instruction. Stage durations require measurement.

The guest can be running while the service still withholds user work. Readiness belongs to the chosen template and restore protocol.

Follow one copy-on-write page

With the File memory backend, Firecracker maps the snapshot memory file privately. Host page faults bring required file contents into RAM on demand. Writes create private copies rather than modifying the backing template.[3]

Suppose one 4 KiB page contains the value 20. A and B initially read that clean page. When A writes 99, A gets a private page containing 99. B and the immutable base still contain 20. The physical page-size choice in this example is explicit.

Before and after mapping diagrams use three immutable base pages, each modeled as 4 KiB, with page indices 0, 1, and 2 and values 10, 20, and 30. Before the write, A/generation 1 and B/generation 1 each map all three shared base entries. After A writes 99 to page 1, only A’s page 1 mapping points to a separate private page containing 99. A’s pages 0 and 2 and all B mappings still point to the original base. B and backing page 1 retain 20. Three shared base entries plus one private entry make four modeled pages, or 16 KiB. This counts model entries and does not assert physical residency.
Follow A’s middle mapping. Its write creates one private page containing 99 while B and the backing retain 20. The model counts three shared base entries plus one private entry, giving four 4 KiB pages and 16 KiB of modeled accounting. Physical residency and VMM overhead require separate measurements. Reading can fault in clean bytes without creating a private modified copy. Here g1 identifies generation 1.

Reading a previously absent page can incur I/O and increase resident memory without creating a private modified copy. Writes, including runtime bookkeeping or collector metadata updates, can increase private memory. Mapping a snapshot avoids copying every unmodified byte up front, while page faults and working-set growth still affect latency and capacity.

MAP_NORESERVE changes reservation behavior on systems that use it. It doesn't create physical RAM or make overcommit safe. Plan admission against actual resident-memory pressure and the guest's allowed growth.

Choose who handles missing pages

Firecracker also offers the Uffd backend, which delegates missing-page handling through Linux userfaultfd to a userspace process. The integrator supplies that handler and its permissions, storage, validation, failure behavior, and latency budget.[7]

A custom handler could read snapshot chunks from a local cache or a remote store. Remote NVMe or object-storage fetching is an integration design. Firecracker's interface doesn't supply a complete distributed cache. A stalled handler can stall guest progress, so test cold pages and failure recovery as well as hot-cache restores.

Repair the state that must be fresh

Cloning a template also clones information that applications may assume is unique or current. Inspect the following state before admitting tenant work.

Wall clocks and application deadlines

The documented snapshot path can resume the guest wall clock from snapshot time. Synchronize it through the guest protocol, or use a supported restore-time clock option under its documented host and clock-source conditions.[3]

An old wall clock can affect certificate validity and token expiry. Monotonic durations, saved timers, and application deadlines need their own restore semantics. Test them on the actual guest rather than assuming every timer expires or every TLS operation fails.

Supported VMClock implementations can expose a changed generation counter to userspace after restore. That is a signal for compatible applications to refresh their state. It requires matching guest support and application integration.[3]

Kernel randomness and copied application state

Firecracker updates the virtual machine generation identifier (VMGenID) when restoring a snapshot. A supporting guest kernel uses the notification to reseed its random generator. There is a window between resuming vCPUs and handling that notification.[8]

The kernel's random generator and a copied userspace pseudorandom generator are separate state. Reinitializing the kernel doesn't replace previously generated tokens, cached random values, application identifiers, or every userspace seed. The snapshot can also retain the same /proc/sys/kernel/random/boot_id.[8]

Use a restore protocol validated for the actual guest driver and kernel configuration. Keep customer keys and one-use credentials out of the template. Admit user work only after the required kernel and application-state checks succeed. A generic “32 fresh bytes sent” counter doesn't establish that those checks passed.

Host networking and guest identity

The host TAP device name, its network namespace, and the guest's IP/MAC configuration are different objects. Jailer joins a supplied namespace. A clone strategy can reuse the same TAP name and private guest address inside distinct namespaces, with host routing or NAT separating their external paths.[6][9]

For example, 192.168.241.2 is a private address. Repeating it in isolated namespaces doesn't make it unique globally. The host must still provision the intended routes and filtering. The upstream clone-network example is a starting point that needs its own security and performance validation.[9]

Open connections and supervisor state

Snapshot restore can lose network connections. Firecracker resets existing vsock connections while preserving guest listen sockets for new connections.[3] Reconnect the host control channel for the new sandbox generation.

Guest file descriptors refer to guest resources and device protocols. They aren't ordinary host file descriptors copied into the guest. Keep the template quiescent, reconcile external state explicitly, and avoid carrying live user processes or external sessions into a shared template.

After restoring copied state, VMGenID notifies a supporting guest kernel, which reseeds its kernel random generator after an asynchronous handling window. Separate copied userspace PRNG state, cached tokens, and boot_id still require their application protocol. Restored wall time and monotonic deadlines have separate validation. Below, two integrator-prepared network namespaces each contain a TAP named tap0 and the repeated private guest address 192.168.241.2. Jailer joins the selected namespace. Distinct host routing and filtering paths remain necessary. A repeated private IP is not a unique global identity. Admission remains closed until the required state checks pass.
Kernel randomness, copied application state, clocks, and network identity have separate owners. VMGenID handling reseeds a supporting kernel RNG after an asynchronous window. Copied userspace state and identifiers need their application protocol. The integrator prepares namespaces and TAPs, then Jailer joins them. Repeated private address 192.168.241.2 still needs distinct host routes and filtering. Validate clocks and reconnect generation-bound control before admission.

Give each guest a disposable writable filesystem

A package installation or generated output.csv should change only that sandbox's writable layer. In this design, the guest mounts an OverlayFS root: a merged view of a read-only base and per-instance scratch.

The lower layer can come from a read-only SquashFS image attached through a virtual block device. In this design, the guest creates an upper directory and allocates a fresh, empty work directory for each instance. OverlayFS requires workdir to be on the same filesystem as upperdir. The work directory supports OverlayFS operations and is kept outside the merged user view.[10]

Read a base configuration file and the merged view can use the lower file. Modify it and OverlayFS copies it into the upper layer. Create output.csv and it exists in the upper layer. Delete a lower file and a whiteout records its removal from the merged view while leaving the lower file intact.[10]

The host owns the scratch backing file or volume. The guest owns its mounted OverlayFS view. After the instance retires, the host can dispose of the instance's backing resources. A host process cannot directly unmount an unrelated guest's / as though it were a host mount.

A host-owned scratch backing object supplies one private guest filesystem for A/generation 1. Upper and work are separate private directories on that same filesystem, and work stays outside the merged user view. Four aligned guest mount states track immutable lower config.txt bytes marked base. Reading uses the lower file with an empty upper. Modifying config.txt copies it into upper with A’s bytes. Creating output.csv puts its data row 1,99 only in upper. Deleting config.txt leaves a whiteout in upper, while lower still retains the original bytes and the merged view shows only output.csv. After authoritative A/generation 1 execution retirement and reference closure, the host disposes of its scratch backing. Fresh A/generation 2 has empty upper and work directories and sees the clean lower config.txt in its merged view. The guest owns its mounts. Disposal and retention require their own runtime verification.
Track config.txt through the lower, upper, and merged views. CSV rows list id, then value. Modification copies it into upper, creation adds output.csv there, and deletion leaves a whiteout while lower retains its bytes. Upper and work are separate private directories on one guest filesystem. Work stays outside the merged view. After authoritative retirement and reference closure, the host disposes of its scratch backing and allocates fresh generation 2 scratch. The guest owns the mounts.

For reuse, stop admission, retire execution, close references, revoke capabilities, and dispose of the old scratch backing. Construct the next instance from the clean template and a fresh upper/work pair. Cleanup latency depends on the storage path and outstanding work. Filesystem disposal also needs separate retention rules for exported artifacts, logs, and backups.

Use vsock as a bounded command channel

Virtio-vsock provides host-guest communication through a virtual socket device. Firecracker mediates host AF_UNIX sockets and guest AF_VSOCK sockets.[11] This control path uses Unix/vsock endpoints rather than host TCP ports.

That removes TCP port allocation from this particular channel. It still consumes descriptors, buffers, and VMM work. TCP capacity depends on its configured ephemeral range and connection tuples. Compare both paths with measurements instead of assigning either one a fixed transit time.

A guest supervisor can listen on vsock port 5000. For a host-initiated connection, the host connects to the configured Unix socket, sends CONNECT 5000\n, and waits for an OK PORT\n acknowledgement. Only then does the same stream carry application messages.[11]

A host AF_UNIX endpoint sends the literal line CONNECT 5000 backslash-n to a guest listening on AF_VSOCK port 5000 through Firecracker. The acknowledgement OK 1073741824 backslash-n is shown as three stream fragments: OK space 1073, then 741, then 824 and newline. The existing reader requests one byte per recv and accumulates until newline, leaving following application bytes untouched. One whole two-second monotonic deadline and a 64-byte limit bound the acknowledgement. The full regular expression is OK space one or more decimal digits followed by newline, and the parsed host-side port must be at least 2 to the power 30 and less than 2 to the power 31. EOF, an invalid line or range, timeout, and excess length fail the handshake before application work.
Accumulate the literal OK 1073741824\n through newline before using application bytes. These groups illustrate stream fragmentation. The preserved reader calls recv(1), with one two-second monotonic deadline and a 64-byte maximum for the entire acknowledgement. Validate the full line and reserved range [2³⁰, 2³¹). EOF, invalid input, timeout, or excess length fail the handshake. Subsequent payload remains untouched.

Stream reads can return part of a message or close before a delimiter arrives. The following host-side integration fragment reads one bounded acknowledgement without consuming application payload bytes. It requires a running, correctly configured guest and is left unmarked as an executable lesson cell.[12]

host-vsock-client.py
import re
import socket
import time

def read_ack(sock, timeout=2.0):
    deadline = time.monotonic() + timeout
    line = bytearray()
    for _ in range(64):
        remaining = deadline - time.monotonic()
        if remaining <= 0:
            raise TimeoutError("vsock acknowledgement deadline exceeded")
        sock.settimeout(remaining)
        chunk = sock.recv(1)
        if not chunk:
            raise ConnectionError("vsock closed before acknowledgement")
        line.extend(chunk)
        if chunk == b"\n":
            match = re.fullmatch(rb"OK ([0-9]+)\n", bytes(line))
            if match is None:
                raise ConnectionError("invalid vsock acknowledgement")
            port = int(match.group(1))
            if not 2**30 <= port < 2**31:
                raise ConnectionError("unexpected host-side vsock port")
            return port
    raise ConnectionError("vsock acknowledgement exceeds 64 bytes")

with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
    sock.settimeout(2.0)  # Example connect timeout.
    sock.connect("/srv/jailer/firecracker/vm-a/root/vsock.sock")
    sock.sendall(b"CONNECT 5000\n")
    assigned_port = read_ack(sock, timeout=2.0)
    sock.settimeout(2.0)  # A separate timeout for the next operation.
    sock.sendall(b'{"action":"execute","script":"print(42)"}\n')

The port check above follows the pinned Firecracker implementation's reserved host-side range. The complete service also needs generation-bound authorization, message framing, backpressure, cancellation, and output limits. Check the matching host and guest prerequisites for the deployed release.

Constrain outbound traffic and broker private API access

The command channel and Internet access serve different purposes. Give the guest only the network routes it needs, and enforce that policy outside the untrusted guest.

Put forbidden destinations behind an enforced deny path

Cloud instance metadata can expose instance information and role credentials. AWS documents IPv4 IMDS at 169.254.169.254 and an optional IPv6 endpoint, fd00:ec2::254, on supported Nitro instances and subnets.[13] The credentials belong to the EC2 instance's assigned role. Whether that instance is a bare-metal worker or a VM is a deployment choice.

Cover every relevant address family and path. Denying one address in one forwarding chain leaves other routes, local services, redirects, or proxy paths to inspect. The policy for this design is:

Guest requestEnforced handling
Host management, metadata, another tenant, or unapproved private destinationDeny before it leaves the sandbox's permitted network boundary
Approved package artifactFetch through the authorized artifact service
Approved private API operationSend to the authenticated API broker
Arbitrary destination or unresolved policyDeny and return a bounded error

Validate all resolved addresses, including IPv6 and mapped forms. Connect to the validated destination, preserve TLS hostname verification, and reject or revalidate redirects. Resolve and validate again for a new connection. Domain allowlisting and an earlier DNS lookup alone don't prevent rebinding.[14]

Give the broker an explicit protocol

Suppose A may read one repository through a GitHub integration. The orchestrator issues a short-lived capability bound to A's instance generation, tenant, repository, and permitted read operation. The guest presents that capability to an authenticated broker endpoint.

The broker validates the caller and operation, constructs the permitted upstream request, retrieves the real credential, and opens TLS to the approved API origin. The real credential stays inside the trusted broker. An ordinary encrypted HTTPS tunnel doesn't expose its Authorization header for a transparent proxy to rewrite.

Untrusted A/generation 1 holds a short-lived capability for one tenant, repository, and read operation. An explicit application request reaches the trusted broker, which authenticates caller and generation, checks tenant, resource, operation, and expiry, then constructs an approved request. A credential vault supplies the real secret only inside the broker boundary. The broker inserts the credential and opens hostname-verified TLS to the approved API. Invalid capabilities are rejected without an upstream call. Direct metadata, host, other-tenant, and private routes are denied outside the guest, including relevant IPv4 and optional IPv6 metadata endpoints. The guest capability expires and is revoked at retirement; it still grants bounded authority.
Follow one repository read from A/generation 1 through caller and scope validation to a broker-constructed request. The real credential stays in the vault and broker, which opens verified TLS. Invalid scope causes no upstream call. Direct forbidden routes are denied across relevant address families. The guest capability grants bounded authority until expiry or revocation. Opaque HTTPS headers cannot be transparently rewritten.

A phantom reference or broker capability still grants authority. Guest code can try to abuse every operation it permits. Limit methods, resource paths, redirects, response size, lifetime, and rate. Revoke the capability when its instance retires. A host or broker compromise crosses the trusted boundary described earlier.

Size resident memory and runnable work separately

Use one hypothetical host for the worksheet: 128 physical CPU cores and 256 GiB RAM. These are chosen inputs, rather than specifications for a named server.

Reserve 32 GiB for host services, shared resident template data, and an initial allowance for other overhead. This leaves:

256 GiB−32 GiB=224 GiB=229,376 MiB.256\text{ GiB}-32\text{ GiB} =224\text{ GiB} =229{,}376\text{ MiB}.

If every guest reserves a full 2 GiB within that remaining budget, the quotient is 229,376/2,048=112229{,}376/2{,}048=112. If each guest instead contributes an assumed 50 MiB of private resident pages, the private-page-only quotient is ⌊229,376/50⌋=4,587\lfloor229{,}376/50\rfloor=4{,}587.

The roughly 41-fold difference exposes the assumption that matters: how much each guest actually makes private. The second quotient excludes per-instance overhead and additional safety headroom. It cannot establish a host limit for 4,587 active 2 GiB guests. If those guests write their allowed memory, the physical demand changes dramatically.

A declared 256 GiB host worksheet reserves 32 GiB and leaves 224 GiB, or 229376 MiB. A full 2 GiB per-session budget gives quotient 112. Assuming only 50 MiB private pages per session gives private-page-only quotient 4587. The two counts appear on one exact zero-to-5000 scale. This second quotient omits per-instance overhead, additional safety headroom, and private growth. An explicit arithmetic sensitivity at 100 MiB private pages gives quotient 2293. Configured guest RAM, private modified pages, and shared resident pages are separate accounting categories, and none of these quotients establishes an active-session limit.
Use the same count scale for each quotient. The declared host leaves (256 − 32) × 1024 = 229,376 MiB. A full 2,048 MiB budget gives 112, assumed 50 MiB private pages give 4,587, and the 100 MiB sensitivity gives 2,293. Private-page-only quotients exclude per-instance overhead, additional headroom, and growth. The reserve includes shared residency and initial overhead. Configured RAM, private pages, and shared resident pages remain separate quantities.

Shared page-cache accounting, VMM memory, page tables, buffers, scratch growth, simultaneous faults, and a cold template can further reduce density. Measure those terms under concurrent starts and representative code, then admit against bounded memory pressure. Test correlated peaks and recovery capacity.

CPU overcommit needs a separate worksheet. At 128 cores and 30 configured one-vCPU sessions per core, the host would expose 3,840 vCPUs. An assumed 3% average duty cycle corresponds to 115.2 core-equivalents of mean demand. Synchronized work can still make all 3,840 vCPUs runnable and create queueing. Mean demand doesn't determine p99 startup or completion latency.

The declared worksheet has 128 physical cores and 30 one-vCPU sessions per core, giving 3840 configured vCPUs. On one zero-to-3840 core-equivalent demand scale, physical capacity is 128, assumed mean demand at 3 percent duty is 115.2, and synchronized runnable demand is 3840. The mean does not determine queueing or p99. Separately, one VMM fair-class cgroup with cpu.max 100000 100000 receives an aggregate 100000 microseconds per 100000-microsecond period. An illustrative split has thread 1 consume 60000 and thread 2 consume 40000 from that one shared budget, separated at the exact 60000 point. Guest process limits and host pids.max are separate controls.
Compare all upper bars on one core-equivalent scale: 128 physical cores, 128 × 30 × 0.03 = 115.2 mean demand, and 3,840 synchronized runnable vCPUs. Queueing and p99 require workload evidence. Separately, cpu.max = 100000 100000 grants one fair-class VMM cgroup an aggregate 100,000 µs per 100,000 µs period. The illustrative 60,000 + 40,000 µs split consumes one shared budget. Guest process limits and host pids.max enforce separate boundaries.

Place the VMM process and its tasks in the intended host cgroup. For fair-class CPU scheduling, cpu.max = 100000 100000 allows that group an aggregate 100,000 microseconds of CPU time per 100,000-microsecond period. It doesn't assign that allowance independently to every vCPU thread.[15]

Set memory.max with the guest footprint and VMM overhead in mind, monitor memory events, and define out-of-memory (OOM) handling. Host pids.max limits host tasks. Guest process limits constrain the untrusted processes inside that guest. Keep both controls, plus output, disk, and network limits, in the resource profile.[15]

Model page ownership and retirement locally

Before implementing the host agent, check the lifecycle with a small deterministic model. It represents three clean 4 KiB pages and separate private writes. Its readiness flags are supplied controller observations, and its retirement call represents a trusted confirmation that execution has stopped.

The program performs dictionary and integer operations in one Python process. It checks the model's transitions, while real readiness probes and resource retirement require a VMM integration test.

Predict two results: after A writes 99 into page 1, what does B read? After A requests cancellation, can cleanup already discard A's scratch?

sandbox-ownership-model.py
from dataclasses import dataclass, field

PAGE_BYTES = 4096
REQUIRED = frozenset({"clock", "kernel_rng", "app_state", "network", "control"})

@dataclass
class Clone:
    sandbox_id: str
    generation: int
    base: tuple[int, ...]
    state: str = "RESTORED"
    private: dict[int, int] = field(default_factory=dict)
    scratch: dict[str, bytes] = field(default_factory=dict)

    def admit(self, verified: set[str]):
        if self.state != "RESTORED" or not REQUIRED <= verified:
            raise RuntimeError("restore checks incomplete")
        self.state = "READY"

    def start(self):
        if self.state != "READY":
            raise RuntimeError("instance is not ready")
        self.state = "RUNNING"

    def read(self, page: int):
        if self.state != "RUNNING":
            raise RuntimeError("instance is not running")
        if type(page) is not int or not 0 <= page < len(self.base):
            raise RuntimeError("page is outside the modeled mapping")
        return self.private.get(page, self.base[page])

    def write(self, page: int, value: int):
        self.read(page)  # Require a running instance and an existing page.
        self.private[page] = value

    def request_cancel(self):
        if self.state != "RUNNING":
            raise RuntimeError("instance is not running")
        self.state = "CANCEL_REQUESTED"

    def confirm_retired(self, sandbox_id: str, generation: int):
        if (sandbox_id, generation) != (self.sandbox_id, self.generation):
            raise RuntimeError("retirement identity mismatch")
        if self.state != "CANCEL_REQUESTED":
            raise RuntimeError("retirement is out of order")
        self.state = "RETIRED"

    def dispose(self):
        if self.state != "RETIRED":
            raise RuntimeError("execution retirement required")
        self.private.clear()
        self.scratch.clear()
        self.state = "DISPOSED"

def rejected(call, expected):
    try:
        call()
    except RuntimeError as error:
        assert str(error) == expected
    else:
        raise AssertionError("invalid transition succeeded")

base = (10, 20, 30)
a, b = Clone("A", 1, base), Clone("B", 1, base)
rejected(lambda: a.admit({"clock"}), "restore checks incomplete")
assert a.state == "RESTORED"
for clone in (a, b):
    clone.admit(set(REQUIRED))
    clone.start()
assert a.base is b.base
rejected(lambda: a.write(-1, 99), "page is outside the modeled mapping")
a.write(1, 99)
a.scratch["output.csv"] = b"id,value\n1,99\n"
assert (a.read(1), b.read(1), base[1]) == (99, 20, 20)
page_accounting_bytes = (len(base) + len(a.private) + len(b.private)) * PAGE_BYTES
print("page 1: A =", a.read(1), "B =", b.read(1), "base =", base[1])
print("modeled page-accounting KiB:", page_accounting_bytes // 1024)

a.request_cancel()
rejected(a.dispose, "execution retirement required")
assert a.private and a.scratch
rejected(lambda: a.confirm_retired("A", 2), "retirement identity mismatch")
a.confirm_retired("A", 1)
a.dispose()
assert not a.private and not a.scratch and not b.scratch
assert b.read(1) == 20
print("A after confirmed retirement:", a.state)
print("B retains clean base and empty scratch:", not b.scratch)

usable_mib = (256 - 32) * 1024
print("full 2GiB budget quotient:", usable_mib // 2048)
print("50MiB private-page-only quotient:", usable_mib // 50)
print("mean core-equivalents at 3%:", f"{128 * 30 * 0.03:.1f}")
Output
page 1: A = 99 B = 20 base = 20
modeled page-accounting KiB: 16
A after confirmed retirement: DISPOSED
B retains clean base and empty scratch: True
full 2GiB budget quotient: 112
50MiB private-page-only quotient: 4587
mean core-equivalents at 3%: 115.2

A's write changes its private mapping. B keeps the clean value. The 16 KiB page-accounting total counts one shared three-page base plus A's one private page. This arithmetic assigns a page to each base entry and private entry; operating-system and VMM overhead remain outside the model.

The second result is a cleanup invariant: requesting cancellation leaves A's resources owned until the controller confirms retirement for A's exact generation. The model catches an out-of-order disposal and a stale generation. It doesn't establish that an actual device, process, mount, or remote request has stopped.

The local dictionary model takes A/generation 1 through RESTORED, READY, RUNNING, CANCEL_REQUESTED, RETIRED, and DISPOSED. Missing readiness observations keep it RESTORED and reject admission. Cancellation keeps its private page 1 value 99 and output.csv owned. A retirement notice for A/generation 2 fails identity. Authoritative retirement for A/generation 1 is a trusted model assertion. In the integration boundary, remaining references must also close before disposal and resource reuse. A lost reply or heartbeat keeps ownership uncertain for reconciliation, with no timeout-to-free edge. B/generation 1 continues reading base value 20 with empty scratch. This model does not probe real VMM retirement.
Cancellation keeps A’s private 99 page and output.csv owned. The wrong generation cannot retire them. In this model, confirm_retired for A/generation 1 supplies a trusted retirement assertion. A real integration must verify execution retirement and remaining reference closure before disposal and reuse. Lost replies require reconciliation while ownership stays held. B still reads 20 with empty scratch. The drawing executes no VMM retirement probe.

Reconcile failures before returning capacity

A controller can lose its reply while a VMM keeps running. Keep that instance uncertain, recover its authoritative state, and stop or adopt it under the same generation. A new controller should reconcile durable ownership with host processes, cgroups, sockets, volumes, and network objects.

FailureEvidence to inspectResponse owned by the platform
Runaway guest processesGuest process limits, vCPU usage, output growthEnforce guest limits and host CPU/output budgets
Private-memory growthHost cgroup memory events, resident pages, guest OOM stateReject new work, stop the affected instance under policy, and preserve host reserve
Lost controller heartbeatGeneration-bound process and resource inventoryReconcile ownership before adoption or termination
Stalled page handler or storageFault progress, backend errors, instance deadlineStop admission and retire the affected instance before reclaiming
Credential abuseBroker caller, capability, operation, resource, and upstream resultDeny unauthorized operations and revoke the affected capability
Cleanup incompleteLive process/FD references, mounts, network state, scratch ownershipQuarantine the instance resources until disposal is verified

For an owned non-root process cgroup in a valid domain configuration, Linux cgroup.kill can terminate the cgroup tree. In a threaded cgroup, writing cgroup.kill fails with EOPNOTSUPP because the operation targets whole processes. Verify process exit and the remaining resource references before returning capacity.[15] A PID number or a timed heartbeat alone doesn't identify every resource safely, especially after PID reuse.

Before allowing a new tenant, check that the old generation has stopped, scratch and writable memory are no longer reachable, capabilities are revoked, and exported artifacts follow their retention policy. Then test that the new instance sees only the clean base. Fault-inject controller crashes, delayed cleanup, repeated events, and cold-page failures in the actual runtime.

The complete deployment evaluation also needs patched host/guest configurations, guest-escape testing appropriate to the threat model, cross-tenant network probes, resource-pressure tests, and measurements of the full readiness endpoint. The local model establishes the stated arithmetic and transitions.

Follow-up questions

How would a sandbox read a 50 GiB model without copying it into its scratch layer?

Use an authorized read-only virtual block device backed by a managed image. Bind the image revision and access policy to the instance. Account for its I/O and resident page-cache working set, even when the underlying file is shared. A filesystem-sharing feature requires explicit support in the chosen VMM and guest configuration.

Why can a garbage collector increase private memory after restore?

Writes to marks, reference counts, or other heap metadata can break copy-on-write sharing. Reads can bring clean pages into RAM without making private modified copies. Measure the runtime's actual write pattern and resulting resident memory.

Check your understanding

Complete the lesson

Mastery Check

Answer every question, then check your score. Score 75% or higher to mark this lesson complete.

1.A and B share four modeled 4 KiB base entries [7, 11, 13, 17]. A writes 81 at index 2, then B writes 52 at that same index. What are A, B and the base reads at index 2, and the total modeled page-accounting size?
2.The supplied VMM sketch encounters three KVM_RUN returns in order: -1/EINTR with stop_requested false, 0 with MMIO, then -1/EINTR with stop_requested true. How many MMIO handler calls occur, and does the loop issue a fourth KVM_RUN?
3.A/generation 7 has current passing observations for clock, kernel_rng, network and control. Its only passing app_state record belongs to generation 6; the generation 7 app_state result is pending. What should the readiness boundary do?
4.A supplied request is accepted at 0 ms. Placement ends at 4 ms, snapshot loading at 6 ms, readiness checks at 17 ms, and the first user instruction at 18 ms. Against the article’s 15 ms acceptance-to-user-entry target, what duration and decision describe this request?
5.A clean lower image contains policy.txt and runtime.bin. In one guest overlay, A deletes policy.txt and creates result.json. After confirmed retirement and closed references, the next generation receives fresh upper/work directories. Which pair describes A’s pre-retirement view and the next generation’s view?
6.Under the taught bounded acknowledgement protocol, feed the reader the bytes "OK 1073741831\nPAYLOAD" in one stream, and separately "OK 2147483648\n". Which result matches its complete-line and port-range contract?
7.A new connection to an allowed API name now resolves only to an unauthorized private address, although an earlier connection used a public address. Separately, the API broker receives an expired capability for that API. Which pair of current actions follows the described boundary?
8.A supplied host has 192 GiB RAM with 24 GiB reserved, an assumed 96 MiB of private pages per job, and 96 cores. It configures 20 one-vCPU sessions per core at 4% mean duty. In the one-active-job-per-core burst model, what are the private-page-only quotient, mean core-equivalents and queued jobs when all sessions wake?
9.One fair-class VMM cgroup has cpu.max = "120000 100000". Its first thread consumes 70000 microseconds in the current period, with no other usage yet. What shared quota remains, and what quota/period mean allowance does this configuration represent?
10.A/generation 9 requests cancellation at 1 s. A generation-10 retirement notice arrives at 2 s. The controller verifies generation 9 execution retired at 5 s, but a duplicated VM descriptor and a scratch reader remain open until 9 s. All other capability-revocation and retention checks are complete. At which earliest supplied point can the platform reclaim those owned resources for another tenant?

10 questions remaining.

References

Firecracker: Lightweight Virtualization for Serverless Applications.

Agache, A., et al. · 2020 · NSDI 2020

https://www.usenix.org/conference/nsdi20/presentation/agache

Firecracker: Production Host Setup Recommendations

Firecracker Contributors · 2026

https://github.com/firecracker-microvm/firecracker/blob/main/docs/prod-host-setup.md

Firecracker Snapshotting and Restore Support

Firecracker Contributors · 2026

https://github.com/firecracker-microvm/firecracker/blob/main/docs/snapshotting/snapshot-support.md

The Definitive KVM (Kernel-based Virtual Machine) API

Linux Kernel Organization · 2026

https://docs.kernel.org/virt/kvm/api.html

Firecracker kernel policy and optional PCI VirtIO transport

Firecracker contributors · 2026

https://github.com/firecracker-microvm/firecracker/blob/ab198c7aa1f72cb30541165c20be27dbd1b88c2e/docs/kernel-policy.md

Firecracker Jailer

Firecracker contributors · 2026

https://github.com/firecracker-microvm/firecracker/blob/ab198c7aa1f72cb30541165c20be27dbd1b88c2e/docs/jailer.md

Firecracker: Handling page faults on snapshot resume

Firecracker contributors · 2026

https://github.com/firecracker-microvm/firecracker/blob/ab198c7aa1f72cb30541165c20be27dbd1b88c2e/docs/snapshotting/handling-page-faults-on-snapshot-resume.md

Firecracker: Uniqueness of clones

Firecracker contributors · 2026

https://github.com/firecracker-microvm/firecracker/blob/ab198c7aa1f72cb30541165c20be27dbd1b88c2e/docs/snapshotting/random-for-clones.md

Firecracker: Network configuration for snapshot clones

Firecracker contributors · 2026

https://github.com/firecracker-microvm/firecracker/blob/ab198c7aa1f72cb30541165c20be27dbd1b88c2e/docs/snapshotting/network-for-clones.md

Overlay Filesystem Documentation

Linux Kernel Organization · 2026

https://docs.kernel.org/filesystems/overlayfs.html

Firecracker vsock design and protocol

Firecracker contributors · 2026

https://github.com/firecracker-microvm/firecracker/blob/ab198c7aa1f72cb30541165c20be27dbd1b88c2e/docs/vsock.md

Python Socket Programming HOWTO

Python documentation contributors · 2026

https://docs.python.org/3/howto/sockets.html

Use IMDSv2 for Amazon EC2 Instance Metadata

Amazon Web Services · 2026

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html

Server-Side Request Forgery Prevention Cheat Sheet

OWASP Foundation · 2026

https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html

Control Group v2: resource limits and cgroup.kill

Linux Kernel Contributors · 2026

https://cdn.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html

Discussion

Questions and insights from fellow learners.

Discussion loads when you reach this section.