{"slug": "when-linux-cgroups-quietly-throttle-api-gateway-tcp", "title": "When Linux cgroups Quietly Throttle API Gateway TCP", "summary": "A CDN77 debugging report, published November 25 and surfaced on Hacker News, traced intermittent nginx throughput collapse to a Linux memory-cgroup signal that pushes affected TCP sockets onto a constrained receive-window path. The slowdown appeared only on cache misses, was limited to nginx processes, recovered without intervention, and did not reproduce with a direct curl from the same host. The report points to the version-specific tcp_under_memory_pressure() branch in Linux 6.19, which checks a socket's memory cgroup and its ancestors before global TCP memory pressure, recording a MEMCG_SOCK_THROTTLED event when a pressure timestamp is still active.", "body_md": "Originally published by Yilia Lin on [API7.ai](https://api7.ai/blog/linux-cgroup-tcp-throttling-api-gateway) on 2026-12-01.\n\nThis republication was prepared with AI assistance. The original article’s drafting process has not been verified.\n\nAn API gateway can have healthy CPU, free memory, clean logs, and a responsive origin while a subset of upstream transfers slows dramatically. That combination tempts teams to tune proxy buffers or blame the network. A recent Linux investigation showed why the evidence can point lower in the stack.\n\nOn November 25, CDN77 published [a debugging report about intermittent nginx throughput collapse](https://www.cdn77.com/blog/debugging-nginx-linux-contribution). The report reached the [Hacker News front page](https://news.ycombinator.com/item?id=50699391) because its symptoms were unusually deceptive: the slowdown affected nginx processes, appeared on cache misses, recovered without intervention, and did not reproduce with a direct `curl` from the same host. The cause was a Linux memory-cgroup signal that put affected TCP sockets on a constrained receive-window path.\n\nThe operational lesson is not that every slow API is a kernel bug. It is that gateway diagnosis must preserve the boundary between application latency, proxy behavior, TCP state, and cgroup accounting.\n\n`sock_throttled` in cgroup v2 `memory.events`; the counter shows that the pressure branch was used, not why reclaim became inefficient.\nCDN77's incident had four useful discriminators. The behavior was intermittent, connection-specific, limited to nginx, and visible when nginx fetched data on a cache miss. A direct transfer from the affected server was fast. Static configuration and neighboring servers looked alike, while CPU, host memory, and kernel logs did not explain the difference.\n\nThat shape narrows the search more effectively than a generic “gateway is slow” alert. Build a small comparison matrix before tuning anything:\n\n| Test | What it isolates | Interpretation limit | \n|---|---|---|\n| Cached response through gateway | Client-to-gateway path and local response delivery | Does not exercise the origin connection | \n| Cache miss through gateway | Full client → gateway → origin path | Still combines proxy and kernel behavior | \n| Direct request from gateway host to origin | Host network path without the gateway worker | Uses a different process and socket cgroup | \n| Same route on a neighboring gateway | Workload and origin with another data plane | Kernel, placement, and connection state can differ | \n\nIf only the cache-miss gateway path is slow, keep origin fetch, worker process, socket state, and service cgroup in scope. If the direct request is also slow, the proxy-specific hypothesis weakens. If cached responses degrade too, the bottleneck may be downstream or local rather than origin-facing.\n\nThis is the same path discipline required for [HTTP connection-reuse diagnosis](https://api7.ai/blog/http-connection-reuse-api-gateway-latency), but the mechanism here is different. Connection reuse concerns whether a socket returns to a pool. Cgroup socket pressure can constrain an established connection that is otherwise functioning.\n\nThe relevant branch is version-specific. In Linux 6.19, [`tcp_under_memory_pressure()`](https://github.com/torvalds/linux/blob/v6.19/include/net/tcp.h#L299-L311) first checks whether the socket's memory cgroup is under pressure and only then checks global TCP memory pressure. The cgroup helper walks the socket's cgroup and its ancestors. When a pressure timestamp is still active, that helper records a `MEMCG_SOCK_THROTTLED` event and returns true.\n\nThe signal comes from reclaim efficiency, not a direct comparison with total free host memory. Linux 6.19's [`vmpressure.c`](https://github.com/torvalds/linux/blob/v6.19/mm/vmpressure.c) uses a window of 512 scanned pages, documented there as 2 MiB when pages are 4 KiB. A medium pressure level begins when unsuccessful reclaim reaches 60 percent—equivalently, when less than 40 percent of the scanned pages were reclaimed. For the cgroup v2 socket-pressure path, the kernel asserts the pressure state for one second; later events can extend it.\n\nWhen TCP receive processing sees that state, it can take the pressure branch instead of growing the receive window normally. CDN77 observed a very small `rcv_ssthresh` and traced the path through `tcp_adjust_rcv_ssthresh()`. Because the pressure flag is cgroup-scoped, connections whose sockets are charged to that cgroup or a pressured ancestor can encounter the same branch.\n\nTwo qualifications belong beside that explanation:\n\n`memory.high` or `memory.max` was crossed. In the reported incident, loading the memory controller and service placement were enough to make the reclaim-efficiency signal relevant.\nApache APISIX 3.21's [Prometheus plugin](https://apisix.apache.org/docs/apisix/3.21/plugins/prometheus/) exposes `apisix_http_latency` with `request`, `upstream`, and `apisix` types. The `upstream` series measures time waiting on the upstream response. The `apisix` value is the request latency minus upstream latency and includes downstream traffic time plus NGINX-side time; it is not a pure Lua execution metric.\n\nThose definitions make the metric useful for phase separation:\n\nAdd transport evidence before declaring a kernel cause. Compare bytes per second, retransmissions, receive-window evolution, connection age, and origin timing for affected and unaffected sockets. Confirm which cgroup owns the nginx or APISIX worker. A packet capture can show window behavior, while a bounded `bpftrace` or eBPF probe in a test or incident window can observe the kernel branch. Apply production probes only with an owner, a time limit, and measured overhead.\n\nOn Linux 6.19, inspect the service's cgroup v2 files:\n\n```\ncat /sys/fs/cgroup/system.slice/apisix.service/memory.events.local\ncat /sys/fs/cgroup/system.slice/apisix.service/memory.stat\ncat /sys/fs/cgroup/system.slice/apisix.service/memory.pressure\n```\n\nThe [cgroup v2 documentation](https://github.com/torvalds/linux/blob/v6.19/Documentation/admin-guide/cgroup-v2.rst#memory-interface-files) defines `sock_throttled` as the number of times sockets associated with the cgroup are throttled. Use `memory.events.local` when you want events at that cgroup rather than the hierarchical totals in `memory.events`. Record a baseline and compare the counter delta with the exact incident window. A nonzero lifetime total is not proof that the current request was affected.\n\nOlder kernels may not expose this counter even when related pressure logic exists. In that case, do not convert “counter unavailable” into “mechanism absent.” Pin the running kernel and distribution patches, then use version-appropriate source, tracing, and vendor guidance.\n\nCDN77 temporarily moved nginx into the root cgroup because the investigated `vmpressure()` path returns for the root cgroup. That experiment removed the symptom in their environment, but it also changed memory accounting and out-of-memory behavior. Copying it blindly can reduce containment or move resource pressure elsewhere.\n\nUse a staged response instead:\n\nLinux 6.19's new counter improves observability; CDN77 explicitly said a proper fix would follow. Do not describe the counter itself as remediation. Likewise, increasing proxy buffers or upstream keep-alive limits without evidence can add memory or socket pressure while leaving the kernel path unchanged.\n\nHidden TCP throttling is a reminder that API latency is a path, not a single dashboard panel. Start with the failure shape, compare gateway and direct paths, separate upstream from downstream latency, and then join gateway evidence with socket and cgroup state.\n\nApache APISIX and API7 Gateway provide a useful observation boundary for route, node, status, and latency data. They do not replace kernel diagnosis, but they help identify which traffic phase deserves it. For a broader monitoring model, use the [API observability guide](https://api7.ai/blog/api-observability-guide), then add the Linux 6.19 cgroup evidence when the symptom matches this specific failure path.", "url": "https://wpnews.pro/news/when-linux-cgroups-quietly-throttle-api-gateway-tcp", "canonical_source": "https://dev.to/yilialinn/when-linux-cgroups-quietly-throttle-api-gateway-tcp-3b0a", "published_at": "2026-12-03 15:06:35+00:00", "updated_at": "2026-12-03 15:14:58.547201+00:00", "lang": "en", "topics": ["ai-infrastructure", "developer-tools"], "entities": ["CDN77", "nginx", "Linux", "API7.ai", "Hacker News", "Yilia Lin"], "also_reported_by": [], "takeaway": "In Linux 6.19, tcp_under_memory_pressure() checks the socket's memory cgroup and its ancestors before global TCP pressure, so a cgroup reclaim-efficiency signal can throttle established TCP connections even when host CPU and memory look healthy.", "recommendation": {"advice": "When only cache-miss gateway paths slow down, compare cached, cache-miss, direct-from-host and neighboring-gateway requests, and check cgroup v2 memory.events for sock_throttled before tuning proxy buffers.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/when-linux-cgroups-quietly-throttle-api-gateway-tcp", "markdown": "https://wpnews.pro/news/when-linux-cgroups-quietly-throttle-api-gateway-tcp.md", "text": "https://wpnews.pro/news/when-linux-cgroups-quietly-throttle-api-gateway-tcp.txt", "jsonld": "https://wpnews.pro/news/when-linux-cgroups-quietly-throttle-api-gateway-tcp.jsonld"}}