Design an Agent Sandbox Platform (E2B / Daytona)
Sub-2s isolated code execution for 1,000+ concurrent AI agents: prewarmed Firecracker microVMs with CoW repo snapshots, host-side L7 Envoy egress & secret injection, S3 presigned artifacts, and dual-layer TTL reaping.
The whiteboard
Trace the 4 flows below to see sub-2s microVM provisioning, zero-trust egress, live vs. S3 file retrieval, and zombie-VM reaping. Builds on Distributed Locks & Leases, Queues vs Event Logs, and Design an Enterprise LLM Gateway.
whiteboard · press 1–5 to trace flows
Full platform: prewarmed Firecracker microVMs + CoW snapshots · host iptables + L7 Envoy secret injection · S3 artifacts & dual-layer TTL reaper1,000+ VMs · <2s startup · hard isolation
Scale & APIs
1,000+
concurrent active sandboxes (10s–30m runs)
< 2 s
startup with repo & deps mounted
0 secrets
raw credentials stored on VM filesystem
POST /sandbox -> 200 { sandbox_id }POST /sandbox/{id}/exec -> streams stdout/stderr (SSE / WS / gRPC) + exit_codeGET /sandbox/{id}/files?p= -> live stream (running) OR S3 presigned URL (archived)DELETE /sandbox/{id} -> 200 OK (upload artifacts to S3 & destroy microVM)How it works
1. Control plane & heartbeat scheduling
- Capacity-aware scheduling:
exec daemonrefreshes a Redis lease (heartbeat TTL) with available CPU/RAM slots.schedulerpicks a healthy node from Redis and writes(sandbox_id, tenant_id, node_id, status, expires_at, s3_path)to Postgres. - Admin control plane: Registers/drains execution nodes (write-through to Redis) and pushes egress allowlist policies.
2. Compute isolation & sub-2s startup (execution node)
- Firecracker microVMs over containers: Untrusted AI-generated code can exploit kernel CVEs in shared-kernel containers; Firecracker gives each sandbox its own minimal Linux guest kernel via KVM with strict cgroups/disk/PID quotas and
vsockcommand transport (no SSH daemon needed). - Prewarm pool (~150ms) + CoW snapshots: Pre-booted blank microVMs eliminate kernel boot latency, and mounting a Copy-on-Write (OverlayFS / ZFS) pre-baked repo + dependency snapshot eliminates 15–30s
git clone/npm installdelays.
3. Zero-trust network & secrets control
- L3/L4
iptables: Per-VM network namespaces block lateral VM-to-VM traffic and cloud metadata endpoints (169.254.169.254). - Host-side L7 Egress Proxy (Envoy): Domain allowlisting (
api.github.com,registry.npmjs.org,llm-gateway.internal) breaks with raw IP firewall rules because CDNs rotate IPs. Routing all egress through host Envoy solves both default-deny domain filtering and credential injection. - Zero raw secrets on VM disk: Never mount long-lived Vault secrets into the VM where prompt-injected code can
catthem. Inject only short-lived scoped tokens (e.g., 15-min GitHub App token) or let host Envoy attach auth headers on outbound requests.
4. Artifacts, file retrieval & cold archival
- Live vs. terminated routing:
filesresolvesnode_idvia Redis/Postgres and streams directly fromexec daemonwhile running. OnDELETE /sandbox/{id}(or completion),exec daemonuploads workspace artifacts to S3 and destroys the VM; futureGET /filescalls return an S3 presigned URL. - Lifecycle archival: S3 lifecycle rules transition aged artifacts to Glacier after 1 year and update Postgres metadata.
5. Dead-node recovery, hard TTL reaper & observability
- Dead-node detection (
recon): Watches Redisheartbeat TTL; if an execution node vanishes,reconmarks its sandboxessys_errorin Postgres so upstream durable orchestrators (Temporal) retry cleanly in a fresh sandbox. - Dual-layer hard TTL kill-switch:
exec daemonenforces a local hard timeout (e.g., 30 min) ANDreconsweepsexpires_at < now()in Postgres — killing runaway infinite-loop agents even on healthy nodes. - Tagged telemetry:
exec daemonstreams lifecycle events, CPU/RAM metrics, and logs to Kafka tagged with(tenant_id, run_id, trace_id).
lesson
Why L7 Envoy on the host beats pure iptables: Raw IP rules cannot reliably allowlist api.github.com or registry.npmjs.org behind rotating CDN IPs — and a host-side proxy lets you inject API/Git credentials outside the VM so untrusted agent code never sees raw tokens.
Check yourself
interactive · stress-test this sandbox design against security & infra probes
01 · Why Firecracker microVMs instead of standard Kubernetes containers?
untrusted code · multi-tenant · <2s boot
Your team already runs Kubernetes. Why pay the operational cost of Firecracker microVMs for running AI-generated agent code instead of regular Docker containers with resource limits?
Select an option to check the trade-off.
02 · Why inject credentials in a host-side L7 proxy instead of VM env vars?
prompt injection · default-deny egress · zero exfiltration
An agent needs to call your internal LLM gateway and push a PR to GitHub. Why have the host-side Envoy proxy inject auth headers instead of passing API keys as environment variables inside the microVM?
Select an option to check the trade-off.
03 · Why enforce a hard TTL in BOTH Exec Daemon and Recon?
zombie VMs · node crash · billing safety
Exec Daemon already runs a local 30-minute timer to kill runaway microVMs. Why does Recon also sweep Postgres for expires_at < now()?
Select an option to check the trade-off.
leasecoordination
Time-bounded exclusive ownership of a resource (like a machine ID or lock) that expires automatically if the holder crashes.