Skip to main content

A failing backend out of rotation: api-breaker and Upstream health checks

Keep a two-node Upstream answering when one node dies with health checks, and stop asking a dead backend at all with api-breaker, both measured live.

By Akif F. Güven Updated Gateway

What you will have at the end

Two mechanisms, measured by stopping a backend while requests run. On a two-node Upstream with health checks, the node that died is taken out of rotation and twenty requests during the outage are all answered 200 by the other node; when it comes back, it is probed healthy and takes traffic again. On a Route to that one dead node with api-breaker, three failed attempts open the breaker, and the next requests are answered 503 by the gateway in a few milliseconds instead of waiting seconds for a connection that will not come. Traffic Explorer shows which node answered each request, and the audit log holds both changes.

Prerequisites

  • A backend on two nodes. Here the Upstream orders-ha has the nodes orders-svc:8080 and orders-v2:8080, two instances of the orders service that answer /health with 200, and the Route orders-ha-api serves /ha/orders/* from it.
  • For the breaker, a Route to a single node: orders-v2-api serves /v2/orders/* from the Upstream orders-v2, the node that the article stops.
  • Traffic logging turned on under Traffic Explorer › Settings, and a role that may manage Routes and Upstreams. The screenshots below were taken as nora.becker.

Two different questions

Health checks answer “which nodes of this Upstream may receive a request?” and keep the Upstream answering as long as one node is well. The breaker answers “should this Route ask its backend at all right now?” and spares the client the wait when the whole backend is down. The first is set on the Upstream, the second on the Route, and a production Route usually wants both.

Health checks on the Upstream

Open the Upstream orders-ha, select Edit upstream and scroll to Health checks. Switch Active health check on; the help says what it is: the engine probes every node on its own schedule, and off keeps every node in rotation regardless of its state. The fields that open:

  • Path: /health, the path the probe requests on every node; Type http, the default; Timeout 1 second.
  • Healthy › Interval 2 seconds and Successes 2: an unhealthy node is probed every two seconds and taken back after two successful probes in a row. HTTP statuses stay at 200, 302.
  • Unhealthy › HTTP failures 2, TCP failures 2 and Timeouts 2: two probes in a row answered with an unhealthy status, refused, or left unanswered take the node out. Interval for unhealthy nodes stays at its default.

Passive health check, the switch under it, marks nodes from real traffic and is allowed only together with the active check; it stays off here, so what follows is the active probe alone.

The Health checks section of the orders-ha Upstream form with Active health check on: Concurrency 10, Healthy HTTP statuses at their default, Interval 2 s, Successes 2, and the Changes panel on the right listing the seven settings about to be saved A probe every two seconds, two to flip a node either way. The Changes panel shows exactly what will reach the engine.

Select Save upstream. The Upstream page shows the settings and a line that matters for what comes next: node health is available with the Control API or Prometheus, not on this page, so the evidence of a node leaving rotation is in the traffic, below.

The breaker on the Route

Open the Route orders-ha-api, Plugins, Add plugin, api-breaker:

  • Break response code: 503. The help says what the client gets while the breaker is open: this code, and the upstream is not asked at all.
  • Unhealthy › Failures 3 and HTTP statuses 502. The note under the statuses is the one to read twice: only what the upstream answered counts, a connection it refuses counts as 502, and a status the gateway produced itself never opens the breaker.
  • Healthy › Successes 3: successes in a row that clear the failure count.
  • Longest time the breaker stays open: 30 seconds. The note explains the shape: the breaker first opens for two seconds and doubles that with every further round of failures, up to this value.

The api-breaker form: Break response code 503, Unhealthy Failures 3, HTTP statuses holding 502 with the note that a refused connection counts as 502, and the Healthy section below Three refused connections open the breaker; while it is open the client gets 503 at once.

Select Add plugin. The same Plugin, with the same values, was put on orders-v2-api, the single-node Route the measurement uses.

Stop a node and watch

One setting first. In a first run, with the Upstream’s Timeouts at the engine defaults, one request that was already on its way to orders-v2 when the container stopped waited 38.97 seconds on its first attempt before the retry answered it; its record’s Timing tab reads Gateway 2 ms, Upstream 38.97 s, 2 attempts. A client of that request saw a 200 after 39 seconds. So the Upstream’s Timeouts are set before the measurement: Connect 2 seconds, Send 2 seconds, Read 3 seconds, the longest a client will wait for a node that does not answer.

Then a request every quarter of a second for a minute; orders-v2 is stopped five seconds in and started again twenty-three seconds later:

### two-node Upstream, 150 requests, orders-v2 stopped at 23:25:41 and started at 23:26:04
codes: 150 × 200
rows over 1 s: 8, each 2.00–2.03 s

Every request was answered. Eight of them, spread over the outage, were handed to the stopped node first, waited the two-second connect timeout and were retried on the other node, which Traffic Explorer marks with +1 in the Upstream node column; the rest went straight to the surviving node. The health check decides which node a request is handed to; the timeouts decide what a request pays when the choice was wrong.

A stretch of the Traffic Explorer list for /ha/orders/ord-10421 during the outage: 200 records a quarter of a second apart, most answered by 172.23.0.24:8080 in a few milliseconds, four of them at 2.00 or 2.01 s marked +1, and from 23:26:04 on records answered by 172.23.0.30:8080 again Nothing failed; eight requests paid the connect timeout once. The +1 is the retry, and the node’s address is back half a second after the container.

Eight requests to the single-node Route, one per second, against the dead node:

21:12:33 502 3.106935s
21:12:38 502 2.414559s
21:12:41 502 2.995975s
21:12:45 503 0.006054s
21:12:46 503 0.003157s
21:12:47 502 1.017039s
21:12:49 502 2.986668s
21:12:53 502 3.002853s

Three attempts, each a 502 after about three seconds of trying to connect. The third opens the breaker: the next two requests are answered 503 in six and three milliseconds without a connection attempt. Two seconds later the breaker closes to try again, the attempt fails, and the next round opens it for longer, which is the doubling the form described.

Traffic Explorer filtered to the path /v2/orders: three 502 records with the node's address and about three seconds of latency, two 503 records with gateway as the Upstream node and 0 ms, further 502 records, and 200 records once the node is back The 503 rows name gateway as the Upstream node: the breaker answered, the backend was not asked.

Bring the node back

orders-v2 is started again. On the two-node Route the first record naming its address again is half a second after the container started, and from then on the two addresses alternate as before. On the breaker Route the next five requests are 200 in a few milliseconds each: the breaker closed on its next try and three successes cleared the count.

### breaker Route after the node is back
21:13:19 200 0.009562s
21:13:20 200 0.006426s
21:13:21 200 0.004251s

Under Analytics, the breaker Route’s Server errors card lists the 5xx by code and by source. For this window it holds 502 only, five of them, and by source Unreachable, the connections every node refused. Traffic Explorer has six 502 records and two 503s for the same minute: Analytics counts increases and leaves the first sample of a new series out, so the first 502 is missing and the two-sample 503 series is not counted in the card. The records are the complete list; the card is the share.

The Server errors card of the orders-v2-api Route: 5xx by code in the last 24 hours, a ring reading 5 · 5xx, By source Unreachable 100 %, and one row, 502 · 5 · 100 % By code and by source on one card. Unreachable is the node that refused; a 503 the breaker answered would be listed under the gateway.

What the audit log keeps

The health checks are an Upstream update: who, when, and the checks block as the difference, with the probe path, the intervals and the thresholds on the after side. The breaker is a Route update with the api-breaker block. Neither holds a secret.

The Change section of the audit record of the orders-ha Upstream update: the two nodes unchanged and, under checks, the active block added with healthy interval 2 and successes 2, timeout 1, http_path /health, and unhealthy timeouts, tcp_failures and http_failures at 2 The probe as the engine received it. A later change to a threshold is a second record with the old and the new value.

What to do next

Written by
Akif F. Güven
Engineer, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.