Definition · TTL and stalenessA cache entry written at time t with a time to live T is served until t + T and then discarded. If the underlying value changes at some time inside that window, the entry is stale from the change until the window ends. The stale fraction is the expected proportion of the window during which the entry is stale.
This is a staleness measured in time, not in reads. Weighting by reads gives the same answer when reads are spread evenly through the window, which is the case this lesson computes.
Two assumptions make the formula possible and both are visible on the page rather than buried. The first is that updates are periodic, every U seconds. The second is that the phase between the refresh clock and the update clock is uniform — the refresh lands at a time unrelated to the updates. Neither is true of a real system, and the lab lets you fix the phase and watch a single run disagree with the average.
Where the two branches come from
Let G be the time from the start of a refresh window to the next update, uniform on [0, U). The entry is fresh until that update and stale for the remainder of the window, so the stale time is max(0, T − G) — the piecewise function of Algebra, and the expected value of Discrete Mathematics applied to it.
stale time in one window = max(0, T − G), G uniform on [0, U)
T ≤ U: E[max(0, T − G)] = T²/(2U)
fraction = T²/(2U) ÷ T = T/(2U)
T ≥ U: an update always lands in the window, so stale time = T − G
E = T − U/2, fraction = 1 − U/(2T)
at T = U both give 1/2
Example · A one-minute update, a ten-second TTLU = 60 s, T = 10 s. Since T ≤ U the stale fraction is T/(2U) = 10/120 = 1/12 = 8.33%. Stretch the TTL to 30 s and it becomes 1/4 = 25.00%; to 60 s and it is 1/2 = 50.00%; to 120 s and the other branch gives 3/4 = 75.00%. A TTL twice the update interval means three quarters of the time you are serving something wrong.
What the other direction costs
So shorten the TTL. Staleness falls linearly — and every key now expires more often, so it misses more often. A key read λ_key times a second expires once every T seconds, and exactly one of the λ_key·T reads in that window pays for the refill, giving a miss rate of 1/(λ_key·T). Once λ_key·T ≤ 1 the key expires between reads and every read is a miss.
Example · The trade, at five reads a secondλ_key = 5. At T = 10 s the stale fraction is 1/12 and the miss rate 1/50 = 2.00%. At T = 1 s the stale fraction falls to 1/120 = 0.83% and the miss rate rises to 1/5 = 20.00% — a tenth of the staleness for ten times the backend reads. At T = 240 s the stale fraction is 7/8 = 87.50% and the miss rate is 1/1200 = 0.08%.
Both ends of that trade are a choice about what the system is for. A price display can be a minute wrong and must not melt the pricing service; a permission check can be nothing wrong and is read rarely enough that a short TTL costs little. The formula does not decide; it prices both options in the same units so the decision is not made by whoever spoke last.
Why the lab runs a timeline beside the formula
T/(2U) is an expectation over the phase, not a prediction about a particular run. Fix the phase at 30% of U with U = 60 s and T = 10 s and the run actually measures 1/30 = 3.33% stale against a formula that says 1/12 = 8.33%; averaged over twenty-four phases it lands back on 8.33% exactly. Neither number is wrong. They answer different questions.
That is the honest way to use this: as a sensitivity rather than a forecast. It tells you that doubling the TTL roughly doubles the staleness while T ≤ U, and that is a shape you can rely on even when the update process is not periodic at all. What it will not give you is the stale fraction your system had last Tuesday, and a model whose assumptions are on the page is one you can tell that about.