blog.rust.gay

Fifteen million web servers, one VM, and a lot of missing RAM

The goal sounds like a joke, and the author knows it: run 15 million web servers on one VM with 10 GB of RAM and one CPU core. The post starts with a mock funding pitch and a promise to solve cybersecurity by making web servers so abundant that demand drops to zero. Then it does the actual work. The result is a proof of concept that reaches 16 million tiny web servers by moving the TCP/IP stack out of the Linux kernel and running the guests inside a custom userspace runtime.

The useful part is not the headline number. It is the memory accounting. A normal Linux TCP socket costs about 3,300 bytes before the application stores anything useful. At 15 million sockets, that is roughly 50 GB of kernel memory, which is five times the VM's entire budget. The project only becomes possible when the kernel is removed from the per-connection path. That is a concrete lesson for anyone building high-density network services, even if they never run 15 million servers.

The naive server hits three ceilings

The first version is a C server that calls socket, listen, accept, recv, send, and close. It returns a fixed OK response and closes the connection. That design gets about 65,000 requests per second on the test machine, which is respectable for a toy. It also fails immediately when you try to run 15 million copies.

The first ceiling is process memory. Each server process uses about 1.5 MB of RAM. Fifteen million of them would need roughly 20 TB. The VM has 10 GB. The process-per-server model is dead before the network stack matters.

The second ceiling is port space. A TCP port is a 16-bit field, so there are only 65,536 possible values. Linux's default ip_local_port_range is 32768 to 60999, which is smaller still. The post walks through the historical reasons: privileged ports, ephemeral port allocation, and the need to avoid collisions with services like MySQL. You can widen the range, and you can use UDP and TCP on the same port, but you still cannot reach 15 million listeners on one IP address.

The workaround is to use more addresses. One option is 450 extra network cards, each with its own IP. The other is AnyIP: route a whole subnet to the loopback interface and listen on every address in it. An IPv6 /64 gives you 18 quintillion addresses. An IPv4 range with 15 million addresses would cost around $250 million, which is a useful reminder that IPv4 scarcity is not just a consumer problem.

The third ceiling is the kernel socket itself. Even if you avoid one process per server, each Linux socket allocates memory for struct tcp_sock, struct socket_alloc, struct file, and a dentry. The post puts the total at around 3,300 bytes per socket. That is the number that kills the project at roughly 2.5 million servers. The kernel is doing bookkeeping that the application never asked for, and at this density the bookkeeping is the dominant cost.

Shrinking the guest

The first fix is to stop using one process per server. The post uses LFI, or Lightweight Fault Isolation, to run many guests inside one host process. LFI provides memory isolation and replaces guest syscalls with calls to a host handler. The host can implement I/O however it wants, including a custom network stack. A userland scheduler switches between guests, the way a runtime switches between green threads.

The minimal state for a guest is small. If the scheduler only switches at the main loop, it needs a program counter and two file descriptors: the listening socket and the client socket. That is 16 bytes per guest if you use a u64 and two i32s. Fifteen million guests would need about 220 MB. With bit packing, the post estimates 8 bytes per guest, or about 120 MB. That is a rounding error against the 10 GB budget.

The active connection is the expensive part. The post assumes about 5 KB per active TCP connection. If one million connections are active at once, that is 5 GB. The remaining 14 million idle servers are supposed to cost nothing. That assumption is wrong in the kernel, which is why the naive idle test goes out of memory. In the custom runtime, an idle guest really can be just a context and a listening address, so the math starts to work.

This is the core trick: separate the cost of being reachable from the cost of being active. A traditional server pays kernel memory for every listening socket, whether or not a client ever connects. A userspace runtime can make an idle listener a data structure, not a kernel object. That is how the project gets from 2.5 million to 16 million.

Moving TCP/IP out of the kernel

The network stack is the other half of the problem. The post uses smoltcp, a standalone, event-driven TCP/IP stack designed for bare-metal and real-time systems. smoltcp does not need heap allocation, but it also does not do I/O by itself. It needs a Device implementation that moves packets between the stack and the network.

There are three practical options. TUN and TAP are virtual interfaces provided by the kernel; they operate at the IP and Ethernet layers. DPDK talks directly to the network card, bypassing the kernel entirely. AF_XDP is a newer Linux subsystem that uses a memory-mapped ring and an eBPF filter to steer packets to userspace while the kernel still owns the device.

