DEV Community

Denis
Denis

Posted on Originally published at pixeloffice.eu

How We Tuned Linux Kernel Sockets & Rescaled an AI Gateway to 16GB in 45 Seconds for 50-Second LLM Streams

Most AI gateway benchmarks test trivial 10-token prompt-response roundtrips. In production, AI agent swarms and compiled backends generate thousands of tokens per session over long-lived streaming connections.

Yesterday evening at 19:09 CEST, a compiled Go backend (Go-http-client/1.1) connecting from high-performance computing infrastructure in Dallas, Texas sent a complex reasoning prompt to PixelRouter.

Here is what happened:

Timestamp (CEST) Client Target Model Tokens (Prompt → Comp) Stream Duration Cost Savings vs GPT-4o
18:59:48 Go-http-client/1.1 glm-5.3-flash 1,672 → 6,601 39.16s $0.0043 97%
19:09:52 Go-http-client/1.1 glm-5.3-flash 2,419 → 8,677 49.70s $0.0057 97%

The second request generated 8,677 output tokens over 49.70 continuous seconds via Server-Sent Events (SSE).

To an unhardened reverse proxy, this looks like a slowloris attack: an open socket draining memory buffers while slowly receiving chunked tokens. Under default Linux and Nginx configurations, connections drop around the 30-second mark due to socket timeout or proxy buffer overflow.

Here is how we tuned the system from the Linux kernel to Nginx, and executed a 45-second hardware rescale under active multi-region load without dropping a single packet.


1. Linux Kernel Socket Tuning (/etc/sysctl.d/99-pixelrouter.conf)

Default Linux kernel network parameters are optimized for bursty web traffic with sub-second request-response lifecycles. When handling thousands of concurrent, long-running streaming channels, default settings lead to SYN drops and socket exhaustion.

We deployed system-level tuning to /etc/sysctl.d/99-pixelrouter.conf:

# /etc/sysctl.d/99-pixelrouter.conf

# Maximum listen queue backlog for incoming connections
net.core.somaxconn = 32768

# Maximum queue of half-open TCP connections (SYN backlog)
net.ipv4.tcp_max_syn_backlog = 32768

# Ephemeral port range for outbound upstream connections
net.ipv4.ip_local_port_range = 10240 65535

# Fast recycling of sockets in TIME_WAIT state for keepalive reuse
net.ipv4.tcp_tw_reuse = 1

# Reduce socket lingering time from 60s to 15s to free file descriptors
net.ipv4.tcp_fin_timeout = 15

# Memory buffer tuning for high-bandwidth streaming sockets
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
Enter fullscreen mode Exit fullscreen mode

Apply immediately with:

sudo sysctl -p /etc/sysctl.d/99-pixelrouter.conf
Enter fullscreen mode Exit fullscreen mode

This expanded our socket listen backlog from 128 to 32,768 and prevented TCP connection queue drops during traffic spikes.


2. Nginx Zero-Buffering & Keepalive Architecture

In default reverse proxy mode, Nginx attempts to buffer upstream responses before delivering them to the client. For SSE token streams (text/event-stream), buffering destroys interactive latency and risks 504 Gateway Timeout.

We configured Nginx to disable buffering on LLM routes and established persistent connection pools between Nginx and our Node.js core:

# /etc/nginx/sites-available/pixel-office
upstream node_backend {
    server 127.0.0.1:3000;
    keepalive 64;               # Persistent connection pool to Node.js
    keepalive_requests 10000;   # Max requests per keepalive connection
    keepalive_timeout 60s;
}

server {
    listen 443 ssl backlog=8192;
    http2 on;

    location / {
        proxy_pass http://node_backend;
        proxy_http_version 1.1;

        # WebSocket & SSE connection upgrading
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # Disable proxy buffering for sub-millisecond SSE delivery
        proxy_buffering off;
        proxy_cache off;

        # Extended timeouts for deep reasoning models (up to 24h)
        proxy_read_timeout 86400;
        proxy_send_timeout 86400;
    }
}
Enter fullscreen mode Exit fullscreen mode

Combined with worker_connections 10240 and worker_rlimit_nofile 65535 in /etc/nginx/nginx.conf, each worker thread can maintain thousands of concurrent token streams with near-zero context switching overhead.


3. The 45-Second Hot Rescale (16GB RAM / 8 vCPU)

This morning at 08:11 CEST, automated latency telemetry sweeps hit our gateway from Ashburn, Virginia (43.166.255.122). Knowing that synthetic monitors execute heartbeat sweeps every 45 to 60 minutes, we scheduled an immediate hardware scaling window to upgrade from 2 vCPU / 4GB RAM to 8 vCPU AMD EPYC and 16GB RAM (Hetzner CPX42).

Executing an upgrade without IP mutation or data corruption requires strict sequential steps:

  1. Pre-Flight State Dump: Backed up persistent databases and synced PM2 process dumps via pm2 save into /root/.pm2/dump.pm2.
  2. Clean Memory Flush & Halt: Executed sync && shutdown -h now. All filesystem dirty buffers flushed cleanly before hardware detachment.
  3. CPU/RAM Rescale (Keep Disk Size): In Hetzner Cloud Console, selected CPX42 with Keep current disk size enabled. This avoids slow partition resizing, keeping the operation under 15 seconds and guaranteeing zero IP address changes.
  4. Cold Boot & Systemd Resurrect: Powered on the virtual machine. Kernel initialized 8 EPYC cores and 15,603 MB RAM.
# Verified Live Memory & Core Count (08:14:43 CEST)
$ free -m && nproc
Mem: total: 15603 MB | used: 614 MB | available: 14988 MB (96% free)
8 cores (AMD EPYC-Genoa Architecture)
Enter fullscreen mode Exit fullscreen mode

The entire cycle completed in under 45 seconds.


4. Multi-Region Latency Profile

Positioning the gateway on Hetzner's Falkenstein/Nuremberg infrastructure gives direct low-hop access to Frankfurt's DE-CIX hub. Our live empirical latency benchmarks across global test nodes verify the advantage:

  • Frankfurt am Main (DE-CIX): 3.2 ms (Direct European core peering)
  • US East (Ashburn, VA): 88.4 ms (Transatlantic fiber transit)
  • US West (Silicon Valley, CA): 141.6 ms (Cross-continental edge)
  • East Asia (Tokyo, JP): 224.1 ms (Transpacific route)

Testing Live Streaming

PixelRouter provides drop-in OpenAI (/v1/chat/completions) and Anthropic (/v1/messages) compatibility with automated model failover, native SSE streaming, and 85–97% wholesale token cost reduction.

curl -N -X POST https://api.pixeloffice.eu/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer px_test_free" \
  -d '{
    "model": "deepseek-v4-flash",
    "messages": [{"role": "user", "content": "Explain Nginx keepalive sockets"}],
    "stream": true
  }'
Enter fullscreen mode Exit fullscreen mode

Live playground and interactive benchmarks are available at https://pixeloffice.eu/router.html.

Top comments (0)