Learners in Kathmandu, London and Virginia
Run in several regions and DNS (like Route 53) decides where each user goes: the closest region, a primary with a standby, or a weighted split for gradual rollouts. Health checks keep traffic away from a sick region — but one failed check isn't enough to declare it dead, or a blip would cause a stampede.
Health and routing
function healthFrom(results, threshold = 3) {
let healthy = true;
let streak = 0;
for (const ok of results) {
streak = ok !== healthy ? streak + 1 : 0; // count results that disagree with the current state
if (streak >= threshold) {
healthy = ok;
streak = 0;
}
}
return healthy;
}
function route(records, { policy, userRegion, random = Math.random }) {
const healthy = records.filter((r) => r.healthy);
if (healthy.length === 0) throw new Error("No healthy endpoints");
if (policy === "failover") {
const primary = healthy.find((r) => r.role === "primary");
return (primary ?? healthy[0]).region;
}
if (policy === "latency") {
return healthy.reduce((best, r) => (r.latency[userRegion] < best.latency[userRegion] ? r : best)).region;
}
if (policy === "weighted") {
let x = random() * healthy.reduce((sum, r) => sum + r.weight, 0);
for (const r of healthy) {
x -= r.weight;
if (x < 0) return r.region;
}
return healthy[healthy.length - 1].region;
}
throw new Error("Unknown policy");
}const records = [
{ region: "ap-south-1", role: "primary", weight: 3, healthy: true, latency: { kathmandu: 40, london: 120, virginia: 200 } },
{ region: "eu-west-2", role: "secondary", weight: 1, healthy: true, latency: { kathmandu: 150, london: 10, virginia: 80 } },
{ region: "us-east-1", role: "secondary", weight: 1, healthy: true, latency: { kathmandu: 230, london: 75, virginia: 5 } },
];