The trade-offs are concrete. DPDK gives the best performance because it avoids syscalls and interrupts, but it busy-polls a CPU core and takes exclusive control of the NIC. Once you bind a device to DPDK, it disappears from the normal network stack. If you do it over SSH, you lose SSH. AF_XDP is slightly slower, but the kernel still owns the device, interrupts still work, and SSH remains available. It also supports SO_PREFER_BUSY_POLL, which gets closer to DPDK's busy-loop behavior at the cost of a core.

The post's final flow is easy to describe and hard to build. The runtime loads 15 million guests into memory. The scheduler runs them one by one. Guest syscalls are intercepted and replaced by the host's smoltcp stack and its device adapter. Each guest listens on its own address. The scheduler polls the device, wakes the guest that owns the incoming packet, lets it process the request, and switches to the next guest.

The isolation model deserves scrutiny. LFI is not a virtual machine. The guests share a process, and the host runtime is the trusted computing base. LFI provides memory isolation and replaces syscalls with host handlers, but the host handler is now part of the attack surface. A bug in the handler can break the isolation for every guest in the process. That is a different risk profile from one process per tenant, and it is the main reason this design is a proof of concept rather than a multi-tenant service. The memory savings are real; the security trade-off is also real.

A simplified version of the address setup looks like this:

sudo ip -6 route add local 2001:db8:1234:5678::/64 dev lo

That single command gives the host an entire /64 to listen on. The server can pick the nth address from the range for the nth guest. It is a neat trick, and it is the kind of thing that makes the project possible without buying 450 network cards.

The result and the caveats

With smoltcp, the post reports about 620 bytes per idle server and around 1 KB per active server. That is enough to reach 16 million servers. DPDK performs best in the benchmark, with AF_XDP close behind. The kernel network stack runs out of memory around 2.5 million servers, which is the number that makes the comparison vivid.

The caveats matter as much as the result. This is a proof of concept, not a production web server. The guests run a tiny fixed response, not a full HTTP implementation. The runtime does not provide POSIX compatibility. The post says that building a compatible Linux runtime and reimplementing all the syscalls is a separate, much larger project. The security model is also not fully explored: LFI provides memory isolation, but running many guests in one process changes the failure and attack surface.

The benchmark is also a benchmark. It measures tiny requests, a fixed response, and a synthetic workload. It does not measure TLS handshakes, HTTP/2 multiplexing, keep-alive behavior, or the cost of a real application. A production service would need to handle those, and the memory per connection would rise. The headline "16 million web servers" is accurate for the workload that was tested. It is not a claim that a general-purpose web host can run 16 million WordPress sites on one VM.

The number is a proxy for a more useful metric: bytes per reachable endpoint. Fifteen million is memorable, but the engineering question is how much memory it costs to keep an endpoint alive when it is not serving a request. The post's answer is roughly 620 bytes with smoltcp, versus roughly 3,300 bytes for a kernel socket. That is a 5x improvement at the idle end, and it is the part that generalizes beyond this demo. If your service has millions of mostly idle connections, the same accounting applies.

Why it matters

The project is a useful stress test of a common assumption: that the kernel's networking stack is the right default for every workload. For most services, it is. The kernel is stable, well understood, and handles a wide range of protocols and devices. But at extreme density, the per-socket overhead becomes the dominant cost. A userspace stack can trade generality for memory efficiency. AF_XDP makes that trade less risky than DPDK because it keeps the kernel in the loop.

The other lesson is about measurement. The post starts with a simple server, measures it, and then removes one bottleneck at a time. The process memory, the port space, the kernel socket, the TCP stack, and the scheduler are all measured separately. That is a better debugging narrative than a single benchmark chart. It also makes the result reproducible: the code is linked in the post, and the steps are explicit.

If you are building a high-density proxy, a test harness, or a sandbox service, the techniques are worth studying. If you are building a normal web application, the kernel is probably fine. The interesting question is where the crossover is. At what connection count does a userspace stack pay for its complexity? The post does not answer that for every workload, but it gives a credible data point at the extreme end.

There is also a tension in the one-core target. DPDK achieves its performance by busy-polling a CPU core. If the VM has only one core, the poller and the guests are competing for the same resource. AF_XDP can run in interrupt mode or busy-poll mode, which gives the operator a dial between latency and CPU consumption. The benchmark result depends on which mode was used and how the scheduler was tuned. The post is transparent about the setup, but the headline number should not be read as "one core, no trade-offs."

The next thing to watch is AF_XDP tooling. It is the option that keeps SSH working, preserves the kernel's device ownership, and still avoids most of the per-packet cost. If the tooling matures, it could make high-density userspace networking practical for more than a proof of concept. Until then, 16 million web servers on one VM is a very good demo, not a deployment recommendation.

Sources


← back to posts