Tail Latency and Garbage Collection in Go and Node.js

A look at how V8 garbage collection pauses showed up in p99 latency on one Node.js service, and how Go's concurrent collector behaved in a similar test.

· 2 min read · Erik Kubica

Average latency can hide a lot, so when someone tells me their Node.js API averages 6 milliseconds, I usually ask to see the p99 and p99.9 graphs too.

On the projects where I've looked, the average often sits at 6 ms while the 99th percentile has a saw-tooth shape, spiking to 180 ms or 450 ms.

In 2023, I looked into this pattern on an e-commerce checkout service. Customers were intermittently abandoning carts because the payment tokenization endpoint would freeze for half a second.

In that case it was not slow SQL or network jitter. It was the V8 garbage collector running full mark-sweep cycles.

How V8 and Go Handle Memory Differently

Both Node.js (via Google's V8 engine) and Go manage memory automatically using garbage collectors, but their design priorities differ.

V8 was designed for web browsers, where interactivity matters most. It divides the heap into generations: New Space (nursery) and Old Space. It uses Scavenge collectors for young objects and a Mark-Sweep-Compact collector for long-lived objects.

When a Node API creates millions of temporary JSON objects, buffers, and strings per second, the Old Space can fragment. Eventually, V8 triggers a major GC cycle:

$ node --trace-gc dist/server.js
[18412:0x5241000]   14210 ms: Scavenge 48.2 (56.0) -> 38.1 (58.0) MB, 4.21 / 0.00 ms
[18412:0x5241000]   18940 ms: Mark-sweep 184.2 (212.0) -> 142.1 (214.0) MB, 78.42 / 0.00 ms (average mu = 0.812)

Look at that second line: Mark-sweep ... 78.42 ms.

For about seventy-eight milliseconds, the single V8 event loop thread stopped executing JavaScript, and incoming HTTP requests queued in socket buffers.

Go's Tri-Color Concurrent Collector

Go made a different trade-off: it favors low latency over raw peak throughput.

Go uses a concurrent tri-color mark-sweep collector. It does not move objects around in memory (non-moving, non-generational). Instead, it runs GC phases concurrently alongside application goroutines, using roughly 25% of the available CPU (GOMAXPROCS) to clean memory in the background.

Enabling GC tracing in Go shows what happens under a similar workload:

$ GODEBUG=gctrace=1 ./api
gc 14 @12.412s 2%: 0.042+1.12+0.038 ms clock, 0.33+0.42/1.8/0.84+0.30 ms cpu, 42->44->22 MB, 45 MB goal, 0 MB stacks, 8 P

Look at the clock pause times:

0.042 ms + 1.12 ms + 0.038 ms

The stop-the-world (STW) pauses are forty-two and thirty-eight microseconds. The concurrent phase takes 1.12 milliseconds, but user requests keep executing on other CPU cores meanwhile.

A Benchmark of Tail Latency

I ran a sustained load test against both runtimes using hey, allocating 2 MB of JSON payloads per request:

$ hey -z 30s -c 100 -q 50 http://127.0.0.1:8080/allocate

These were the latency distributions on my setup:

Metric Node.js (v20) Go (1.23)
Average (p50) 5.8 ms 2.1 ms
90th Percentile (p90) 18.4 ms 3.4 ms
99th Percentile (p99) 194.2 ms 4.8 ms
Max Latency 482.0 ms 14.2 ms

In this test, Node's p99 climbed to nearly 200 ms whenever V8 swept the heap, while Go's p99 stayed under 5 ms. It is a deliberately allocation-heavy test, so your numbers will depend on how your service allocates.

Average latency is worth watching, but the 99th percentile is where GC pauses tend to show up.

If your SLA needs predictable response times and your service allocates heavily, it is worth checking your tail latency before choosing a runtime. Node with some tuning, or less allocation per request, may well be enough, and Go's concurrent collector is one option that kept tail latency flat in my tests.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.