v7 has its eval results, and every rating and kept trace now names the version behind its answer. However, your eval cases hold only the questions you thought of. Hikers bring the rest, in other phrasings and other languages, and about other huts on the same day.
So v7 has to meet real questions, a few hikers at a time, with a way back. In this lesson, you’ll put v7 behind a flag that your code reads on every request to give each hiker one version. You’ll roll it back by changing one line. For every split, you first have to decide which hikers get v7, so start there.
Give Each Hiker a Bucket
A feature flag is a setting your code reads to decide which version runs. The quickest way to send a share of traffic to v7 is a random draw on each request, but hikers don’t stop at one question. Someone planning the Col de Fenêtre crossing to hut 6 could get v5’s answer, then ask a follow-up and get v7’s, built on a history v5 wrote. If they then repeat a question or rate an answer down, you can’t attribute that feedback to either version.
Instead, give each hiker a bucket: a number from 0 to 99 computed from their anonymous hiker ID, so it’s the same on every request. Hikers in buckets below the flag’s share get the new version. Create config/flags.json, naming the current version, the new one, and its share:
config/flags.json
{
"answer": {
"current": "v5",
"next": "v7",
"share": 0
}
}
Then compute the bucket in a new flags.js:
flags.js
import { createHash } from "node:crypto";
export function bucketOf(name, hiker) {
const hash = createHash("sha256")
.update(`${name}:${hiker}`).digest();
return hash.readUInt32BE(0) % 100;
}
export function pickVersion(flags, name, hiker) {
const { current, next, share } = flags[name];
if (!hiker) return { id: current, bucket: null };
const bucket = bucketOf(name, hiker);
return { id: bucket < share ? next : current, bucket };
}
A SHA-256 hash turns the same input into the same bytes every time. bucketOf() reads the first four bytes as a number and reduces it with % 100, which spreads hikers evenly over the buckets. Hashing the flag’s name with the ID gives every flag its own split, so a later flag doesn’t land on the same unlucky 5 percent.
Every new answer version reuses the answer flag, though, so buckets 0 to 4 go first each time, and the same 5 percent of hikers try every untried version. You can add the new version’s ID to the hash to vary who goes first. This lesson keeps the flag name alone, since v7 is the only rollout so far.