Terminology (6)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Exponentially weighted moving average Also EWMA
- A running estimate that moves a fraction toward each new value. Fixed weights fade geometrically per update; time-based weights depend on elapsed time.
- Smoothing factor Also α
- The weight given to the newest value. Larger α reacts faster and smooths less.
- Seed
- The starting estimate. Seeding with the first measurement avoids an invented initial zero pulling the early estimates downward.
- Half-life
- The time after which an old estimate keeps half its weight. It sets α for any interval: α = 1 − 2^(−Δt / h).
- Lag
- How long the smoothed value takes to follow a real change. Longer half-lives mean longer lag.
- Missing measurement
- A scrape or sample that failed. It carries no value and should not be replaced with 0.
Why this matters
Load balancers, autoscalers, adaptive timeouts and alerting rules all act on a smoothed signal so one noisy reading does not trigger them. Exponential smoothing is cheap, keeps one running level and appears in Unix load averages and TCP’s round-trip estimate. Its behavior depends on details that are easy to get wrong: the starting value, the weight for uneven intervals, and what happens when a measurement is missing.
This lesson assumes weighted averages and exponential decay from earlier lessons.
How exponential smoothing remembers
An exponentially weighted moving average, or EWMA, combines the previous level with each new measurement. The weight α controls how far the level moves toward that measurement. Repeated updates reduce the influence of older observations, letting one stored level summarize a history. The starting level is part of that history.
The weighted-average and exponential-decay ideas from earlier lessons connect through a half-life: the time over which the old level retains half its weight. For irregular measurements the elapsed time determines the next weight. A missing observation supplies no value; the smoothing rule and a separate freshness policy must say how to handle that gap.
01The update and its seed
The update and its seed
The first good scrape after two metric failures reported a 12% error rate. The smoother counted each failed scrape as 0% and gave the first good scrape after the gap the weight of one minute instead of three. The page came a full scrape later than it should have.
Before repairing gaps, the monitor checks whether startup invents a low error rate. Each new value moves the estimate a fraction α of the way toward it: level += α × (value − level). Older values fade by a factor (1 − α) per update. Predict the three lines this program prints.
const errorRates = [10, 10, 40, 10]; // % per scrape
const alpha = 0.3;
const run = (seed: number) => {
let level = seed;
return errorRates.map((x) => (level += alpha * (x - level)).toFixed(2)).join(" ");
};
console.log(run(errorRates[0]!)); // seeded with the first scrape
console.log(run(0)); // seeded with zero
console.log((1 - 2 ** (-60 / 300)).toFixed(4)); // weight for 60 s with a 300 s half-life
02Half-life to weight
Half-life to weight
The failed-scrape incident exposes a different assumption: an update is not always one minute after the previous real measurement. A half-life h says how fast old information fades in time, not per sample. After Δt seconds the old estimate keeps 2^(−Δt / h) of its weight, so the new value gets α = 1 − 2^(−Δt / h). With h = 300 s, a 60-second scrape gets α ≈ 0.1294; after a 600-second gap the next scrape gets 0.75.
2 ** (-dt / h) and Math.pow(2, -dt / h) are the same. Times from Date.now() are milliseconds, so keep the half-life in the same unit.
03Smoothing costs delay
Smoothing costs delay
| After | h = 60 s | h = 300 s |
|---|---|---|
| 60 s | 5.00% | 1.29% |
| 300 s | 9.69% | 5.00% |
| 600 s | 9.99% | 7.50% |
After one half-life the smoothed level has covered half of a real step. A longer half-life ignores more noise and pages later; a shorter one pages sooner and on more noise. The half-life is the decision.
04A missing value is not zero
A missing value is not zero
A failed scrape carries no measurement. Writing it as 0% pulls the level toward “healthy” exactly when scrapes fail, which is often when the service is in trouble. Skip it: the level stays put, and the next real scrape is weighted by the full time since the last real one. This model gives the newest rate that interval’s weight; it does not establish what the error rate was during the missing interval. Monitor freshness separately.
05Page on the level
Page on the level
06Smooth the error rate
Smooth the error rate
Restore the monitor’s startup level, then stop treating every scrape as one fixed interval. Finally repair the missing-scrape path without losing elapsed time or paging on the raw measurement. These rates use approximate binary64 arithmetic. Compute weight = 1 − exp2(−Δt / h), then level = level + weight × (value − level), and compare that computed level with the parsed threshold using ≥ before display rounding. There is no additional epsilon. Native exp2/power libraries can differ in their last bits; this model does not promise arbitrary cross-library bit identity at a threshold.
Error-rate smoothing
Smooth the series, use the half-life, then page on the smoothed level.
In the frontend A smoothed FPS badge in Next.js and Svelte Optional
This badge smooths the reciprocal frame interval using a positive halfLifeMs. It measures frame arrivals over wall-clock time, so a background pause contributes a long, slow interval when frames resume. It is not a foreground-only rendering metric. That metric would need a separate visibility/freshness policy, just as a monitor needs a missing-scrape policy.
"use client";
import { useEffect, useState } from "react";
// Smoothed frames per second; the weight uses the real time between frames,
// including background gaps; this is not foreground-only rendering FPS.
export function FpsBadge({ halfLifeMs = 500 }: { halfLifeMs?: number }) {
const [fps, setFps] = useState<number | null>(null);
useEffect(() => {
let level: number | null = null;
let last = performance.now();
let frame = requestAnimationFrame(function tick(now) {
const dt = now - last;
last = now;
if (dt > 0) {
const alpha = 1 - 2 ** (-dt / halfLifeMs);
level = level === null ? 1000 / dt : level + alpha * (1000 / dt - level);
setFps(level);
}
frame = requestAnimationFrame(tick);
});
return () => cancelAnimationFrame(frame);
}, [halfLifeMs]);
return <output>{fps === null ? "…" : `${fps.toFixed(0)} fps`}</output>;
}
Check this idea later
Sources
- RFC 6298: computing TCP’s retransmission timer (smoothed RTT)
- NIST/SEMATECH e-Handbook: single exponential smoothing
- Go: math.Exp2
Values above were computed in Node 22 and run in Go 1.27, Rust 1.99 and Apple clang 17. The error-rate service is a worked model.
Sign in to keep your read marks on every device. Sign in
Was this helpful?