Node.js and the tale of worker threads
A Node.js service using cluster mode called os.cpus().length inside a Kubernetes container, which returns the host's core count, not the pod's CPU allocation. On a 48-core node it forked 48 processes inside a 2GB pod. Each needed about 120MB, so the processes were killed under memory pressure and immediately re-forked by the exit handler, flooding a translation service with startup requests (1,000 to 20,000 rpm) and causing cancelled connections and read timeouts. The initial diagnosis of a retry feedback loop caused by process.exit() on fetch failure turned out to be wrong. The fix was to drop cluster mode and let Kubernetes handle scaling, and the incident motivated adding event-loop lag and other signals through an internal Node.js observability SDK.
Related posts
- Fully Loaded Node – A Node.JS Holiday Season, part 2
Mozilla Hacks mediumAdeptNode.js runs JavaScript on a single core, so CPU-heavy work needs special handling to keep a server responsive. Mozilla Persona's team judged five designs against four goals: saturating all cores, staying responsive, degrading gracefully under overload and integrating simply. Running work on the main thread, wrapping it in a fake-async API, using threaded native libraries (capped by Node's 4-thread internal pool, which also starves I/O) and the cluster module all fall short. Their compute-cluster library runs dedicated worker processes behind an async API, tracks queue depth and average job time, and rejects requests up front when the predicted wait exceeds a client-supplied max_request_time.
- Node.js at scale, rebalanced: how we cut cost by 38%
Booking.com Engineering mediumAdeptBooking.com cut compute costs for its largest Node.js React SSR service by 38% (30% fewer pods, 20% less memory per pod, up to 10% lower tail latency) without touching service code, by moving from pm2's cluster-based processes to wattpm worker threads and revisiting long-untouched configuration. Synthetic k6 tests showed 20-50% more throughput within latency SLOs, validated through a side-by-side production A/B split at the routing layer. Production exposed uneven worker load because SO_REUSEPORT's connection hashing interacted badly with nginx loopback connections recycled via tcp_tw_reuse. Giving each worker its own port and load-balancing in nginx fixed it, which also opened the door to smarter balancing between heavy renders and light metadata requests.
- 2023-03-08 incident: A deep dive into our incident response
Datadog Engineering mediumAdeptDatadog's March 8, 2023 global outage, triggered by an automatic systemd upgrade that broke Kubernetes nodes across isolated regions on multiple clouds, tested an incident process built on out-of-band monitoring, senior incident-commander rotations, blameless culture and a 30-minute customer update cadence. Response was slowed by the loss of Datadog's own monitoring, intermittent failures of the incident tooling, and the assumption that isolated regions could not fail together, so diagnosing the Kubernetes node failure took over an hour. The response scaled to nearly 100 workstreams and 500-750 engineers in shifts, with workstream leads acting as untrained sub-commanders. Full infrastructure restoration took until 18:00-19:00 UTC.
- How We Found 7 TiB of Memory Just Sitting Around
Render mediumAdept
Render's Kubernetes clusters have an unusually large number of namespaces, and daemonsets that listwatch them, such as Vector's kubernetes_logs source, used gigabytes of memory per node and put load on the apiserver during rollouts. Render added an opt-in config option to Vector (insert_namespace_fields) that disables namespace watching, since its config only used namespace labels to identify user pods. A missed second kubernetes_logs source initially hid part of the savings. Per-pod memory fell from nearly 4 GiB to tens of MiB, which freed about 1 TiB on the largest cluster and just over 7 TiB across the fleet.
- Debugging Systems in the Cloud: MySQL, Kubernetes, and Cgroups
Shopify Engineering hardMasterSlow MySQL Pod startup and degraded query performance on long-running GKE nodes in Shopify's KateSQL platform traced back to a Linux kernel memory cgroup leak: roughly 50K zombie memory cgroups made mem_cgroup_commit_charge slow, so each 128MB mmap during InnoDB buffer pool initialization took about 100 ms. The investigation used strace, perf flamegraphs, slabtop and /proc/cgroups, and Google kernel engineers confirmed it as a side effect of slab reparenting on cgroup removal. Forcing kubepods memory.force_empty cut cgroups to about 1800, and an 80G buffer pool then initialized in five seconds. The permanent fix was upgrading to GKE 1.18 (COS 85, kernel 5.4). Interim options included recycling nodes, dropping caches and isolating short-lived Pods.