Cloud & Infrastructure · Paper 05 / 05

Firecracker: Lightweight Virtualization for Serverless Applications

Alexandru Agache, Marc Brooker, Andreea Florescu, Alexandra Iordache, Anthony Liguori, Rolf Neugebauer, Phil Piwonka, Diana-Maria Popa

2020 · NSDI · ~11 min read

Read the original paper

Firecracker is a minimal virtual machine monitor that lets AWS Lambda run thousands of customers’ functions on one host, each in its own KVM-isolated microVM. Read it for a clear comparison of isolation options and an honest account of what a stripped-down VMM gives up.

When AWS Lambda launched in 2014, each customer’s functions ran in containers inside EC2 virtual machines dedicated to that customer. The VM separated one customer from another; containers separated functions of the same customer. This was secure, but it wasted capacity. A customer with one small function still needed a whole VM, and functions from different customers could never share one, so hosts carried a lot of reserved but idle memory and CPU.

The Firecracker paper, from NSDI 2020, describes the virtual machine monitor AWS built so that thousands of functions from unrelated customers could share one host, each with the isolation of a virtual machine and close to the start-up cost and memory footprint of a container. After this page you should be able to explain why neither containers nor traditional VMs met Lambda’s requirements, what Firecracker removed from a VMM and why, how the jailer limits a compromised VMM, and what the design costs in I/O performance.

We will follow one invocation: the first call to a 128 MB Python function that has not run on this worker host before, and then what changes when calls arrive faster.

Six requirements that ruled out the obvious options

Section 2 of the paper lists what Lambda needed from its isolation layer. Isolation: code from different customers must be safe to run on the same hardware, including against privilege escalation and side channels. Overhead and density: thousands of functions per host, with little waste. Performance: close to native. Compatibility: arbitrary Linux binaries and libraries, without code changes. Fast switching: starting new functions and cleaning up old ones quickly. Soft allocation: overcommitting CPU and memory, with each function using only what it actually needs.

Then the paper tests each existing option against that list. The table below summarises the argument.

OptionWhat isolates tenantsWhy it fell short for Lambda
containersLinux namespaces, cgroups and seccomp on a shared kernelevery tenant can reach the host kernel’s large system-call surface; narrowing it with seccomp breaks compatibility
language-level sandboxesa language runtime such as a JavaScript enginecannot run arbitrary Linux binaries
user-space kernels such as gVisora reimplementation of the Linux interface in a separate processcompatibility gaps and extra overhead on system calls
traditional VMs with QEMUhardware virtualisation via KVMlarge device emulation code, more memory per VM, slower start
FirecrackerKVM plus a minimal VMM in a sandboxed processgives up devices and some I/O performance (see below)
containeryour functionruntimehost kernel · sharedhardwarevirtual machineyour functionguest kernelQEMU · 1.4M lines, BIOS, PCIKVM · hardwareFirecracker microVMyour functionguest kernelVMM · ~50k lines, 5 devicesKVM · hardwarehighlighted: the layer that decides the attack surface between tenants
Where the isolation boundary sits. Containers share the host kernel. A traditional VM puts hardware virtualisation between tenants but brings a large emulator. A microVM keeps the hardware boundary and removes most of the emulator.

The reasoning about QEMU deserves a precise reading. Hardware virtualisation was never the problem: KVM, which is part of Linux, uses the processor’s virtualisation support to isolate CPU and memory, and it is small and well reviewed. The problem was the VMM above it, the user-space program that emulates disks, network cards, buses and firmware for the guest. QEMU is over a million lines of code and can emulate a large range of hardware. Each emulated device is code that processes input from an untrusted guest, and that code was where VM escapes had historically been found.

What is left when you strip a VMM

Firecracker replaces QEMU and keeps KVM. It started from crosvm, Google’s VMM for Chrome OS, and is written in Rust, which rules out many memory-safety bugs in the code that parses guest input. The paper puts it at about 50,000 lines.

The device list is short on purpose. There is a virtio network device and a virtio block device, a serial console, and a minimal keyboard controller kept only so the guest can signal a shutdown. There is no BIOS: Firecracker loads a Linux kernel directly into guest memory and jumps to it. There is no PCI bus; devices are exposed over memory-mapped virtio. There is no USB, graphics or sound.

One omission shows the reasoning clearly. Many VMMs let the guest see a directory on the host through a shared file system. That would have been convenient for Lambda, since the function’s code lives on the host. Firecracker offers only a block device, because a file system protocol would expose a much larger part of the host kernel to the guest. The guest gets a disk image, and the guest kernel does all the file system work on its own side of the boundary.

Each microVM is a separate Firecracker process. That sounds wasteful, but it is how Firecracker reuses Linux instead of reimplementing it: the host scheduler schedules vCPU threads, cgroups limit CPU, memory is allocated lazily by the host kernel when the guest first touches it, and standard tools can see and kill each VM. The cost is that the host kernel remains part of what must be trusted and patched, and the paper says so.

