Back

CDN77 traces slow nginx downloads to cgroup memory pressure socket throttling
SiTech AI Team3 min read

CDN77 traces slow nginx downloads to cgroup memory pressure socket throttling

CDN77 engineers traced random slow HTTP cache-miss downloads to cgroup memory pressure that shrank nginx socket buffers. Moving nginx to the root cgroup stopped the throttling, and a new Linux 6.19 counter exposes it.

CDN77 has described how its engineers investigated random slowdowns on HTTP cache-miss downloads from edge servers. The issue affected only nginx processes, recovered on its own, and did not reproduce in a curl test on the same server. The team traced it to cgroup memory pressure that shrank TCP socket buffers and contributed a Linux 6.19 counter for the behavior.

Random slowdowns with no error codes

The customer complaint involved slow download speeds from an edge server, with no error codes. CDN77 could not reproduce it. The only visible sign was lower traffic on the network interface, and the slowdown happened randomly, recovered by itself, and did not affect all clients. In one case, it dropped just before 3 a.m., stayed flat for five hours, and returned to normal around 8 a.m.

CDN77 runs its CDN layers on heavily modified nginx. The team checked network paths, kernel TCP settings, nginx proxy buffering, disk I/O, CPU, memory, kernel logs, and kernel versions, but those checks led to dead ends. The issue later appeared in multiple regions, ruling out a specific ISP or network path. It appeared only on HTTP cache misses. During throttling, a curl command worked, but nginx downloaded slowly. Rebooting fixed it for a time.

Tracing reads and TCP fields

Because only nginx seemed affected, the team suspected userspace. It hooked read and epoll related syscall entry and exit points to capture arguments, return codes, and durations. The experiments showed no anomalies. Nginx was not delaying read syscalls and was reading everything available until EAGAIN.

The team used a kprobe in tcp_rcv_established with bpftrace and printed tcp_sock fields. They saw rcv_ssthresh was tiny, exactly four times the advertised maximum segment size. That value is only written by tcp_adjust_rcv_ssthresh, and the viable path was through tcp_grow_window when tcp_under_memory_pressure returned true, even though SNMP counters reported zero memory pressure.

The TCP check also looks at whether the socket's cgroup is under memory pressure when the cgroup memory controller is loaded. Systemd places each service in its own cgroup and sets default controllers, including the memory controller. CDN77 was not using limits such as memory.high or memory.max, but the controller still activated an undocumented feature when loaded.

A cgroup-wide pressure flag

The pressure flag is set per cgroup, not per socket, so one reclaim shortfall could throttle every connection nginx owned. A pressure flag on the memcg shrinks every socket buffer and collapses throughput. The pressure is raised through vmpressure, which returns immediately for the root cgroup. If the level rises above low, it writes jiffies plus HZ into the memcg socket pressure, and every socket in that cgroup is throttled for at least one second.

Memory reclaim reports scanned and freed pages 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. Free memory on the machine is not in the formula. The two-megabyte window was a hardcoded constant in the kernel version being debugged, set to 512 pages for 4KB pages.

As a temporary fix, CDN77 moved nginx processes into the root cgroup. The report notes there were no out-of-memory kills, so the throttling was not justified. CDN77 reported the issue upstream and, with help from Linux memory subsystem maintainers, contributed a patch. Since Linux 6.19, memory.events includes a sock_throttled counter for sockets throttled by this mechanism. CDN77 said it will share a proper fix later.

Sources: cdn77.com

SiTech

SiTech — AI-powered web development

We build fast, modern websites and bring AI into real business workflows. Have a project or a question? We'd love to help.