# When Linux cgroups Quietly Throttle API Gateway TCP

> Source: <https://dev.to/yilialinn/when-linux-cgroups-quietly-throttle-api-gateway-tcp-3b0a>
> Published: 2026-12-03 15:06:35+00:00

**Recommendation:** 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.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

Originally published by Yilia Lin on [API7.ai](https://api7.ai/blog/linux-cgroup-tcp-throttling-api-gateway) on 2026-12-01.

This republication was prepared with AI assistance. The original article’s drafting process has not been verified.

An 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.

On 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.

The 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.

`sock_throttled` in cgroup v2 `memory.events`; the counter shows that the pressure branch was used, not why reclaim became inefficient.
CDN77'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.

That shape narrows the search more effectively than a generic “gateway is slow” alert. Build a small comparison matrix before tuning anything:

| Test | What it isolates | Interpretation limit | 
|---|---|---|
| Cached response through gateway | Client-to-gateway path and local response delivery | Does not exercise the origin connection | 
| Cache miss through gateway | Full client → gateway → origin path | Still combines proxy and kernel behavior | 
| Direct request from gateway host to origin | Host network path without the gateway worker | Uses a different process and socket cgroup | 
| Same route on a neighboring gateway | Workload and origin with another data plane | Kernel, placement, and connection state can differ | 

If 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.

This 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.

The 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.

The 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.

When 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.

Two qualifications belong beside that explanation:

`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.
Apache 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.

Those definitions make the metric useful for phase separation:

Add 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.

On Linux 6.19, inspect the service's cgroup v2 files:

```
cat /sys/fs/cgroup/system.slice/apisix.service/memory.events.local
cat /sys/fs/cgroup/system.slice/apisix.service/memory.stat
cat /sys/fs/cgroup/system.slice/apisix.service/memory.pressure
```

The [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.

Older 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.

CDN77 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.

Use a staged response instead:

Linux 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.

Hidden 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.

Apache 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.
