← Back to Engineering Blog

Deleting Our Custom Scale-to-Zero Gateway: 16.3s to 2.4s Cold Wake and -80% RAM with KEDA

Why our homemade Node.js and Python idle gateway grew past 4,500 lines and 855 MiB of resident RAM—and how replacing it with KEDA + KEDA HTTP Add-on cut cold wake latency ~6.9x.

kuberneteskedascale-to-zeroarchitectureperformance

Every homelab-turned-PaaS builder eventually hits the same temptation: "How hard can scale-to-zero HTTP routing really be? I will just write a lightweight proxy that buffers requests for 10 seconds and bumps Deployment replicas from 0 to 1."

We fell straight into that trap. Over a few months of rapid iteration, our "simple" idle proxy ballooned into 3,623 lines of Python across seven modules (http_idle.py alone reached 1,937 lines) plus 971 lines of custom Node.js gateway code (gateway.mjs / fleet.mjs), 6 always-on Gateway Pods, 4 credential-renewal CronJobs, and 29 per-namespace NetworkPolicy objects.

By late November, ripping all of it out in favor of upstream KEDA (2.21.1) + KEDA HTTP Add-on (0.16.0) became one of the highest-leverage engineering wins on our Kyoto cluster.

What Broke With Our Custom Idle Gateway

In isolation, a custom proxy feels easy to control. In production across dozens of sleeping apps, the glue cost compounded fast:

  • Painful cold wake latency (16.35s): Between control-plane reconciliation polling, multi-hop proxying (6–7 hops even for awake apps before we bolted on a metric-driven bypass), and hold/re-engage grace windows, waking an app from 0 Pods took 16.35s in end-to-end verification.
  • High idle footprint (855–857 MiB RAM): Keeping 6 custom Node.js gateway Pods warm (to cover our shared idle pool and edge routing) burned 855–857 MiB of RAM and 20–21m CPU just waiting for traffic.
  • Self-inflicted DB locks & CronJob churn: The idle reconciler hammered the Control Plane database with 5-second heartbeat loops. During our PostgreSQL migration, reverse lock ordering between state persistence and heartbeats even triggered 40P01 DeadlockDetected errors.
  • Complex state machines to avoid deletion deadlocks: Because our gateway held synthetic Ingress state and per-namespace network policies, deleting an active app required carefully timed grace windows (AWAKE_BYPASS_GRACE_SECONDS=15, REENGAGE_GRACE_SECONDS=10) to avoid 503s or STOP_APP_FIRST deadlocks.

Why We Picked KEDA HTTP Add-on (and Rejected Sablier)

Before migrating, we evaluated Traefik Sablier (v1.21.1) as a potentially even simpler alternative. We ruled it out for three concrete reasons:

  1. Its Kubernetes blocking strategy had a known synchronization race causing infinite redirect loops under load (sablier#1273).
  2. Its dynamic strategy returns an HTML waiting page—which breaks curl, mobile APIs, and webhook callers expecting JSON.
  3. It required experimental Yaegi plugin injection directly inside our production Traefik Pods.

KEDA + KEDA HTTP Add-on, on the other hand, ran cleanly in its own isolated keda namespace, kept production Traefik completely untouched, and let the Go-based KEDA HTTP Interceptor hold incoming connections (both browsers and JSON/API callers) until the target Pod passed readiness probes—returning a direct, transparent HTTP/2 200 with zero redirects and zero HTML waiting screens.

Before vs. After: Real Kyoto Cluster Numbers

Moving all apps from our custom idle stack to InterceptorRoute + HTTPScaledObject + ScaledObject delivered immediate, measurable wins across both single-app tests and full production rollout:

MetricCustom http_idle GatewayKEDA 2.21.1 + HTTP Add-on 0.16.0Delta
Cold wake latency (0 -> 1 Pod, gVisor)16.35s2.383s (PoC) / 3.64–3.90s (prod E2E)~4.2x–6.9x faster
5-way concurrent cold burst (0 -> 1)—5/5 HTTP/2 200 in 3.42sZero 503s
Steady-state proxy latency overhead6–7 hops (pre-bypass)+1.7ms p50 (89.1ms -> 90.8ms)Near wire speed
Idle infrastructure Pods & RAM (PoC parity)6 Pods / 855–857 MiB8 Pods / 153–174 MiB (246 MiB post-rollout)~80–82% less RAM (PoC)
Custom code & K8s policy sprawl4,594 lines + 29 NPsNet -11,500 lines deleted across final waveMassive diet

Stateful apps backed by persistent volumes also resumed cleanly (12.661s cold wake including ReadWriteOnce volume reattachment with data intact), and both SSE chunked streaming and WebSockets (wss:// 101 Switching Protocols) worked transparently through the KEDA Interceptor.

Two Subtle Details That Saved Us in Production

Adopting upstream KEDA did not mean zero glue—two small patterns kept production rock-solid:

  1. Stopping an app explicitly without breaking scale-to-zero: When a user intentionally stops an app (pangea stop), random internet scanners hitting its URL must not silently wake it back up. Setting KEDA's standard autoscaling.keda.sh/paused-replicas: "0" annotation on the ScaledObject alongside a retired Ingress state kept explicitly stopped apps pinned at 0 Pods (5/5 HTTP 404), even under direct edge requests.
  2. Keeping keda tight with Cilium + CEL: We kept KEDA's namespace locked down with 6 minimal CiliumNetworkPolicy rules (remembering to allow fromEntities: [host] on port 8081 so kubelet probes to keda-http-operator stay green), while letting Kubernetes ValidatingAdmissionPolicy validate route definitions declaratively.

Takeaway

The biggest lesson from retiring our custom idle gateway aligns with a rule we now enforce across the platform: solve 80%+ of infrastructure mechanics with boring, upstream CNCF tooling, and keep your custom code focused purely on the thin product boundary. Deleting >11,000 lines of custom code made cold wakes ~6.9x faster—and made my evenings dramatically quieter.