---
/**
* `<BackfillFanout />` — how a Domain backfill reaches every instance, and
* how the tree grows with the Domain. A relay given more than six items splits
* them among six child relays (Cloudflare runs at most six outgoing calls per
* invocation), so the tree gets deeper, not wider: about log6 of the instance
* count levels. The figure draws one path down the tree for 10 thousand, 100
* thousand and a million instances, level by level; at each level the other
* five relays do the same work beside it. Counts per level are approximate:
* six to the power of the level.
*/
const sizes = [
{ n: 10_000, label: "10k" },
{ n: 100_000, label: "100k" },
{ n: 1_000_000, label: "1M" },
];
---
<figure class="bf not-content" id="bf" aria-label="A backfill tree growing deeper as the Domain grows">
<div class="bf-top">
<span class="bf-step" id="bf-step"></span>
<span class="bf-sizes" role="group" aria-label="Instances">
{sizes.map((s, i) => <button type="button" data-size={i}>{s.label}</button>)}
</span>
<button type="button" id="bf-pause" aria-pressed="false">❚❚ Pause</button>
</div>
<svg id="bf-svg" viewBox="0 0 820 420" role="img" aria-hidden="true"></svg>
<figcaption class="bf-cap">
One path down the tree; at every level the other five relays do the same beside it. Each relay calls at most
six at once, because a Cloudflare Worker invocation runs at most six outgoing calls at a time.
</figcaption>
</figure>
<style is:global>
.bf {
margin: 2rem 0;
padding: 12px 14px 10px;
font: 14px/1.5 var(--alc-font-sans);
color: var(--alc-fg-1);
background: var(--alc-bg-elev-1);
border: 1px solid var(--alc-hairline-2);
border-radius: 12px;
}
.bf-top {
display: flex;
align-items: flex-start;
gap: 10px;
min-height: 3em; /* two lines, so a longer step doesn't move the tree */
margin-bottom: 6px;
}
.bf-step {
flex: 1;
font-weight: 700;
font-size: 15px;
}
.bf-sizes {
display: inline-flex;
gap: 4px;
}
.bf button {
all: unset;
flex: none;
cursor: pointer;
padding: 3px 10px;
font-size: 12px;
font-weight: 600;
color: var(--alc-fg-1);
background: var(--alc-bg-elev-1);
border: 1px solid var(--alc-hairline-3);
border-radius: 7px;
}
.bf button[aria-current="true"] {
color: var(--alc-accent-text);
border-color: var(--alc-accent);
}
.bf button:focus-visible {
outline: 2px solid var(--alc-accent);
}
.bf svg {
display: block;
width: 100%;
height: auto;
transition: height 0.4s ease;
}
.bf-label {
font: 600 11px var(--alc-font-mono);
fill: var(--alc-fg-3);
}
.bf-count {
font: 600 11px var(--alc-font-mono);
fill: var(--alc-fg-3);
text-anchor: end;
}
.bf-edge {
fill: none;
stroke: var(--alc-accent);
stroke-width: 1.2;
stroke-opacity: 0.6;
stroke-dasharray: 1;
stroke-dashoffset: 1;
transition: stroke-dashoffset 0.45s ease;
}
.bf-stub {
stroke: var(--alc-accent);
stroke-opacity: 0.35;
stroke-width: 1;
stroke-dasharray: 2 3;
opacity: 0;
transition: opacity 0.4s ease 0.3s;
}
.bf-node {
opacity: 0;
transition: opacity 0.3s ease 0.3s;
}
.bf-on .bf-edge {
stroke-dashoffset: 0;
}
.bf-on .bf-node,
.bf-on .bf-stub,
.bf-on .bf-row-text {
opacity: 1;
}
.bf-row-text {
opacity: 0;
transition: opacity 0.3s ease 0.2s;
}
.bf-node rect {
fill: var(--alc-bg-elev-1);
stroke: var(--alc-accent);
stroke-width: 1.2;
}
.bf-node.bf-root rect {
fill: color-mix(in srgb, var(--alc-accent) 22%, transparent);
}
.bf-node text {
font: 700 12px var(--alc-font-mono);
fill: var(--alc-fg-1);
text-anchor: middle;
}
.bf-relay {
fill: var(--alc-bg-elev-1);
stroke: var(--alc-accent);
stroke-width: 1.2;
}
.bf-relay.bf-path {
fill: color-mix(in srgb, var(--alc-accent) 45%, transparent);
}
.bf-relay.bf-side {
stroke-opacity: 0.5;
}
.bf-instance {
fill: #e3b341; /* aggregate */
}
.bf-key {
fill: #7fb069; /* view */
}
.bf-cap {
margin-top: 6px;
font-size: 13px;
color: var(--alc-fg-3);
text-align: center;
}
</style>
<script define:vars={{ sizes }}>
const root = document.getElementById("bf");
if (root) {
const NS = "http://www.w3.org/2000/svg";
const FAN = 6;
const W = 820;
const LEFT = 150;
const RIGHT = 760;
const ROW = 42;
const TOP = 26;
const GAP = 34; // between the six children of one relay
const svg = document.getElementById("bf-svg");
const label = document.getElementById("bf-step");
const pause = document.getElementById("bf-pause");
const buttons = [...root.querySelectorAll("[data-size]")];
const reduced = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
const fmt = (n) => n.toLocaleString("en-US");
let paused = false;
let pinned = false;
let token = 0;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const wait = async (ms, mine) => {
await sleep(ms);
while (paused && mine === token) await sleep(120);
return mine === token;
};
const el = (name, attrs, parent) => {
const node = document.createElementNS(NS, name);
for (const [k, v] of Object.entries(attrs)) node.setAttribute(k, String(v));
parent.appendChild(node);
return node;
};
const curve = (x1, y1, x2, y2) => {
const my = (y1 + y2) / 2;
return `M${x1} ${y1}C${x1} ${my} ${x2} ${my} ${x2} ${y2}`;
};
const clamp = (x) => Math.max(LEFT + GAP * 2.5, Math.min(RIGHT - GAP * 2.5, x));
/**
* Rows: the run, then relay levels until the instances. Levels of relays:
* about log6(n), the depth at which six-way splits reach every instance.
*/
const build = (n) => {
const depth = Math.ceil(Math.log(n) / Math.log(FAN));
const relayLevels = depth - 1;
svg.replaceChildren();
const rows = [];
const rowY = (i) => TOP + i * ROW;
const height = rowY(relayLevels + 3) - 10;
svg.setAttribute("viewBox", `0 0 ${W} ${height}`);
// The run.
let x = (LEFT + RIGHT) / 2;
const runRow = el("g", {}, svg);
const runNode = el("g", { class: "bf-node bf-root" }, runRow);
el("rect", { x: x - 60, y: rowY(0) - 13, width: 120, height: 26, rx: 7 }, runNode);
el("text", { x, y: rowY(0) + 4 }, runNode).textContent = "Backfill run";
el("text", { class: "bf-label bf-row-text", x: 8, y: rowY(0) + 4 }, runRow).textContent = "run";
rows.push(runRow);
// Relay levels, then the instances and the keys they feed.
let from = { x, y: rowY(0) + 13 };
for (let level = 1; level <= relayLevels + 1; level++) {
const instances = level === relayLevels + 1;
const y = rowY(level);
const row = el("g", {}, svg);
const center = clamp(x);
const kids = Array.from({ length: FAN }, (_, i) => center + (i - (FAN - 1) / 2) * GAP);
const next = (level % 2 === 0 ? 3 : 2);
for (const [i, kx] of kids.entries()) {
el("path", { class: "bf-edge", d: curve(from.x, from.y, kx, y - 7), pathLength: 1 }, row);
if (instances) {
el("circle", { class: "bf-node bf-instance", cx: kx, cy: y, r: 5 }, row);
} else {
const path = i === next;
el("circle", { class: `bf-node bf-relay ${path ? "bf-path" : "bf-side"}`, cx: kx, cy: y, r: 6 }, row);
if (!path) el("line", { class: "bf-stub", x1: kx, y1: y + 7, x2: kx, y2: y + 20 }, row);
}
}
const count = instances ? n : Math.min(FAN ** level, Math.ceil(n / FAN));
el("text", { class: "bf-label bf-row-text", x: 8, y: y + 4 }, row).textContent = instances
? "instances"
: `relays · level ${level}`;
el("text", { class: "bf-count bf-row-text", x: W - 8, y: y + 4 }, row).textContent =
`${instances ? "" : "≈ "}${fmt(count)}`;
rows.push(row);
x = kids[next];
from = { x, y: y + 7 };
if (instances) {
// Each instance's history reaches the keys it feeds.
const keys = el("g", {}, svg);
const ky = rowY(level + 1);
for (const kx of kids) {
el("path", { class: "bf-edge", d: `M${kx} ${y + 6}L${kx} ${ky - 6}`, pathLength: 1 }, keys);
el("circle", { class: "bf-node bf-key", cx: kx, cy: ky, r: 5 }, keys);
}
el("text", { class: "bf-label bf-row-text", x: 8, y: ky + 4 }, keys).textContent = "view keys";
rows.push(keys);
}
}
return { rows, depth };
};
const show = async (i, mine) => {
const { n } = sizes[i];
buttons.forEach((b, j) => b.setAttribute("aria-current", String(i === j)));
const { rows, depth } = build(n);
const text = `${fmt(n)} instances: ${depth} levels of calls, each relay calling at most six at once.`;
if (reduced()) {
rows.forEach((r) => r.classList.add("bf-on"));
label.textContent = text;
return true;
}
label.textContent = `${fmt(n)} instances`;
void svg.getBoundingClientRect();
for (const r of rows) {
r.classList.add("bf-on");
if (!(await wait(450, mine))) return false;
}
label.textContent = text;
return true;
};
const play = async (start = 0) => {
const mine = ++token;
if (reduced()) return void show(sizes.length - 1, mine);
for (let i = start; ; i = (i + 1) % sizes.length) {
if (!(await show(i, mine))) return;
if (!(await wait(3000, mine))) return;
if (pinned) return;
}
};
buttons.forEach((b, i) =>
b.addEventListener("click", () => {
pinned = true;
paused = false;
pause.textContent = "❚❚ Pause";
pause.setAttribute("aria-pressed", "false");
void play(i);
}),
);
pause.onclick = () => {
paused = !paused;
pause.textContent = paused ? "▶ Play" : "❚❚ Pause";
pause.setAttribute("aria-pressed", String(paused));
};
// Until it scrolls into view, show the largest tree, drawn.
build(sizes[sizes.length - 1].n).rows.forEach((r) => r.classList.add("bf-on"));
label.textContent = "1,000,000 instances: 8 levels of calls, each relay calling at most six at once.";
buttons.forEach((b, j) => b.setAttribute("aria-current", String(j === sizes.length - 1)));
const io = new IntersectionObserver(
(entries) => {
if (entries.some((e) => e.isIntersecting)) {
io.disconnect();
void play();
}
},
{ threshold: 0.3 },
);
io.observe(root);
}
</script>