A microVM is configured through a small REST API on a Unix socket. Starting one looks like this, using the open-source project’s API:

PUT /boot-source     {"kernel_image_path": "vmlinux", "boot_args": "console=ttyS0 reboot=k panic=1"}
PUT /drives/rootfs   {"drive_id": "rootfs", "path_on_host": "fn.ext4", "is_root_device": true, "is_read_only": false}
PUT /machine-config  {"vcpu_count": 1, "mem_size_mib": 128}
PUT /actions         {"action_type": "InstanceStart"}

The network and block devices can also be given rate limiters, token buckets for bandwidth and operations per second, so that one tenant cannot use up a host’s disk or network.

What happens when the guest sends a packet

To see where a microVM spends time, follow one network write from our function. The Python code calls an HTTP client, which writes to a socket, and the guest kernel’s virtio-net driver places a descriptor pointing at the packet into a ring buffer in guest memory. It then writes to a memory-mapped register to say there is work. That register is not real hardware, so the processor exits from the guest to KVM, and KVM returns control to the Firecracker thread that owns the device.

Firecracker reads the descriptor from guest memory, checks it, copies the packet out and writes it to a tap device on the host with an ordinary system call. When the write completes, it marks the descriptor used and asks KVM to inject an interrupt into the guest. Each step is cheap, but each exit and system call takes time that a process on bare metal would not spend, and a disk read or write follows the same pattern against a file on the host.

This path is also the attack surface. The descriptors and packets are data written by the guest, which the customer controls, and Firecracker’s device code is the first thing on the host to parse them. Keeping that code small, in Rust, and limited to a few devices is how the design reduces the chance that a crafted descriptor becomes an escape. The rate limiters sit on this path too, so one guest cannot flood the host’s network or disk.

Following one invocation into a slot

Section 4 of the paper describes how Lambda uses Firecracker. A request to invoke our function arrives at a front end, which asks the Worker Manager where to send it. The Worker Manager routes invocations of the same function to the same workers, so that warm environments get reused. On a worker host, each function’s environment is a slot: one microVM with a guest kernel, the language runtime and the customer’s code loaded. A slot handles one invocation at a time.

Our function has no slot yet. The Placement service picks a worker with capacity and a new slot is created. Firecracker starts the microVM; the paper measures boot times around 125 ms with a minimal guest kernel and configuration. Then the Python runtime starts inside it, the function’s module is imported, and the handler runs. The microVM boot is only part of the cold start the customer sees, and the runtime and the customer’s own initialisation can take longer.

Change one variable. Two seconds later the function is called again. The Worker Manager sends the call to the same worker and the existing slot handles it with no boot at all. Change another. Fifty calls arrive together. A slot handles one invocation at a time, so this means up to fifty slots, each its own microVM, possibly spread across several workers. Every one is isolated from the others by KVM, even though they are the same customer’s code.

Now the memory budget. Our function asks for 128 MB, and Firecracker’s own overhead per microVM is a few megabytes; the project promises under 5 MiB, and the paper’s measurements are in that range. That overhead is what makes density possible.

Predict first

A worker host has 128 GB of memory. Each function needs 128 MB. How many fit if each sandbox adds 5 MB of overhead (a microVM), and how many if each adds 512 MB (a typical traditional VM)? Then repeat for 1,024 MB functions.

Show the worked answer

For 128 MB functions: 131,072 MB divided by 133 MB is about 985 microVMs. Divided by 640 MB it is about 204 traditional VMs. The microVM fits almost five times as many.

For 1,024 MB functions: 131,072 divided by 1,029 is about 127, and divided by 1,536 it is about 85. The advantage drops to about one and a half times. Per-sandbox overhead matters most when functions are small, and most serverless functions are small.

These counts assume every byte of every function’s memory is in use. In practice most functions use less than they asked for and are idle much of the time, and the host only allocates guest memory when it is touched, which is why Lambda can overcommit further.

Try itHow many strangers fit on one machine?

Pick a host size and a function's memory. Density is what fits; the race is a cold start, drawn to scale.

sandboxisolationmemory overheadcold startfunctions per host
traditional VMhardware (hypervisor)~512 MB~8000 ms204
containershared kernel (namespaces)~8 MB~60 ms963
Firecracker microVMhardware (KVM) + minimal device model~5 MB~125 ms985
traditional VMcontainerFirecracker microVMbars fill in real time; the long one is the point
density gain vs VMs4.8×more functions per host at this size
start-up gain vs VMs64×faster cold start

Containers were already dense and fast, but they share the host kernel with every other tenant; a provider cannot put strangers' code there. Virtual machines isolate properly but cost hundreds of megabytes and seconds to start. Firecracker keeps the hardware boundary and throws away everything a serverless function will never use: no BIOS, no PCI bus, five devices, and a 50,000-line VMM instead of a million.

Overcommitting is the soft-allocation requirement in practice. It relies on functions on one host not all being busy at the same moment. Spreading a customer’s slots across many hosts, and mixing many customers on each host, keeps the load on any one host closer to an average.

