skew-router

command module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 39 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 and Traefik. 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.

browser ──▶ Traefik ──────────────────────▶ Service web-<id>   (one Deployment + Service per build)
               ▲
               │ routes, every few seconds (Traefik HTTP provider)
               │
         skew-router   pointer, retention, revocation

Vercel does this for its deployments. skew-router does it inside your cluster without sitting in the request path: it watches one Service per web build and tells Traefik where each request goes. Traefik then sends every request straight to its build, so skew protection costs nothing per request.

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 turns that into Traefik routes:

The request carries It goes to
no id (a document navigation, a crawler) the active version
the id of a version that is still routable that version
an unknown, retired or revoked id the active version (Next then reloads the page)
anything, while no version is ready 503

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.

Route templates

A real site rarely has one route. It has a catch-all with its rate limit and WAF, a static-assets route with a different limit, a route for internal traffic with no limits. You describe those routes once, in a JSON file (-routes), exactly as you'd write them in Traefik:

{
  "routes": [
    { "name": "trusted", "priority": 1000,
      "rule": "Host(`www.example.com`) && ClientIP(`10.42.0.0/16`)",
      "entryPoints": ["websecure"], "middlewares": ["web-strip-token@kubernetescrd"], "tls": {} },
    { "name": "static", "priority": 500,
      "rule": "Host(`www.example.com`) && PathPrefix(`/_next/static/`)",
      "entryPoints": ["websecure"], "middlewares": ["web-edge-static@kubernetescrd"], "tls": {} },
    { "name": "web", "priority": 100,
      "rule": "Host(`www.example.com`)",
      "entryPoints": ["websecure"], "middlewares": ["web-edge-web@kubernetescrd"], "tls": {} }
  ]
}

For each template skew-router generates:

  • a default router: the template's rule, priority and middlewares, to the active version;
  • a pinned router per other routable version: the template's rule AND the version's id (header or ?dpl=), at priority + 1, with the same middlewares, straight to that version. The serving version gets none, since the default router already sends its requests there.

So a pinned request keeps exactly the protection its template gives it: a pinned /_next/static chunk still gets the static route's rate limit.

Rate limits count per router. Traefik builds a middleware instance per router, and an in-memory rateLimit keeps its buckets per instance. Leaving out the serving version's pinned router keeps a browser on the current build on one bucket per template, as without skew protection. A tab on an older build counts apart while that build is routable, so a client mixing ids can get up to 2× a limit for the hour after a deploy (with -max-versions=2). For an exact limit, give the middleware Traefik's redis backend: its key is the middleware's name and the source, without the router, so every router shares one bucket. Traefik answers 500 while it can't reach that Redis. Keep template priorities at least 2 apart, so a pinned router never ties or overtakes the next template (the router refuses a file that doesn't).

It also serves a service skew-active (referenced from other providers as skew-active@http) pointing at the active version, for routes that need a backend but no pinning, like an apex redirect.

A single route can be given with flags instead of a file: -traefik-rule, -traefik-entrypoints, -traefik-middlewares, -traefik-certresolver or -traefik-tls.

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, and they are deleted after the drain. A revoked version is never routed again: 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: each builds the routing table from its informer cache with the same pure function, so all of them serve the same routes.
  • 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.
  • If every replica is down, Traefik keeps the last routes it fetched and goes on routing (frozen) until one is back. Tested: with the endpoint gone for 8 s, Traefik kept answering 200.

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, the routes ConfigMap. Build the image with docker build -t <your-registry>/skew-router:<tag> .

  3. Point Traefik at it (static configuration):

    providers:
      http:
        endpoint: http://skew-router.web.svc.cluster.local:9090/traefik
        pollInterval: 2s
    

    On k3s that's additionalArguments in the Traefik HelmChartConfig. Remove your own routes for the site: the generated ones replace them.

  4. 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
-routes none JSON file with the route templates
-traefik-rule (and -traefik-entrypoints, -traefik-middlewares, -traefik-certresolver, -traefik-tls) none a single route, instead of -routes
-admin-listen :9090 /traefik, /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 the routes point at
-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 answering (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.

Overhead

None per request: requests never pass through the router. Measured behind a real Traefik on a 4 vCPU AMD EPYC box (Traefik, backend and load generator sharing the cores; 32 concurrent clients; TestMeasureTraefik, which also checks every generated route against that Traefik):

Path req/s p50 p99
Traefik → version, no skew protection 18,759 1.3 ms 6.5 ms
Traefik → version, skew routes (pinned) 18,322 1.4 ms 6.1 ms

Up to v0.2 skew-router could also proxy the traffic itself; behind Traefik that halved throughput (10,668 vs 20,975 req/s in the same setup), so v0.3 removed it.

Observability

  • GET /versions on the admin port: the routing table every replica is using.
  • GET /traefik: the routes Traefik is being given.
  • GET /metrics: skew_router_versions_routable, skew_router_versions_retired_total, skew_router_versions_revoked_total, skew_router_active_info. Per-request numbers are Traefik's: its router metrics carry the version id in the router name.
  • JSON logs on stdout.

Limits

  • Traefik only: the routes are Traefik's dynamic configuration.
  • It costs a second set of pods for up to -version-ttl after every deploy.
  • A pointer move takes up to one poll interval to reach Traefik.
  • 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.

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