{
  "url": "https://www.cdn77.com/blog/debugging-nginx-linux-contribution",
  "title": "Uncovering Hidden TCP Throttling in the Linux Kernel",
  "byline": "(c) 2026 DataCamp Limited",
  "siteName": "CDN77",
  "leadImage": "https://images.prismic.io/cdn77-blog/FW32k7i6AZRBxaCn_nginx-cdn.png?auto=format,compress",
  "wordCount": 1316,
  "readingTimeMinutes": 6,
  "cacheStatus": "HIT",
  "markdown": "# Uncovering Hidden TCP Throttling in the Linux Kernel\n\n**Disclaimer: No AI was used for writing this blog. Enjoy a humanly engineered piece.**\n\n  \nOne day, a customer was complaining about slow download speeds from one of our edge servers. Since the internet is full of unpredictability, complaints like that occasionally happen, but this time, there were no error codes. We couldn't reproduce the issue. The only thing we could see was lower traffic on the network interface. The strangest part was that it happened randomly, recovered on its own, and it didn't affect all clients connected to that server.\n\nCDNs are usually built in a multi-layer setup. When a client requests content, the closest edge node serves it from its cache. If the content isn't there, the edge fetches it from an upper layer. The request can travel further up until it reaches a shield node, which finally fetches the content from the customer's origin. At CDN77, all these layers run heavily modified nginx. As we would eventually discover, nginx was an important part of this story.\n\n![](https://images.prismic.io/cdn77-blog/FW32k7i6AZRBxaCn_nginx-cdn.png?auto=format,compress)\n\nHere's what we could see internally about the complaint: The download speed would collapse for certain connections, stay low for hours, and then recover without anyone touching anything. In one instance, it dropped just before 3 a.m., stayed flat for five hours, and returned to normal around 8 a.m. Nothing told us why. \n\n![](https://images.prismic.io/cdn77-blog/dcGgHIhMioygXBQU_CDN77-download-speed-graph.jpg?auto=format,compress)\n\nSo we started with the usual suspects: Maybe it was an ISP, peering, or transit issue; some network problem outside our infrastructure. Maybe one of the kernel's TCP settings was misconfigured - the well-known **tcp\\_rmem**, **tcp\\_wmem**, or **tcp\\_mem**. Or perhaps something in the nginx configuration, like proxy buffering. We also considered disk I/O stalling the cache, CPU or memory saturation, something hidden in the kernel logs, or a particular kernel version.\n\nAll reasonable possibilities. And every one of them led to a dead end. Over time, the issue appeared in multiple regions, ruling out a specific ISP or network path. The sysctl settings and nginx configuration were identical to those on healthy neighboring servers. So were the nginx and kernel versions. CPU utilization, memory usage, and kernel logs showed no discrepancies either.\n\nBut we noticed something interesting: **The issue only appeared on HTTP cache misses**. So, when the bug was happening and actively throttling, we launched a **curl** command on that server to test the download speed. But it was working; only nginx downloaded it slowly. We also noticed that rebooting the server fixed the issue for quite some time. Until, of course, it happened again. At this point, we had a pretty strange combination of symptoms. The problem was:\n\n-   Dynamic,\n-   Self-healing,\n-   Affected only nginx processes,\n-   Appeared on cache-miss connections.\n\nStatic config reviews weren't getting us anywhere. We needed to see what was actually happening.\n\n### Going further down the rabbit hole\n\nSince only nginx seemed to be affected, we were suspicious of userspace. Maybe nginx was delaying its read syscalls. Maybe it wasn't reading enough bytes per call. We were even desperate enough to suspect that **epoll** itself was broken. We hooked into the entry and exit points of the **read** and **epoll** related syscalls, capturing function arguments, return codes, and syscall durations. If nginx was spending too much time waiting, or doing something strange while reading data, we should have been able to see it. But the experiments showed no anomalies whatsoever. Nginx wasn't delaying its read syscalls. It was issuing them as fast as possible. Was it reading too few bytes? No. It was reading everything available until it hit **EAGAIN**, exactly as expected.\n\nSo userspace probably wasn't the culprit.\n\nTime to look into the kernel.\n\nWhat if... the bug was in the kernel?\n\nWe hooked a kprobe into **tcp\\_rcv\\_established** using **bpftrace** and started printing fields from **struct tcp\\_sock** that could plausibly explain a throughput collapse. Fields like **rcv\\_ssthresh**, the advertised maximum segment size, the send congestion window, **app\\_limited**, the receive window, etc. (Note: The receive window was our main suspect.)\n\n```\nstruct tcp_sock {\n...\n    u32    window_clamp;   /* Maximal window to advertise */\n    u32    rcv_ssthresh;    /* Current window clamp */\n...\n    u16    advmss;          /* Advertised MSS */\n    u32    snd_cwnd;        /* Sending congestion window */\n...\n    u32    app_limited;     /* limited until \"delivered\" reaches this val */\n...\n    u32    rcv_wnd;         /* Current receiver window */\n...\n};\n```\n\nAnd finally, we started seeing a pattern. The value of **rcv\\_ssthresh** was tiny. **Exactly four times the advertised maximum segment size**. And that value is only ever written by **tcp\\_adjust\\_rcv\\_ssthresh()**. In our case, the only viable path leading to that function was through **tcp\\_grow\\_window()**, but only when **tcp\\_under\\_memory\\_pressure()** returned true.\n\n```\n/* Check #1 */\nif (!tcp_under_memory_pressure(sk)) {\n    unsigned int truesize = truesize_adjust(adjust, skb);\n    int incr;\n\n    /* Check #2. Increase window, if skb with such overhead\n     * will fit to rcvbuf in future.\n     */\n    if (tcp_win_from_space(sk, truesize) <= skb->len)\n        incr = 2 * tp->advmss;\n    else\n        incr = __tcp_grow_window(sk, skb, truesize);\n\n    if (incr) {\n        incr = max_t(int, incr, 2 * skb->len);\n        tp->rcv_ssthresh += min(room, incr);\n        inet_csk(sk)->icsk_ack.quick |= 1;\n    }\n} else {\n    /* Under pressure:\n     * Adjust rcv_ssthresh according to reserved mem\n     */\n    tcp_adjust_rcv_ssthresh(sk);\n}\n```\n\nBut that was suspicious. Earlier in our debugging, we'd checked the SNMP counters and they reported zero memory pressure, none whatsoever. And yet, **tcp\\_under\\_memory\\_pressure()** was returning true. Something didn't add up.\n\n```\n$ nstat -z | grep Pressure\nTcpExtTCPMemoryPressures        0                  0.0\nTcpExtTCPMemoryPressuresChrono  0                  0.0\n```\n\n## Finding the hidden rabbit\n\nIt turns out that the TCP variant of the memory pressure check is implemented differently from others. **Besides checking global TCP memory pressure, it also checks whether the socket's cgroup is under memory pressure**. This additional check happens when the cgroup memory controller is loaded, which it is by default on most distributions.\n\n```\n/* optimized version of sk_under_memory_pressure() for TCP sockets */\nstatic inline bool tcp_under_memory_pressure(const struct sock *sk)\n{\n    if (mem_cgroup_sockets_enabled && sk->sk_memcg &&\n        mem_cgroup_under_socket_pressure(sk->sk_memcg))\n        return true;\n\n    return READ_ONCE(tcp_memory_pressure);\n}\n```\n\nAnd then it clicked. It was the cgroup memory controller all along. We launch nginx as a systemd service, and systemd places each service into its own cgroup and sets default controllers, like the already mentioned memory controller. The memory controller helps you set limits for the maximum memory consumption of a service, which we are actually not using. It's the well-known **memory.high, memory.max**, etc. But the memory controller has an undocumented feature, which activates when the controller is only loaded.\n\nThe next diagram summarises what was happening. There's a flag which is set per cgroup, not per socket. So one reclaim shortfall throttles every connection nginx owns. When there's memory pressure inside a cgroup, reclaim falls short, and it sets a pressure flag on the **memcg**, which is a cgroup-wide option that shrinks every socket buffer, basically collapsing all the throughput. Once the pressure clears, it gets back to the normal state.\n\n![](https://images.prismic.io/cdn77-blog/SqwYTW-C42md-9Mr_nginx-issue.png?auto=format,compress)\n\nThe pressure is raised through the **vmpressure()** function, which returns immediately for the root cgroup. So it only does anything for non-root cgroups. If the level rises above low, it writes **jiffies + HZ** into the memcg socket pressure, and every socket in that cgroup is throttled for at least one second.\n\n```\nvoid vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,\n        unsigned long scanned, unsigned long reclaimed)\n{\n    ...\n    if (!memcg || mem_cgroup_is_root(memcg))\n        return;\n\n    ...\n\n    if (level > VMPRESSURE_LOW) {\n        ...\n        WRITE_ONCE(memcg->socket_pressure, jiffies + HZ);\n    }\n    ...\n}\n```\n\nYou might be asking, what actually raised that pressure?\n\nMemory reclaim reports how many pages it scanned and how many it managed to free per cgroup. Once roughly two megabytes' worth of pages have been scanned, the kernel turns that into a ratio. If it freed less than about 40% of what it walked, that counts as memory pressure, and every socket in the cgroup gets throttled for that one second. **What's NOT in the formula is how much free memory the machine has**. It's purely a measure of reclaim efficiency over a tiny two-megabyte window. So a cache-heavy server where reclaim keeps walking pages fails constantly, even though it has hundreds of gigabytes of free RAM. The two-megabyte window was a hardcoded constant in the kernel version we were debugging, with a TODO: in the kernel asking it to scale with the machine size.\n\n```\n/*\n* TODO: Make the window size depend on machine size, as we do for vmstat\n* thresholds. Currently we set it to 512 pages (2MB for 4KB pages).\n*/\nstatic const unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16;\n```\n\n## Patching the rabbit hole\n\nSo, our temporary fix. Remember that **vmpressure()** returns immediately for the root cgroup? We moved the nginx processes into the root cgroup. No more socket pressure from a reclaim shortfall on a few pages. Also no out-of-memory kills. So even the throttling wasn't justified.\n\n```\nvoid vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,\n        unsigned long scanned, unsigned long reclaimed)\n{\n    ...\n    if (!memcg || mem_cgroup_is_root(memcg))\n        return;\n\n    ...\n\n    if (level > VMPRESSURE_LOW) {\n        ...\n        WRITE_ONCE(memcg->socket_pressure, jiffies + HZ);\n    }\n    ...\n}\n```\n\nWe reported this upstream, because at the time there was no way to even observe this pressure, no counter for it anywhere. With help from the Linux memory subsystem maintainers, we contributed a patch. **Since Linux 6.19, there's a new counter in the memory.events file for sockets throttled by this mechanism.**\n\n```\n$ cat /sys/fs/cgroup/**/memory.events\n...\nsock_throttled 0\n```\n\n**P.S. The added counter is nice, but we'll share a proper fix. Stay tuned.**",
  "html": "<h1>Uncovering Hidden TCP Throttling in the Linux Kernel</h1>\n<p><strong>Disclaimer: No AI was used for writing this blog. Enjoy a humanly engineered piece.</strong></p>\n<p>One day, a customer was complaining about slow download speeds from one of our edge servers. Since the internet is full of unpredictability, complaints like that occasionally happen, but this time, there were no error codes. We couldn't reproduce the issue. The only thing we could see was lower traffic on the network interface. The strangest part was that it happened randomly, recovered on its own, and it didn't affect all clients connected to that server.</p>\n<p>CDNs are usually built in a multi-layer setup. When a client requests content, the closest edge node serves it from its cache. If the content isn't there, the edge fetches it from an upper layer. The request can travel further up until it reaches a shield node, which finally fetches the content from the customer's origin. At CDN77, all these layers run heavily modified nginx. As we would eventually discover, nginx was an important part of this story.</p>\n<p><img src=\"https://images.prismic.io/cdn77-blog/FW32k7i6AZRBxaCn_nginx-cdn.png?auto=format,compress\" alt=\"\" /></p>\n<p>Here's what we could see internally about the complaint: The download speed would collapse for certain connections, stay low for hours, and then recover without anyone touching anything. In one instance, it dropped just before 3 a.m., stayed flat for five hours, and returned to normal around 8 a.m. Nothing told us why. </p>\n<p><img src=\"https://images.prismic.io/cdn77-blog/dcGgHIhMioygXBQU_CDN77-download-speed-graph.jpg?auto=format,compress\" alt=\"\" /></p>\n<p>So we started with the usual suspects: Maybe it was an ISP, peering, or transit issue; some network problem outside our infrastructure. Maybe one of the kernel's TCP settings was misconfigured - the well-known <strong>tcp_rmem</strong>, <strong>tcp_wmem</strong>, or <strong>tcp_mem</strong>. Or perhaps something in the nginx configuration, like proxy buffering. We also considered disk I/O stalling the cache, CPU or memory saturation, something hidden in the kernel logs, or a particular kernel version.</p>\n<p>All reasonable possibilities. And every one of them led to a dead end. Over time, the issue appeared in multiple regions, ruling out a specific ISP or network path. The sysctl settings and nginx configuration were identical to those on healthy neighboring servers. So were the nginx and kernel versions. CPU utilization, memory usage, and kernel logs showed no discrepancies either.</p>\n<p>But we noticed something interesting: <strong>The issue only appeared on HTTP cache misses</strong>. So, when the bug was happening and actively throttling, we launched a <strong>curl</strong> command on that server to test the download speed. But it was working; only nginx downloaded it slowly. We also noticed that rebooting the server fixed the issue for quite some time. Until, of course, it happened again. At this point, we had a pretty strange combination of symptoms. The problem was:</p>\n<ul>\n<li>Dynamic,</li>\n<li>Self-healing,</li>\n<li>Affected only nginx processes,</li>\n<li>Appeared on cache-miss connections.</li>\n</ul>\n<p>Static config reviews weren't getting us anywhere. We needed to see what was actually happening.</p>\n<h3>Going further down the rabbit hole</h3>\n<p>Since only nginx seemed to be affected, we were suspicious of userspace. Maybe nginx was delaying its read syscalls. Maybe it wasn't reading enough bytes per call. We were even desperate enough to suspect that <strong>epoll</strong> itself was broken. We hooked into the entry and exit points of the <strong>read</strong> and <strong>epoll</strong> related syscalls, capturing function arguments, return codes, and syscall durations. If nginx was spending too much time waiting, or doing something strange while reading data, we should have been able to see it. But the experiments showed no anomalies whatsoever. Nginx wasn't delaying its read syscalls. It was issuing them as fast as possible. Was it reading too few bytes? No. It was reading everything available until it hit <strong>EAGAIN</strong>, exactly as expected.</p>\n<p>So userspace probably wasn't the culprit.</p>\n<p>Time to look into the kernel.</p>\n<p>What if... the bug was in the kernel?</p>\n<p>We hooked a kprobe into <strong>tcp_rcv_established</strong> using <strong>bpftrace</strong> and started printing fields from <strong>struct tcp_sock</strong> that could plausibly explain a throughput collapse. Fields like <strong>rcv_ssthresh</strong>, the advertised maximum segment size, the send congestion window, <strong>app_limited</strong>, the receive window, etc. (Note: The receive window was our main suspect.)</p>\n<pre><code>struct tcp_sock {\n...\n    u32    window_clamp;   /* Maximal window to advertise */\n    u32    rcv_ssthresh;    /* Current window clamp */\n...\n    u16    advmss;          /* Advertised MSS */\n    u32    snd_cwnd;        /* Sending congestion window */\n...\n    u32    app_limited;     /* limited until \"delivered\" reaches this val */\n...\n    u32    rcv_wnd;         /* Current receiver window */\n...\n};\n</code></pre>\n<p>And finally, we started seeing a pattern. The value of <strong>rcv_ssthresh</strong> was tiny. <strong>Exactly four times the advertised maximum segment size</strong>. And that value is only ever written by <strong>tcp_adjust_rcv_ssthresh()</strong>. In our case, the only viable path leading to that function was through <strong>tcp_grow_window()</strong>, but only when <strong>tcp_under_memory_pressure()</strong> returned true.</p>\n<pre><code>/* Check #1 */\nif (!tcp_under_memory_pressure(sk)) {\n    unsigned int truesize = truesize_adjust(adjust, skb);\n    int incr;\n\n    /* Check #2. Increase window, if skb with such overhead\n     * will fit to rcvbuf in future.\n     */\n    if (tcp_win_from_space(sk, truesize) &lt;= skb-&gt;len)\n        incr = 2 * tp-&gt;advmss;\n    else\n        incr = __tcp_grow_window(sk, skb, truesize);\n\n    if (incr) {\n        incr = max_t(int, incr, 2 * skb-&gt;len);\n        tp-&gt;rcv_ssthresh += min(room, incr);\n        inet_csk(sk)-&gt;icsk_ack.quick |= 1;\n    }\n} else {\n    /* Under pressure:\n     * Adjust rcv_ssthresh according to reserved mem\n     */\n    tcp_adjust_rcv_ssthresh(sk);\n}\n</code></pre>\n<p>But that was suspicious. Earlier in our debugging, we'd checked the SNMP counters and they reported zero memory pressure, none whatsoever. And yet, <strong>tcp_under_memory_pressure()</strong> was returning true. Something didn't add up.</p>\n<pre><code>$ nstat -z | grep Pressure\nTcpExtTCPMemoryPressures        0                  0.0\nTcpExtTCPMemoryPressuresChrono  0                  0.0\n</code></pre>\n<h2>Finding the hidden rabbit</h2>\n<p>It turns out that the TCP variant of the memory pressure check is implemented differently from others. <strong>Besides checking global TCP memory pressure, it also checks whether the socket's cgroup is under memory pressure</strong>. This additional check happens when the cgroup memory controller is loaded, which it is by default on most distributions.</p>\n<pre><code>/* optimized version of sk_under_memory_pressure() for TCP sockets */\nstatic inline bool tcp_under_memory_pressure(const struct sock *sk)\n{\n    if (mem_cgroup_sockets_enabled &amp;&amp; sk-&gt;sk_memcg &amp;&amp;\n        mem_cgroup_under_socket_pressure(sk-&gt;sk_memcg))\n        return true;\n\n    return READ_ONCE(tcp_memory_pressure);\n}\n</code></pre>\n<p>And then it clicked. It was the cgroup memory controller all along. We launch nginx as a systemd service, and systemd places each service into its own cgroup and sets default controllers, like the already mentioned memory controller. The memory controller helps you set limits for the maximum memory consumption of a service, which we are actually not using. It's the well-known <strong>memory.high, memory.max</strong>, etc. But the memory controller has an undocumented feature, which activates when the controller is only loaded.</p>\n<p>The next diagram summarises what was happening. There's a flag which is set per cgroup, not per socket. So one reclaim shortfall throttles every connection nginx owns. When there's memory pressure inside a cgroup, reclaim falls short, and it sets a pressure flag on the <strong>memcg</strong>, which is a cgroup-wide option that shrinks every socket buffer, basically collapsing all the throughput. Once the pressure clears, it gets back to the normal state.</p>\n<p><img src=\"https://images.prismic.io/cdn77-blog/SqwYTW-C42md-9Mr_nginx-issue.png?auto=format,compress\" alt=\"\" /></p>\n<p>The pressure is raised through the <strong>vmpressure()</strong> function, which returns immediately for the root cgroup. So it only does anything for non-root cgroups. If the level rises above low, it writes <strong>jiffies + HZ</strong> into the memcg socket pressure, and every socket in that cgroup is throttled for at least one second.</p>\n<pre><code>void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,\n        unsigned long scanned, unsigned long reclaimed)\n{\n    ...\n    if (!memcg || mem_cgroup_is_root(memcg))\n        return;\n\n    ...\n\n    if (level &gt; VMPRESSURE_LOW) {\n        ...\n        WRITE_ONCE(memcg-&gt;socket_pressure, jiffies + HZ);\n    }\n    ...\n}\n</code></pre>\n<p>You might be asking, what actually raised that pressure?</p>\n<p>Memory reclaim reports how many pages it scanned and how many it managed to free per cgroup. Once roughly two megabytes' worth of pages have been scanned, the kernel turns that into a ratio. If it freed less than about 40% of what it walked, that counts as memory pressure, and every socket in the cgroup gets throttled for that one second. <strong>What's NOT in the formula is how much free memory the machine has</strong>. It's purely a measure of reclaim efficiency over a tiny two-megabyte window. So a cache-heavy server where reclaim keeps walking pages fails constantly, even though it has hundreds of gigabytes of free RAM. The two-megabyte window was a hardcoded constant in the kernel version we were debugging, with a TODO: in the kernel asking it to scale with the machine size.</p>\n<pre><code>/*\n* TODO: Make the window size depend on machine size, as we do for vmstat\n* thresholds. Currently we set it to 512 pages (2MB for 4KB pages).\n*/\nstatic const unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16;\n</code></pre>\n<h2>Patching the rabbit hole</h2>\n<p>So, our temporary fix. Remember that <strong>vmpressure()</strong> returns immediately for the root cgroup? We moved the nginx processes into the root cgroup. No more socket pressure from a reclaim shortfall on a few pages. Also no out-of-memory kills. So even the throttling wasn't justified.</p>\n<pre><code>void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,\n        unsigned long scanned, unsigned long reclaimed)\n{\n    ...\n    if (!memcg || mem_cgroup_is_root(memcg))\n        return;\n\n    ...\n\n    if (level &gt; VMPRESSURE_LOW) {\n        ...\n        WRITE_ONCE(memcg-&gt;socket_pressure, jiffies + HZ);\n    }\n    ...\n}\n</code></pre>\n<p>We reported this upstream, because at the time there was no way to even observe this pressure, no counter for it anywhere. With help from the Linux memory subsystem maintainers, we contributed a patch. <strong>Since Linux 6.19, there's a new counter in the memory.events file for sockets throttled by this mechanism.</strong></p>\n<pre><code>$ cat /sys/fs/cgroup/**/memory.events\n...\nsock_throttled 0\n</code></pre>\n<p><strong>P.S. The added counter is nice, but we'll share a proper fix. Stay tuned.</strong></p>\n"
}