When the guest breaks out: the jailer

AWS did not assume Firecracker would be bug-free. Each Firecracker process is started by a jailer, which puts it in a chroot containing only the files that microVM needs, in its own PID and network namespaces, under cgroups, and with its privileges dropped. It then applies a seccomp filter that allows only a short list of system calls, a couple of dozen, some with restrictions on their arguments.

host · KVM provides the hardware boundaryjailer: new namespaces, chroot, cgroups, dropped privilegesseccomp: the VMM may make ~24 system calls, nothing elseFirecracker VMM (Rust) → guest kernel → your functiondefence in depth: an escape from the guest still lands in a jail
Layers of the jailer. KVM provides the hardware boundary. The Firecracker process is further restricted by seccomp, a chroot, namespaces and cgroups, so an attacker who gets out of the guest must also break each of those.

Walk through a failure. An attacker controls the guest kernel, which is allowed: the customer can run anything in their own VM. They find a bug in Firecracker’s virtio-net emulation and gain code execution in the Firecracker process. Now what? The process cannot open files outside its chroot. It cannot call execve to start a shell, because that system call is not on the allowed list. It cannot see other microVMs’ processes because of the PID namespace. To reach another tenant, the attacker needs a second bug, this time in the host kernel, reachable only through the few system calls the filter still allows.

The cost of this protection falls on development. Any new feature that needs a new system call needs a change to the filter and a security review, which slows down adopting new kernel interfaces for performance.

The paper also discusses side channels. Hardware isolation does not prevent one tenant from learning about another through shared caches and processor structures, as the Spectre and L1TF vulnerabilities showed. The paper recommends host configuration such as disabling simultaneous multithreading and applying the kernel’s mitigations, and treats this as part of operating Firecracker safely rather than something the VMM can solve by itself.

What it gave up, measured

The evaluation in section 5 compares Firecracker with QEMU and with Intel’s Cloud Hypervisor. On boot time and memory overhead, Firecracker does well, which is what it was designed for. On I/O it is frank about weakness. Block device performance trailed QEMU, especially on large reads and writes, because Firecracker’s block device handled I/O serially at the time. Network throughput also fell short of what the hardware could do. For Lambda functions, which mostly make short network calls, this was acceptable. For a database it would not be.

Later work addressed the cold start more directly. Firecracker gained snapshot and restore, which saves an initialised microVM’s memory and device state so that new instances resume rather than boot. Lambda SnapStart, launched in 2022 for Java, is built on this. Snapshots bring their own problem: every restored copy starts with the same memory, including the state of random number generators, which the Firecracker team has written about separately. Another paper from the same team, on loading container images on demand in Lambda, describes how large function images are split into chunks and fetched lazily.

Walk through the snapshot problem with our function. Lambda initialises the function once, snapshots the microVM, and later restores two copies of that snapshot on different hosts to serve two invocations. Both copies resume with the same memory, so both hold the same pseudo-random generator state. If the function generates a request id, a session token or a key after resuming, the two copies can produce the same value. The fix is to tell the guest that it has been restored, so the kernel and the language runtime can reseed and refresh anything that must be unique. That fix costs cooperation: the guest kernel, the runtime and sometimes the customer’s own code have to respond to the signal, which is part of why snapshot-based starts arrived one runtime at a time.

Firecracker was open-sourced in 2018 and is used outside AWS, for example by Fly.io and as one runtime option for Kata Containers, which runs Kubernetes pods inside lightweight VMs. Its devices and virtualisation code share components with other Rust VMMs through the rust-vmm project. Google took the other path with gVisor, which implements the Linux system-call interface in a user-space kernel; it avoids the guest kernel but pays more per system call.

Firecracker fits short-lived, many-tenant workloads that need strong isolation and modest I/O. It fits less well when a workload needs devices it does not emulate, such as GPUs, or sustained high-throughput disk access. Those are the conditions that would send you back to a fuller VMM or to bare metal.

How to read the original

Section 2, on choosing an isolation approach, is the most useful part of the paper; it is a clear comparison of containers, language VMs, user-space kernels and virtualisation for a stated set of requirements. Read section 3 on Firecracker’s design, especially the parts about the device model and the jailer. Section 4, on Lambda’s architecture, explains slots, the Worker Manager and placement, and is where the microVM becomes a service.

In section 5, look at the boot time and memory overhead results first, then at the I/O figures, and read the authors’ explanations of where Firecracker falls behind. Note what each comparison configures for QEMU and Cloud Hypervisor, since boot times depend heavily on guest kernel and VMM settings. Section 6 discusses lessons and side channels, and is short.

That ends this group of papers. GFS, Dapper, the Tail at Scale, Borg and Firecracker each start from what goes wrong at scale: disks fail, requests cross too many machines, some server is always slow, machines sit idle, and tenants cannot be trusted. Each describes a design that treats that problem as normal.