TIMER WHEELS

A million timeouts without a million-entry heap

L215 MINALGORITHMS
PREDICT

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.

▶ SIMULATION
TIMERS
scheduled 0 · pending 0 · fired 0
fired early 0 · fired late 0
longest delay 0 ticks · wheel span 8 ticks
WHEEL
wheel, 8 slots, rounds counter
▶0 · 0 · 0 · 0 · 0 · 0 · 0 · 0
schedule() worst: 0 entries · mean 0.0
CLOCK
t = 0
entries examined by ticks: 0
✓ every timer fires at its deadline, never early✓ schedule() touches O(1) entries
SCHEDULE
01[TIMERS]config: one list sorted by deadline
02[APP]schedule 200 timers at t=0, delays 1..40
03[CLOCK]advance 8 ticks
04[CLOCK]advance 8 ticks
05[APP]schedule 50 more at t=16, delays 1..24
06[CLOCK]advance 8 ticks
07[CLOCK]advance 8 ticks
08[CLOCK]advance 8 ticks
09[CLOCK]report
Start of sorted list scenario. 9 steps.
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] = [];
}

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.

TUNE IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

a timing wheel trades exact ordering for O(1) schedule and cancel, using time itself as the hash. A bucket index alone cannot say which lap a timer belongs to, so a wheel shorter than your longest delay needs a rounds count or a hierarchy of coarser wheels. Without either, long timers fire early, and the bug is silent.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: CAPSTONE: TOP-K IN A STREAM →