The Inarticulate

A chaotic pinboard of unsorted notes

TAGS ▼
YEARS ▼
PROJECTS ▼
TAGS
  • #automation
  • #cloudflare
  • #docker
  • #life
  • #linux
  • #macos
  • #networking
  • #opensource
  • #performance
  • #proxy
  • #python
  • #rant
  • #regex
  • #shell
  • #ssh
  • #ubuntu
  • #vim
  • #vpn
  • #vps
YEARS
2026 2025 2021 2020 2017 2016 2013 2009 2007 2006
PROJECTS
ydiff View diff side by side
Gcal Exporter Export google calendar events
ipfw.net IP Lookup & Firewall Checker
tenuki Play on OGS from terminal
@ymattw

Tuning a 1 GB Proxy VPS: 5× Throughput, Half the Latency, Half the RAM

Nov 24, 2026

I run my personal proxy stack (VLESS + TLS (Caddy) + Cloudflare CDN) on a cheap GCP e2-micro VM — 2 shared vCPUs (0.25 vCPU sustained baseline), 964 MiB RAM, zero swap, Debian 13, sitting in Taiwan (asia-east1-b). Connecting from Switzerland gives ~8 ms to Cloudflare’s Zurich edge and ~250 ms across the long transcontinental leg to Taiwan.

It worked fine, but I had never actually measured or tuned it. So I spun up two identical e2-micro VMs in the same zone — keeping tw1 untouched as the baseline and tuning tw2 one layer at a time with interleaved A/B benchmarks. An initial A/A run confirmed both unmodified servers behaved identically: ~31 Mbps single-stream download, ~17 Mbps upload, ~21–28 Mbps across 6 parallel downloads, ~1,800 ms median cold TTFB, and 478 MiB idle available RAM.

Everything below ended up backported into my VPS setup scripts .

Layer 1: Linux Kernel & TCP Stack (BBR + fq)

With ~250 ms RTT between Cloudflare’s edge and GCP Taiwan, default cubic congestion control, 208 KB socket caps, tcp_slow_start_after_idle = 1, and an 8,192-entry conntrack table left massive bandwidth on the table.

We switched to BBR (Bottleneck Bandwidth and Round-trip propagation time) — Google’s model-based TCP congestion control algorithm that estimates the path’s bandwidth and minimum RTT to pace sending instead of filling router queues until packets drop like cubic — paired with fq (Fair Queueing), the Linux packet scheduler that provides the per-flow packet pacing BBR relies on to avoid micro-bursts.

In /etc/sysctl.d/99-proxy-tuning.conf (with tcp_bbr persisted in /etc/modules-load.d/bbr.conf and tc qdisc replace dev <iface> root fq so pacing takes effect without a reboot):

  • Congestion control & pacing: net.core.default_qdisc = fq and net.ipv4.tcp_congestion_control = bbr.
  • RAM-scaled buffers & conntrack: 16 MB max TCP/UDP buffers and 65,536 conntrack entries on 1 GB VMs, scaling to 32 MB / 131k on 2 GB and 64 MB / 262k on 4 GB+.
  • Persistent proxy tuning: tcp_slow_start_after_idle = 0, tcp_fastopen = 3, tcp_mtu_probing = 1, tcp_notsent_lowat = 131072, somaxconn = 8192, netdev_max_backlog = 8192. (In Linux, net.ipv4.tcp_* applies to both IPv4 and IPv6 TCP sockets.)

Result vs. baseline: single-stream DL +23% (29.5 → 36.4 Mbps), upload +9%, 6-stream DL +93% (23.5 → 45.5 Mbps), and TCP retransmits dropped -75% (0.28% → 0.07%).

Layer 2: Eliminating the Local Proxy Hop (unix/@xray)

Previously, every packet between Caddy and Xray traversed:

Caddy → 127.0.0.1:60066 → docker-proxy (userland copy) → docker0 veth → iptables NAT → Xray

Switching Xray to an abstract Unix domain socket (listen: "@xray", reverse_proxy unix/@xray) removed docker-proxy, bridge NAT, loopback TCP, and ephemeral port churn in one move. Stripping X-Forwarded-For on the reverse proxy also stopped Xray from writing a warning to disk on every connection.

Result: 6-stream download jumped from 28.1 → 95.4 Mbps (+239%, peaking at 102 Mbps) while using ~50% fewer conntrack entries, ~30% fewer TCP segments, and slightly lower CPU than the baseline.

Layer 3: Migrating WebSocket to XHTTP (H2)

Even at 95 Mbps, cold HTTPS latency stayed stubbornly stuck at ~1,800 ms and connection churn capped at ~9 rps. Why? Because HTTP/1.1 WebSocket cannot be multiplexed over a single origin connection behind Cloudflare — every new proxied flow pays origin TCP+TLS setup plus a WebSocket Upgrade round-trip across 250 ms RTT.

  • Quick test — WebSocket Early Data (?ed=2048): piggybacking the VLESS header on the WS upgrade request (client-only change) immediately cut 365 ms off median latency (1,801 → 1,436 ms, -20%) and boosted churn by +26%.
  • Full upgrade — VLESS + XHTTP over HTTP/2 (h2c): with XHTTP (route /$route* and transport http { versions h2c 2 } in Caddy, alpn: ["h2"] on the client), both Client ↔ Cloudflare and Cloudflare ↔ Caddy multiplex streams over warm HTTP/2 connections.
