gateway icon indicating copy to clipboard operation
gateway copied to clipboard

Reordering HTTPRoute rules causes brief 503 errors to clients

Open jungeonkim opened this issue 6 months ago • 6 comments

Description: I believe this is similar to (or the same as) #6287, but I'm opening a new issue since we hit this problem in an actual production use case.

For readability, we had been defining the last rule in HTTPRoute.spec.rules as a catch-all default rule (path: { type: PathPrefix, value: / }), and placing newly added rules above it in whatever order was easiest to read.

However, with Envoy, whenever the index of a rule changes, clients receive 503 responses for several seconds.

Repro steps:

Switching back and forth between the two manifests below triggers 503 errors every time. The only meaningful difference is that the / PathPrefix rule moves from index 0 (Example A) to index 1 (Example B).

Example A:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: site
spec:
  parentRefs:
    - name: eg
  hostnames: ["example.com"]
  rules:
    - matches:
        - { path: { type: PathPrefix, value: / } }
      backendRefs:
        - { name: frontend, port: 80 }

Example B:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: site
spec:
  parentRefs:
    - name: eg
  hostnames: ["example.com"]
  rules:
    - matches:
        - { path: { type: PathPrefix, value: /api/ } }
      backendRefs:
        - { name: backend, port: 80 }
    - matches:
        - { path: { type: PathPrefix, value: / } }
      backendRefs:
        - { name: frontend, port: 80 }

Applying A, then B, then A (etc.) produces a short window of 503 responses on each transition.

Environment: Envoy Gateway version: 1.8.0

jungeonkim avatar May 19 '26 04:05 jungeonkim

This problem will be alleviated after https://github.com/envoyproxy/gateway/issues/5307 landed, which is targeted to v1.9

zirain avatar May 19 '26 04:05 zirain

@jungeonkim can you confirm that keeping the the catch-all as the first rule doesnt cause the same 503 ?

this is likely happening because the naming for xds clusters is tied to rule index numbers

httproute/<ns>/<route>/rule/<ruleIdx>

so keep the ruleIdx constant can avoid this

arkodg avatar Jun 08 '26 06:06 arkodg

@arkodg Yes, we are already doing the workaround like that. However, as mentioned in above, we hope to place it below for readability.

jungeonkim avatar Jun 08 '26 06:06 jungeonkim

This issue has been automatically marked as stale because it has not had activity in the last 30 days.

github-actions[bot] avatar Jul 08 '26 08:07 github-actions[bot]


TL;DR: we can reproduce this on a ~2,000 route mergeGateways fleet, and @arkodg's rule-index explanation matches everything we see. Two things I don't think have come up yet. The window is a lot wider than "brief", one of our busier domains lost 4.68 seconds of traffic outright. And MergeBackends looks CEL-blocked against mergeGateways, so the fix tracked in #5307 doesn't actually land for the setups most likely to hit this. Question about that at the end.


We've been chasing this on v1.8.0 for a while and can now trigger it on demand. Dumping what we have in case it's useful.

Environment

  • Envoy Gateway v1.8.0 (gateway-helm), mergeGateways: true
  • ~2,000 Gateways / ~2,000 HTTPRoutes in one namespace, one merged proxy fleet
  • 12 proxy replicas, concurrency: 2, ~8,000 active clusters
  • Several hundred thousand req/min through the merged listeners at peak
  • No service mesh, plain Kubernetes Services

Confirming the mechanism

Cluster names are derived from the rule index, as @arkodg suggested in https://github.com/envoyproxy/gateway/issues/9029#issuecomment-4645926694:

httproute/<ns>/<route>/rule/<N>/backend/<M>

A couple of things back that up beyond just reading the resource names.

Removing one rule from exactly two routes dropped cluster_manager.active_clusters by exactly two, even though the backing Service was unchanged and still referenced. During a full fleet drain the same counter fell by about 90, with cluster_removed bursting up to 128 per scrape interval.

The arithmetic works out too: route count times backend-bearing rules per route lands within about 2% of the measured active_clusters total. That only adds up if clusters are per rule rather than per backend.

One thing that surprised us is that no backendRef change is needed at all. We reproduced it by inserting, then removing, a single unroutable PathPrefix rule. Same backend Service, namespace and port throughout, only the rule indices moved. Both directions gave us 500 / NC / cluster_not_found, duration: 0, empty upstream_cluster.

The window is seconds, not milliseconds

This is the bit I'd most want to flag, and we had it wrong ourselves for most of the investigation.

Working backwards from our early reproductions (1 to 7 failures at ~2,000 req/s) we figured the window was under 100ms. Turns out that was just an artefact of testing on routes that barely get any traffic. We saw the first failure or two, nothing after, and assumed it had closed.

A full fleet drain settled it. One of our busier customer domains produced 114 failures across a contiguous 4.68 second span, across 104 distinct paths, measured off Envoy's start_time rather than log-flush time:

first failure start   11:05:38.249
last  failure start   11:05:42.931
                      -----------
                      4.68 s

Effectively all traffic to that route's catch-all rule failed for the duration. Other domains in the same drain lost 1 or 2 requests each, not because their windows were shorter, but because they had less traffic falling into a window of the same width.

So the window looks like a fixed 2 to 5 seconds, matching the gap we see in controller logs between reconciled gateways successfully and infrastructure received an update. What varies is how much traffic falls into it. For a high-traffic domain that is not a transient blip, it's a multi-second hard failure.

xDS ordering itself looks correct

Controller at info, correlating the xds logger against data-plane access logs:

