TIMER WHEELS
A million timeouts without a million-entry heap
A timing wheel is a ring of 8 buckets. A pointer moves one bucket per tick and fires every timer in the bucket it reaches. A timer goes in bucket deadline mod 8, and nothing records how many laps it must wait. At t=0 you schedule a timer with delay 33. When does it fire?
A hashed timing wheel is a circular array of buckets and a pointer that advances one bucket per tick. To schedule a timer, put it in bucket deadline mod slots. On each tick, fire what is in the bucket under the pointer. Schedule and cancel are O(1), whatever the number of timers.
The catch is the span. A wheel of 8 slots only sees 8 ticks ahead. A timer 20 ticks out lands in a bucket the pointer reaches in 4. Either each timer records how many full rounds it still has to wait, or far timers go to a coarser wheel and cascade down later.
schedule(delay) {
const deadline = now + delay;
buckets[deadline % SLOTS].push(timer); // delay > SLOTS wraps around
}
tick() {
now++;
for (const t of buckets[now % SLOTS]) fire(t); // fires on the first pass
buckets[now % SLOTS] = [];
}schedule(delay) {
const deadline = now + delay;
timer.rounds = Math.floor((delay - 1) / SLOTS); // full laps to wait
buckets[deadline % SLOTS].push(timer);
}
tick() {
now++;
const b = buckets[now % SLOTS];
buckets[now % SLOTS] = b.filter(t => {
if (t.rounds > 0) { t.rounds--; return true; } // not this lap
fire(t); return false;
});
} Watch the two invariants. SORTED LIST fires every timer on time but breaks O(1): one insert touches 195 entries. WHEEL, NO ROUNDS schedules in O(1) and fires 200 of 250 timers early. WHEEL + ROUNDS keeps the same 8 buckets and the same O(1) schedule, and fires every timer exactly on its deadline. The CLOCK box shows the cost: ticks examine timers that still have laps to go.
Without a rounds counter, the only way to make every timer fire on time is a wheel wide enough to hold the longest delay. Raise WHEEL SLOTS until nothing fires early, and note how many buckets that costs.
GOAL: every timer fires on time with no rounds counter
This wheel puts each timer in bucket deadline % slots and fires everything the pointer reaches, so any delay longer than slots fires early. Give each timer a rounds count when it is scheduled and make tick() fire only timers with no rounds left (decrement the rest). tick() returns the ids fired on that tick.
Netty's HashedWheelTimer (a single wheel with a rounds counter), Kafka's request purgatory (hierarchical timing wheels), the Linux kernel's timer wheel for jiffy-based timeouts, and connection idle timers in proxies. Varghese and Lauck described the family in 1987.
“Design a scheduler for a million timeouts” or “how does Netty implement timeouts?” Compare heap (O(log n) insert, exact order) with a wheel (O(1) insert and cancel, tick precision), explain the span problem, and offer rounds or a hierarchy as the fix, with the per-tick cost each one pays.
USED IN
Chapters that build on this lesson.
- A design doc for one featureSoftware Engineer