skew-router

command module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 40 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

skew-router

Skew protection for a self-hosted Next.js app on Kubernetes. A browser tab keeps talking to the build that served it, even after you deploy a new one, so its JavaScript chunks, RSC payloads and prefetches never land on a server that doesn't have them.

ingress ──▶ skew-router :8080 ──▶ Service web-<id> :3000   (one Deployment + Service per build)

Vercel does this for its deployments. skew-router does it inside your cluster: a small Go proxy (client-go informers, no other dependencies) that sits between the ingress and one Service per web build.

The story behind it: Two Builds, One Address.

How it routes

Next.js already tells you which build a request belongs to. With deploymentId set at build time, it adds ?dpl=<id> to every /_next/static URL and sends x-deployment-id: <id> on client navigations and prefetches. skew-router reads the header first, then ?dpl=, and picks a version:

The request carries It goes to Metric outcome
no id (a document navigation, a crawler) the active version unpinned
the active version's id the active version latest
the id of an older version that is still routable that version pinned
an unknown, retired or revoked id the active version (Next then reloads the page) fallback
anything, while no version is ready 503 no web version available unavailable

The active version is an explicit pointer in a ConfigMap (web-routing), not "the newest pod". A deploy only stamps the new version as promoted; the router moves the pointer once that version has ready endpoints, and never to one without.

Two modes

  • -mode=proxy (default): requests go ingress → skew-router → version. Simplest to set up, and the router counts every routing outcome in its own metrics.
  • -mode=traefik: the router carries no traffic. It serves Traefik's HTTP provider on the admin port (GET /traefik): one router per routable version matching its id in the x-deployment-id header or ?dpl=, and a lower-priority default router to the serving version. Traefik then sends each request straight to its version, with no extra hop.
# Traefik static configuration
providers:
  http:
    endpoint: http://skew-router.web.svc.cluster.local:9090/traefik
    pollInterval: 2s
skew-router -mode=traefik -traefik-rule='Host(`www.example.com`)' \
  -traefik-entrypoints=websecure -traefik-certresolver=le

Pointer moves, retention and revocation work the same; Traefik picks them up on its next poll. Every replica serves the same configuration, and if every replica is down Traefik keeps the last configuration it fetched, so routing goes on (frozen) until one is back. That's tested, not assumed: with the endpoint gone for 8 s, Traefik kept answering 200. The trade: outcome metrics move to Traefik's per-router metrics (router names carry the version id), and a pointer move takes up to one poll interval to apply.

Retention

  • The previous version stays routable for -version-ttl (default 1 h) after it stopped being active, so open tabs keep working.
  • Then it is scaled to 0, not deleted: the objects stay, with the replica count recorded, so an instant rollback is a scale-up plus a pointer move.
  • Beyond -max-versions (default 2, the active one included), versions are deleted.
  • Security fixes skip the window: a version annotated revokes-older=true unroutes every older version the moment it is active and ready, on every replica at once, and they are deleted after the drain. A revoked version is never served again, not even as the fallback: a 503 beats serving a build with a known hole.

The timers are annotations on the version Services, so they survive router restarts and leader changes.

High availability

  • Run several replicas: every replica routes from its informer cache, and the planning step is a pure function, so all of them compute the same routing table.
  • Only the Lease holder (leader election) writes: the pointer, the timers, scale-downs and deletes.
  • Writes are pinned with UID and resourceVersion preconditions plus a version-label check, because RBAC can't scope a write to objects with a label.
  • Refused connections to a just-created Service (kube-proxy not programmed yet) are retried 5 times, 200 ms apart, instead of becoming a 502.
  • If every router replica is down, a Traefik failover service sends traffic straight to all web pods (examples/traefik-failover.yml). Pinning is lost meanwhile, the site stays up.

Set it up

  1. Bake the id into the build (examples/next.config.ts): deploymentId: process.env.NEXT_DEPLOYMENT_ID, with NEXT_DEPLOYMENT_ID set to the commit SHA in CI. It must be a valid Kubernetes label value.
  2. Install the router (examples/skew-router.yaml): ServiceAccount, a namespace-scoped Role, 2 replicas, a PDB, and the web-fallback Service. Build the image with docker build -t <your-registry>/skew-router:<tag> .
  3. Deploy each build as its own version with examples/web-version.sh and the examples/web-version.yaml template:
NS=web ./examples/web-version.sh rollout "$GIT_SHA" registry.example.com/web:"$GIT_SHA"
NS=web ./examples/web-version.sh promote previous        # instant rollback
NS=web WEB_REVOKE_OLDER=1 ./examples/web-version.sh rollout "$SHA" "$IMAGE"   # a security fix
NS=web ./examples/web-version.sh list

rollout waits until the router has made the new version active and deletes it if that never happens, so a broken build leaves the previous one serving.

The contract this creates

Two builds now serve users at the same time for up to an hour. Anything they share (your API, the database) must stay compatible with both for at least -version-ttl. Only the web build is versioned; a backend fix reaches every version at once.

If you use Server Actions, set a stable NEXT_SERVER_ACTIONS_ENCRYPTION_KEY, or an action posted from an old tab fails against a new build.

Flags

Flag Default Meaning
-listen :8080 proxy address
-admin-listen :9090 /healthz, /readyz, /metrics, /versions (keep it private)
-namespace $POD_NAMESPACE or default namespace of the web versions
-selector app=web label selector of the version Services
-version-label skew-router.dev/version label holding the deployment id
-annotation-prefix skew-router.dev prefix of the promotion and retention annotations
-pointer-configmap web-routing ConfigMap holding the active version
-port-name http Service port to proxy to
-version-ttl 1h how long a version stays routable after it stops being active
-max-versions 2 versions kept, the active one included
-drain 10s wait between unrouting a version and scaling it down or deleting it
-reconcile-interval 5s periodic reconcile, on top of watch events
-shutdown-delay 3s keep serving (unready) this long after SIGTERM
-leader-elect true only the Lease holder writes
-lease-name skew-router Lease used for leader election
-dry-run false log deletions instead of doing them

web-version.sh reads the same prefix from PREFIX (default skew-router.dev); keep the two in sync.

Observability

  • GET /versions on the admin port: the routing table every replica is using.
  • GET /metrics: skew_router_requests_total{outcome}, skew_router_versions_routable, skew_router_versions_retired_total, skew_router_versions_revoked_total.
  • JSON logs on stdout.

Overhead

Behind a real Traefik on a 4 vCPU AMD EPYC box (Traefik, router, backend and load generator sharing the four cores; 32 concurrent clients; TestMeasureTraefik):

Path req/s p50 p99
-mode=traefik: Traefik → version 20,975 1.2 ms 5.6 ms
-mode=proxy: Traefik → router → version 10,668 2.6 ms 8.5 ms

Dropping the hop doubles throughput; in traefik mode the ceiling is Traefik's own.

The proxy on its own

SKEW_BENCH=1 go test -run TestMeasureOverhead -v measures it: the same request straight to a version and through the router, on loopback, 10,000 sequential requests per case.

Machine Direct, p50 Router, p50 Router, p99
Apple M1 Pro 42 µs 100 to 110 µs 297 to 391 µs
4 vCPU AMD EPYC (Hetzner, other workloads running) 155 µs 268 to 371 µs 714 to 881 µs

Pick (the routing decision) is 6.6 ns and reading the id about 0.5 µs (go test -bench .): the cost is the extra proxy hop. With 32 concurrent clients on the 4 vCPU box the router served about 19,500 req/s against 66,500 direct, with client, router and backend sharing four cores.

Limits

  • It costs a second set of pods for up to -version-ttl after every deploy.
  • Revocation loses client state: tabs on a revoked build reload into the fix.
  • An instant rollback to a version scaled to 0 may need to pull its image again if your nodes pruned it.
  • Kubernetes only: it discovers versions through the API server. On plain Docker you'd need a different discovery layer.

License

MIT

Documentation

Overview

skew-router sits between the ingress (Traefik) and the web versions and implements Vercel-style skew protection for a self-hosted Next.js app.

Every web build carries a deployment id (next.config `deploymentId`). Each build runs as its own Deployment + Service `web-<id>`, labelled `skew-router.dev/version=<id>`. A browser tab keeps sending its build's id (`x-deployment-id` header on client navigations, `?dpl=` on static assets), and this router sends those requests to the version that served the tab, as long as it is alive. Requests without an id (document navigations) and ids of versions that are gone go to the latest version. Older versions are retired after a TTL and beyond a maximum count — or at once when the active version is a security fix (annotation `skew-router.dev/revokes-older`).

Runs as several replicas on any node: every replica routes from cluster state alone; one of them (Lease leader election) keeps the retention timers — stored as annotations on the version Services, so restarts and failovers keep them — and deletes expired versions.

See README.md for the operational side and examples/ for the manifests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL