10,000 Concurrent Connections in Go and PHP: A Memory Comparison
A rough memory comparison of holding 10,000 idle connections with PHP-FPM workers versus Go goroutines, and when each makes sense.
· 3 min read · Erik Kubica
PHP's share-nothing model made a lot of sense when I started. A request came in, Apache or PHP-FPM spawned a process, executed the script, tore down every allocated variable, and freed the memory back to the kernel.
If a script leaked memory or crashed on a segmentation fault, it died in isolation. The server stayed up, other users were unaffected, and shared hosting worked well.
Today many web applications also hold long-lived server-sent event streams, subscribe to real-time notification buses, or keep WebSocket connections open. That is where PHP's process model starts to cost real memory, and where I noticed the difference on a couple of projects.
The Arithmetic of Process Memory
In PHP-FPM, every concurrent connection needs a whole worker process. Even if the script only idles on a socket waiting for an event, that worker stays in memory.
Here is the math for 10,000 concurrent connected clients, using a rough 40 MB per worker:
10,000 idle clients * 40 MB per PHP-FPM worker = 400,000 MB (400 GB RAM)
That is around 400 GB of RAM to hold 10,000 connections that are mostly doing nothing. In practice you would hit process limits or cost limits long before that, which is why people usually reach for something else for this workload.
$ free -m
total used free shared buff/cache available
Mem: 386920 354100 12400 24 20420 31200
Swap: 8192 4120 4072
Go: 2 KB Goroutine Stacks
Here is how the Go runtime handles the same scenario.
When you handle an incoming connection in Go, you do not fork a process. You do not even allocate an operating system thread, which on Linux carries an 8 MB default stack reservation (ulimit -s).
Go spawns a goroutine instead. A goroutine begins with a small 2 KB stack:
package main
import (
"net/http"
"time"
)
func handleEvents(w http.ResponseWriter, r *http.Request) {
// Each connection lives in a 2 KB goroutine
flusher, ok := w.(http.Flusher)
if !ok {
return
}
w.Header().Set("Content-Type", "text/event-stream")
for {
select {
case <-r.Context().Done():
return
case <-time.After(5 * time.Second):
w.Write([]byte("data: ping\n\n"))
flusher.Flush()
}
}
}
The same 10,000-connection math in Go:
10,000 concurrent goroutines * 2 KB = 20,480 KB (20.48 MB RAM)
About 20 MB, compared with the 400 GB above. Real numbers are higher once you add buffers, but the order of magnitude is what matters here.
$ ss -s
Total: 10420
TCP: 10014 (estab 10002, closed 0, orphaned 0, timewait 0)
For this kind of workload, the gap between a process per connection and a goroutine per connection is several orders of magnitude.
What I Saw Under Load
I tried this on an ordinary 4-core virtual server costing about 12 € a month. With 10,000 concurrent clients connected and streaming telemetry:
- Go memory footprint: 34 MB RSS total (including network socket buffers).
- CPU utilization: Between 1.8% and 3.4%.
- Context switches: Handled by the Go scheduler in user space, so the Linux kernel was not doing much of the work.
This is one setup, not a benchmark suite, so treat the numbers as a rough idea.
PHP was not designed around long-lived concurrent sockets, and in my experience holding them with process pools gets expensive quickly. There are PHP options built for this (Swoole, ReactPHP and similar) that I have not compared here, and PHP is still a good fit for short, stateless requests. For anything holding a persistent socket, Go is what I reach for.