01:57:05.519  provider     reconciling gateways
01:57:05.738  provider     reconciled gateways successfully      (219ms)
01:57:05.738  gateway-api  received an update
01:57:07.224  infra        received an update                    (+1.49s)
01:57:07.478  xds          received an update
01:57:09.063  xds          EDS watches open   (24)
01:57:09.130  *            FAILING REQUEST STARTS
01:57:09.139  xds          CDS watches open   (12)  version "4"
01:57:09.308  xds          LDS watches open   (12)  version "4"
01:57:09.375  xds          RDS watches open   (12)
01:57:09.468  xds          convergence complete

Delivery order is EDS, CDS, LDS, RDS, with 12 watches per type (one per proxy replica), which is what you'd want. Controller reconcile is fast: 172 to 219ms to sweep ~2,000 Gateways, ~2,000 HTTPRoutes and ~2,000 Secrets. Controller CPU isn't a factor either; the host node sat 80-90% idle, there's no CPU limit set, and it bursts freely to ~4 cores.

So this doesn't look like an ordering or throughput problem. The exposure is the interval where the route table and the cluster table disagree about a name.

If anyone else goes digging: use Envoy's start_time, not the access-log entry timestamp. We saw up to 9.4s of flush lag and it had us convinced for a while that the failures were happening well after convergence finished, which sent us down a wrong path for a bit.

Damage scales with rules that move, not routes patched

From the same drain, 142 failures across 15 domains:

host failures distinct paths
(busy domain) 114 104
next 4 domains 3 to 5 each
remaining 10 1 to 2 each

One domain accounted for 80% of the total.

More usefully, that drain renamed only one cluster per route. The change removed a rule at index 1 and moved the catch-all into the vacated slot, so the two more-specific path rules below it kept their indices and were never renamed. All 142 failures landed on the catch-all rule, and none on the two rules that held position, despite those carrying substantial traffic.

Had the same rule been removed without that reordering, three rules per route would have shifted instead of one. We haven't measured that variant, but the exposure surface is 3x and it would put previously unaffected paths in scope.

That also means the "keep the catch-all's index stable" workaround pays off immediately rather than only on later changes. It reduced the cost of the very change that adopted it.

Why pacing helps but doesn't fix it

We pace bulk patches with a sleep between each. Same batch size, very different outcomes:

run routes pacing failures
earlier bulk migration ~500 none ~2,500 and ~4,000
2026-08-06 drain ~2,000 paced 142

Large effect, but it works by preventing overlapping windows rather than by shortening one. That's consistent with earlier testing where increasing the delay (1s, 3s, 5s) never converged toward zero and wasn't even monotonic.

The question

MergeBackends (#9477) looks like the structural fix. One cluster per backend rather than per rule/backend means reordering rules stops renaming anything. But it's mutually exclusive with mergeGateways, CEL-enforced:

rule="!(has(self.mergeGateways) && self.mergeGateways && has(self.mergeBackends))"

with the PR noting the combination was scoped out as "complexity around a mode that may eventually be deprecated in favor of ListenerSet".

That leaves mergeGateways users without a path to the fix, and those are plausibly the deployments with the largest route counts, so both the most exposure and the most to gain. In our case the payoff would be large: every route targets one of two backends, so a per-backend registry would collapse ~8,000 clusters to about 3.

From reading the implementation, the registry key already carries a gateway dimension:

out.GatewayIRKey = t.getIRKey(gatewayCtx.Gateway)

and under merge mode that resolves to a single value:

func (t *Translator) IRKey(gatewayNN types.NamespacedName) string {
    if t.MergeGateways { return string(t.GatewayClassName) }
    return irStringKey(gatewayNN.Namespace, gatewayNN.Name)
}

so merged Gateways would already share one registry scope. Our guess is the real obstacle is gateway-level policy attribution, since BackendTrafficPolicy and ClientTrafficPolicy resolve per-Gateway and merged Gateways could carry conflicting policies that a shared cluster can't represent. Happy to be told we've misread that.

If it is that, though, it doesn't arise in a setup like ours. Our merged Gateways are all machine-generated from one template and identical apart from hostname and cert ref. Both traffic policies are single objects applied by label selector across the whole fleet (one ClientTrafficPolicy selecting Gateway by label, one BackendTrafficPolicy selecting HTTPRoute by label) rather than one policy per Gateway, so every merged Gateway resolves to the same gateway-level policy by construction. There's nothing for a shared cluster to reconcile.

Feels like that would fit the eligibility checks already there (shouldMergeBackend, BuildBTPIndexes, BuildCTPIndexes): disqualify merging when the merged set is policy-heterogeneous, allow it when it isn't.

So:

  1. Would you consider relaxing the mergeGateways / MergeBackends mutual exclusion for the case where the merged Gateway set is policy-uniform? The complexity the PR describes seems specific to heterogeneous merged sets, which large template-generated fleets like ours aren't.
  2. Or is ListenerSet the intended path for large merged deployments? If so we'd rather plan that migration than keep working around this.

Separately, on MergeBackends as it stands: the known limitation that clusterStatName / alt_stat_name is dropped for merged clusters (#9570) is itself a blocker for us, since we rely on it to keep Envoy metric cardinality manageable at this fleet size.

On change frequency

We touch this template only a few times a year. But the routes are machine-generated from a single template across ~2,000 customer domains, so one logical change becomes ~2,000 index-shifting patches. The impact is concentrated into rare, large events rather than spread thin.

That also bounds how far the index-pinning workaround generalises. It works for us because we own the template and change it deliberately. Anyone whose HTTPRoutes are edited by a CD pipeline, or who adds and removes rules dynamically, would hit this far more often and have much less room to keep indices stable.


Happy to provide more logs, or to run experiments against a build. We can reproduce this on demand in production with a two-line config change.


nhumphreys avatar Aug 06 '26 12:08 nhumphreys

This issue has been automatically marked as stale because it has not had activity in the last 30 days.

github-actions[bot] avatar Sep 06 '26 00:09 github-actions[bot]