Joining and removing engines¶
Add an engine to a running fleet, or remove one, without restarting the router. The router serves the engines in its membership snapshot. Engine membership lists the routes, refusals and status codes.
Prerequisites:
recovery.engine_restart_policyset toindividual- a complete
engine_contractin the fleet configuration - an engine outside any
shared_devicegroup - every engine's
stateisactiveinGET /narwhal/lifecycle. For a removal, the engine you remove can instead bedrained,blockedordeadline_exceededoutside a wave - for a join, an
engine.control_connectionssized for the engine count after the join, as Global admission describes - the same Narwhal release on both routers, so the standby can adopt the membership after a join or removal
- a shell on the router host, in the environment and working directory of the router's launch, with
curland Python 3. That environment holds the router's control token inNARWHAL_ROUTER_TOKEN, which every join, removal, drain and readmission sends
The commands below use the fleet file runs/deployment/fleet.json, the profile store runs/profiles.json at its profiles.path, the calibration artifact runs/deployment/first-token-calibration.json at its engine.first_token_calibration_path, and the limits file runs/deployment/profiling-limits.json. Replace them with your paths.
Before each join or removal, set ROUTER_URL to the router that holds the lease, as in Engine restart and process replacement. Use the router's own address, not the load balancer's. The load balancer answers the membership and lifecycle routes with HTTP 404, and the standby answers them with HTTP 503. Set IID to the engine ID, and create a private run directory:
for url in http://router-a:8000 http://router-b:8000; do
curl -fsS -o /dev/null "$url/narwhal/lifecycle" && ROUTER_URL=$url
done
echo "lease holder: ${ROUTER_URL:?no router holds the lease}"
IID='e4'
STAMP="$(date -u +%Y%m%dT%H%M%SZ)"
RUN_DIR="runs/membership-$STAMP"
umask 077
mkdir -p "$RUN_DIR"
The router reads the profile store when an engine joins, at startup, and when a resumed restart or a standby takeover applies the state handoff. The steps below write each new store in runs/ under a name that carries $STAMP, such as runs/profiles-before-$STAMP.json, and copy it over runs/profiles.json. Keep every store and sidecar that the steps write. The installed store's sidecar refers to each source store by its path and SHA-256 hash, as Selection, refitting, and output describes.
On a fleet that narwhal deploy runs¶
On a narwhal deploy fleet, the inventory decides the engines. Each router container runs without resume. It reads the fleet file and profile store that the run's fleet-limits stage wrote, runs/deploy/<run-id>/stages/fleet-limits/attempt-<n>/fleet.json and the files beside it. It does not read runs/deployment/fleet.json. A join or removal made with the steps below lasts only while the routers pass it on in their handoff. A router that starts without a handoff that carries the membership serves the inventory's engines again. This happens after both routers stop, and on a fleet with one router. The next run that reruns fleet-limits writes the file from the inventory, and a removed engine that is still in the inventory starts again.
To add or remove an engine for good, add or remove its host's NARWHAL_NODE_<n>_* inputs, then run narwhal deploy run --run <run>. A change to the engine hosts or their engines reruns access, discover and every stage after it. The run restarts every engine, and the fleet serves nothing until router-start passes, as Rerunning after a change lists. Keep the steps below for an engine you add to a running pair for a while, or for a fleet whose routers you run yourself.
Join an engine¶
Start the engine¶
-
Start the engine and its attestation sidecar from the members' checkpoint. Use the fleet's engine contract, as in Preparing, checking, and starting each engine and Starting the sidecar.
A native vLLM engine must also load the model from the members' path. Set
NARWHAL_MODEL_DIR, andNARWHAL_MODEL_PATHwhen the members set it, to the members' values. vLLM's NIXL handshake compares a hash that covers the--modelpath. An engine that loads identical files from another directory passesmodel identity. It then fails the fabric check, and a member's engine log showsNIXL compatibility hash mismatch. A container engine loads a checkpoint directory at/modelon every host, so its host directory can differ. -
Check that the engine's
/healthreturns HTTP 200 and that/v1/modelslists the configured model.
Write the candidate fleet file¶
Write the engine's entry, as the fleet file holds it:
cat > "$RUN_DIR/$IID.json" <<'JSON'
{
"iid": "e4",
"url": "http://node4:8000",
"attestation_url": "http://node4:8010/v1/attestation",
"role": "decode"
}
JSON
role sets the role the engine joins in. When engine.connector keeps the backend's engine roles fixed, set pin to true. Write each URL in full: the router refuses ${VARIABLE} references in a join entry.
Add the entry to a copy of the fleet file and validate it:
python3 - runs/deployment/fleet.json "$RUN_DIR/$IID.json" "$RUN_DIR/fleet-with-$IID.json" <<'PY'
import json
import sys
fleet = json.load(open(sys.argv[1]))
fleet["engines"].append(json.load(open(sys.argv[2])))
with open(sys.argv[3], "w") as out:
json.dump(fleet, out, indent=2)
out.write("\n")
PY
.venv/bin/narwhal config validate --fleet "$RUN_DIR/fleet-with-$IID.json"
Profile the engine¶
The join requires a store with a profile for each member and the new engine. A removal leaves the engine's rows in the store. A merge whose --fleet file omits the engine drops those rows. The store update after a removal does this, and so does step 6 below for the join of another engine. An engine that rejoins while the store still holds its rows needs no new profile if it has the process generation of its stored profile. In that case, skip this section.
Copy the profile of a member in place of steps 1 to 4 when the new engine meets two conditions:
- It runs the same launch and model files as the member.
- It runs on the same GPU type as the member.
Replace e0 with the member:
.venv/bin/narwhal-profile --fleet "$RUN_DIR/fleet-with-$IID.json" \
--copy-from e0 --only "$IID" --out "runs/profiles-$IID-$STAMP.json"
The copy needs no drained peer. It refuses an engine whose launch evidence or KV capacity differs from the member's profile, as Copying a member's profile describes. Profile that engine with steps 1 to 4, then continue at step 5.
-
The profiler requires the limits file to name exactly the candidate fleet's engines. Add the new engine's sequence limit to a copy, replacing
256with itsruntime.max_sequences, as Profiling idle engines describes: -
On an SGLang fleet, the sweep also switches and loads a peer engine, as SGLang pairs describes. With
--only, the peer is the engine after the new one in the candidate fleet file. When the new engine is last, as the script above writes it, the peer is the first engine. To choose another peer, sweep with a copy of the candidate fleet file. In the copy, move the peer to the front and keep the new engine last. The merge and the join do not depend on engine order. Drain the peer until it reportsready to stop. Keep it running until the sweep ends. With--router-url, the profiler stops before it contacts any engine if the router has not drained the peer, as Router drain check describes. -
Profile the new engine alone, with the sweep options of the current profiles:
-
On an SGLang fleet, restart the peer and request its readmission, as in Restart one engine. Readmission after a drain requires a new engine process, and the join requires the peer's
stateto beactiveagain. -
Copy the current store and its sidecar:
-
Merge the copy and the new profile into a store for the candidate fleet:
-
Install the merged store at
profiles.path: -
Copy every file these steps wrote under
runs/to the same path on the standby router host.
Calibrate the first-token deadline¶
When engine.first_token_calibration_path is set, the join requires a calibration that covers every directed pair of the candidate fleet. Calibrate against the candidate fleet file. Use the current artifact as the base:
.venv/bin/narwhal-check --fleet "$RUN_DIR/fleet-with-$IID.json" \
--calibrate-first-token --input-tokens 256,8192,16382 \
--samples 100 --observation-timeout-s 20 \
--calibration-base runs/deployment/first-token-calibration.json \
--calibration-out "$RUN_DIR/first-token-calibration.json"
Use the --input-tokens and --samples that the current artifact records in input_tokens and samples_per_group. When a recorded length leaves room for fewer than two output tokens, run the command without --calibration-base, with --input-tokens chosen as in Calibrating the first-token deadline. Set --observation-timeout-s above engine.first_token_timeout_s and at most serving.request_timeout_s.
With --calibration-base, the run reuses each complete group of the current artifact whose two engines keep their URL and process generation. It measures the rest: every directed pair that includes the new engine, and the pairs of any member whose URL or process generation changed.
On SGLang, the run switches each measured pair's engines into the roles its probes need. It does this on the engines themselves, outside the router's role control. At the end, it returns each measured engine to its starting role.
Run the calibration while the members carry no client traffic. Alternatively, calibrate one member at a time, as Calibrate under client traffic describes. The pairs that include the new engine run one at a time on SGLang and at most two at a time on other backends.
Calibrate under client traffic¶
To keep the fleet serving, calibrate the new engine against one member at a time. The router holds that member out of placement while the run measures it, and the other members serve client requests. The remaining members must carry the client load. The fleet must keep a live prefill engine and a live decode engine without the held member. Under recovery.engine_restart_policy: whole_wave the router drains only whole waves, so calibrate with the fleet idle instead.
For each member:
- Drain it without a restart, and wait until the router reports it
ready_to_stop. - Measure the pairs between it and the new engine, with the previous artifact as the base.
--router-urlstops the run before its first probe when the router has not drained the member. - Readmit it. The readmission accepts the same engine process.
Replace the member IDs with those of the current fleet:
BASE=runs/deployment/first-token-calibration.json
for MEMBER in e0 e1 e2; do
curl -fsS -X POST "$ROUTER_URL/narwhal/lifecycle/drain" \
-H "Authorization: Bearer $NARWHAL_ROUTER_TOKEN" \
-H 'content-type: application/json' \
-d "{\"engines\": [\"$MEMBER\"], \"restart\": false}" > /dev/null
until curl -fsS "$ROUTER_URL/narwhal/lifecycle" | python3 -c \
'import json, sys; sys.exit(not json.load(sys.stdin)["engines"][sys.argv[1]]["ready_to_stop"])' \
"$MEMBER"; do sleep 2; done
.venv/bin/narwhal-check --fleet "$RUN_DIR/fleet-with-$IID.json" \
--calibrate-first-token --input-tokens 256,8192,16382 \
--samples 100 --observation-timeout-s 20 \
--pairs-with "$MEMBER" --router-url "$ROUTER_URL" \
--calibration-base "$BASE" \
--calibration-out "$RUN_DIR/first-token-calibration-$MEMBER.json"
curl -fsS -X POST "$ROUTER_URL/narwhal/lifecycle/readmit" \
-H "Authorization: Bearer $NARWHAL_ROUTER_TOKEN" \
-H 'content-type: application/json' \
-d "{\"engines\": [\"$MEMBER\"]}" > /dev/null
BASE="$RUN_DIR/first-token-calibration-$MEMBER.json"
done
cp "$BASE" "$RUN_DIR/first-token-calibration.json"
Each run but the last exits 1 and writes an incomplete artifact, because unmeasured_groups lists the pairs of the members still to come. The last run reuses every earlier group. It writes a complete artifact when every group passed.
A pair between two members whose URL or process generation changed needs both members drained. The run that reaches such a pair stops with the message of the router drain check. When a readmission fails, fix the cause that GET /narwhal/lifecycle reports and readmit the member again before you continue.
When the command exits 0 and the artifact's candidate_deadline_s is below engine.first_token_timeout_s, keep a copy of the current artifact and install the new one:
cp runs/deployment/first-token-calibration.json "$RUN_DIR/first-token-calibration-before.json"
cp "$RUN_DIR/first-token-calibration.json" runs/deployment/first-token-calibration.json
Copy both files to the same paths on the standby router host.
The join fails when the new candidate deadline is at or above engine.first_token_timeout_s. Raising the timeout takes a router restart with the new fleet file.
Request the join¶
Send the entry to the active router:
curl -sS -X POST "$ROUTER_URL/narwhal/engines" \
-H "Authorization: Bearer $NARWHAL_ROUTER_TOKEN" \
-H 'content-type: application/json' \
-d @"$RUN_DIR/$IID.json" \
-o "$RUN_DIR/join-response.json" -w '%{http_code}\n'
The router runs the checks in POST /narwhal/engines, and curl prints the HTTP status when they finish. Read the result:
python3 - "$RUN_DIR/join-response.json" "$IID" <<'PY'
import json
import sys
doc = json.load(open(sys.argv[1]))
join = doc.get("join") or {}
if join.get("joined"):
print(sys.argv[2], "joined in generation", doc["generation"], "checks:", join["checks"])
else:
print("refused:", doc["error"])
print("passed:", join.get("checks", []))
PY
On HTTP 200, the engine takes new requests in its entry's role. On HTTP 409, error gives the refusal or the failed checks. Fix the cause and repeat the request. HTTP 422 reports a problem in the entry, and HTTP 503 means the router does not control the fleet.
To abandon a refused join, install the pre-join store again on both router hosts:
cp "runs/profiles-before-$STAMP.json" runs/profiles.json
cp "runs/profiles-before-$STAMP.samples.json" runs/profiles.samples.json
Without the pre-join store, a router that starts with resume off, or resumes a handoff at generation 0, fails with stale profiles for unconfigured engines. If you installed a new calibration artifact, also copy $RUN_DIR/first-token-calibration-before.json over runs/deployment/first-token-calibration.json.
Keep the engine across restarts¶
After the join succeeds, install $RUN_DIR/fleet-with-$IID.json as the fleet file of both routers' launch commands. Every restart then keeps the engine. On a narwhal deploy fleet, no installed file keeps it. On a fleet that narwhal deploy runs describes why. When an engine rejoins under the same ID with a different entry, install the new fleet file on each router host before the next restart or failover.
Remove an engine¶
Drain and remove the engine¶
Remove the engine with a 300-second drain deadline:
curl -sS -X DELETE "$ROUTER_URL/narwhal/engines/$IID?deadline_s=300" \
-H "Authorization: Bearer $NARWHAL_ROUTER_TOKEN" \
-o "$RUN_DIR/leave-response.json" -w '%{http_code}\n'
HTTP 200 means the engine had no resident work and left at once. HTTP 202 means it is draining. While it drains, repeat this check until it prints removed:
curl -fsS "$ROUTER_URL/narwhal/engines" > "$RUN_DIR/membership.json"
python3 - "$RUN_DIR/membership.json" "$IID" <<'PY'
import json
import sys
doc = json.load(open(sys.argv[1]))
iid = sys.argv[2]
ids = [engine["iid"] for engine in doc["engines"]]
assert iid not in ids, f"{iid} still leaving" if iid in doc["leaving"] else f"{iid} still a member"
print(iid, "removed in generation", doc["generation"])
PY
GET /narwhal/lifecycle shows the engine's drain state and resident requests.
On HTTP 409, error names the rule that refused the removal. On HTTP 503 with a process-identity error, the engine stays out of placement. Repeat the request once the engine and its attestation sidecar answer.
Stop the engine and its attestation sidecar through their process manager.
Update the fleet file and the profile store¶
After the router removes the engine:
-
Write the fleet file without the engine:
python3 - runs/deployment/fleet.json "$IID" "$RUN_DIR/fleet-without-$IID.json" <<'PY' import json import sys fleet = json.load(open(sys.argv[1])) fleet["engines"] = [engine for engine in fleet["engines"] if engine["iid"] != sys.argv[2]] with open(sys.argv[3], "w") as out: json.dump(fleet, out, indent=2) out.write("\n") PY -
Copy the current store and its sidecar, and merge the copy alone. The merge leaves out the rows of engines outside the fleet file:
cp runs/profiles.json "runs/profiles-before-$STAMP.json" cp runs/profiles.samples.json "runs/profiles-before-$STAMP.samples.json" .venv/bin/narwhal-profile --fleet "$RUN_DIR/fleet-without-$IID.json" \ --merge "runs/profiles-before-$STAMP.json" --out "runs/profiles-after-$STAMP.json"The merge prints
left out profiles for engines outside the fleet: <iid>. -
Install the fleet file and the store:
-
Copy the fleet file and every file these steps wrote under
runs/to the same paths on the standby router host.
A router started with resume off serves the fleet file's engines and requires a store with a profile for exactly those engines. The first-token calibration artifact stays valid after a removal.