Metric Baseline (WS) Tuned WS WS + ?ed=2048 XHTTP (H2)
Latency p50 1,793 ms 1,818 ms 1,436 ms 764 ms (-57%)
Latency p95 1,945 ms 1,929 ms 1,517 ms 1,033 ms (-47%)
Churn (16 conc) 9.1 rps 9.2 rps 11.2 rps 18.4 rps (2.0×)
6-stream DL 28.5 Mbps 86.8 Mbps 74.3 Mbps 155.0 Mbps (5.4×)
1-stream DL 32.4 Mbps 37.6 Mbps 39.3 Mbps 42.1 Mbps (peak 96.6)
TCP Retransmits 0.10% 0.00% 0.02% 0.00%

Cold TTFB dropped by over a full second, and Xray’s WebSocket deprecation warning disappeared. We also replaced the hardcoded URL path with the first segment of the generated UUID (${uuid%%-*}) and appended &tfo=1 to the Shadowrocket QR URI so TCP Fast Open enables automatically on scan.

Layer 4: Putting a 1 GB VM on a Memory Diet

Next, I simulated 3 heavy concurrent devices (continuous downloads, 4K video chunks, uploads, and 12 browser request loops plus an active tmux session) with 1-second server telemetry — then pushed it to 60 concurrent streams to see where a 964 MiB e2-micro actually breaks.

The biggest surprise wasn’t Xray or Caddy — it was the OS itself. Nearly half the VM (~470 MiB) was already gone before proxy traffic even started:

  • 6 GCP guest/telemetry agents: ~145 MiB RSS (google-osconfig-agent, google-guest-compat-manager, google-guest-agent-manager plus its workload identity and telemetry plugins).
  • Docker runtime: ~140–160 MiB RSS (dockerd + containerd + shim) just to wrap a single static Go binary running with --network=host.
  • Redundant daemons: haveged (obsolete on Linux 6.12), networkd-dispatcher, and rsyslog (redundant with systemd-journald).

Here is what actually moved the needle:

  1. Disabled unused background services (keeping unattended-upgrades for kernel security patches, and stopping google-guest-agent-manager after a 15-second background delay so GCE first-boot startup scripts still complete cleanly). Verified across a full reboot that SSH and systemd-networkd DHCP come straight back up.
  2. Replaced Docker with a hardened native systemd service: Docker and systemd use the same Linux kernel isolation primitives. Running Xray directly under a dedicated xray system user with NoExecPaths=/, ExecPaths=/opt/xray/xray, MemoryDenyWriteExecute=yes, CapabilityBoundingSet= (zero caps), PrivateUsers=yes, ProtectSystem=strict, ProtectProc=invisible, SystemCallFilter=@system-service, and read-only config.json scored 1.1 OK :-) on systemd-analyze security xray.service — matching or beating docker --network=host while reclaiming ~150 MiB and removing a root daemon. (One subtle catch: ProcSubset=pid hid /proc/sys/net/core/somaxconn from Go, silently dropping the @xray listen backlog from 8,192 to 128 — dropping ProcSubset=pid while keeping ProtectProc=invisible restored 8,192 with the same 1.1 OK score.)
  3. Trimmed Caddy: Disabled Caddy’s unused HTTP/3 UDP :443 listener (servers { protocols h1 h2 } — Cloudflare never uses H3 to origins), set global log { level ERROR }, dropped Debian’s default :80 file server, and shrunk the h2c upstream pool (32 KB buffers, 30s keepalive, 16 idle conns).
  4. Added 512 MB zstd zram swap (systemd-zram-generator, vm.page-cluster = 0, vm.swappiness = 100, plus journald capped at 32 MB). Under pressure, cold pages compressed at 11:1–15:1 (28 MiB into 2.5 MiB of RAM) with zero disk I/O.
Memory Metric (e2-micro, 964 MiB) Before (Docker + GCP agents + 0 swap) After (Native systemd + trimmed + zram)
Idle MemAvailable 478 MiB 704 MiB (+226 MiB)
OS + Kernel peak (3 heavy devices) 469–511 MiB 249 MiB (-50%)
Min MemAvailable (3 heavy devices) 273 MiB 509 MiB (+86%)
Min MemAvailable (60 streams) 48.5 MiB (near OOM) 213.5 MiB (4.4×)
Memory PSI (60 streams) 6.8–10.1 0.36 (~25× lower)

We also measured GOMEMLIMIT (finding a consistent 60% Caddy / 40% Xray usage split across every load level), but with ~670–700 MiB free and 512 MB of zram on a 1 GB VM, OOM is no longer a realistic risk — so we kept GOMEMLIMIT on a side branch rather than adding complexity prematurely.

Final Numbers (and One Bonus Lesson)

From start to finish on the exact same ~$7/month e2-micro:

  • Cold latency (p50): 1,793 ms → 764 ms (-57%)
  • 6-stream burst throughput: 28.5 Mbps → 155–167 Mbps (5.4×; peaked at 248 Mbps DL + 38 Mbps UL in 60-second multi-device stress while CPU burst credits lasted, before settling to ~35–60 Mbps at the 0.25 vCPU sustained baseline)
  • TCP retransmits: 0.10–0.28% → 0.00% in standard benchmarks (0.04–0.21% under heavy multi-device saturation)
  • Available RAM (idle): 478 MiB → ~670–704 MiB (Caddy 37 MiB + Xray 34 MiB fresh)

And one unexpected takeaway: pushing 60 concurrent HTTPS streams (~295 Mbps outbound burst) through an e2-micro to public speed-test endpoints was effective enough to trigger a Google Cloud Trust & Safety DoS alert twenty minutes later. Measure your changes, keep your load tests realistic, and don’t accidentally DDoS-alert yourself.

#linux #networking #performance #vps