A hashed timing wheel assigns each due tick to a circular slot by remainder. The active map stores the exact due tick, payload, and slot for every incident ID. The slot set holds IDs whose due ticks land there, including timers several complete revolutions away. Advancing time visits each intervening tick and examines the current slot; only entries whose exact due tick has arrived are removed and returned. Rescheduling an ID removes it from its old slot before inserting the new record, so an old expiry cannot fire after replacement. Cancellation removes both the map entry and slot membership. Time is an integer tick supplied by the caller and may move forward only. This model is single-process memory state, not a wall-clock scheduler or a durable job queue.
Hashed timing wheels: bucket incident expiries by tick
Operational case
Incident 47 first has a review due at tick three, while incident 19 is due at tick eleven. Replacing 47's review sets its due tick to five. Advancing to tick three returns nothing; at tick five the replacement fires once with its new payload. The slot for tick eleven matches an earlier slot after one wheel revolution, but the exact due value keeps incident 19 pending until tick eleven. A missing cancellation raises an error, and a zero-delay schedule is rejected. The wheel's fixed slot count determines bucket collisions, not the maximum timer horizon; distant timers can remain in a slot across many turns.
Working Python program
class IncidentExpiryWheel:
def __init__(self, slot_count=8):
if slot_count < 1:
raise ValueError("wheel must have a positive number of slots")
self.slot_count = slot_count
self.now = 0
self.slots = [set() for _ in range(slot_count)]
self.active = {}
def schedule(self, incident_id, delay, payload):
if delay < 1:
raise ValueError("delay must be positive")
if incident_id in self.active:
old_slot = self.active[incident_id][2]
self.slots[old_slot].remove(incident_id)
due = self.now + delay
slot = due % self.slot_count
self.active[incident_id] = (due, payload, slot)
self.slots[slot].add(incident_id)
def cancel(self, incident_id):
due, payload, slot = self.active.pop(incident_id)
self.slots[slot].remove(incident_id)
return payload
def get(self, incident_id):
return self.active[incident_id][1]
def advance(self, target_tick):
if target_tick < self.now:
raise ValueError("time cannot move backward")
expired = []
while self.now < target_tick:
self.now += 1
slot = self.now % self.slot_count
for incident_id in tuple(self.slots[slot]):
due, payload, _ = self.active[incident_id]
if due <= self.now:
self.slots[slot].remove(incident_id)
del self.active[incident_id]
expired.append((incident_id, payload))
return expired
wheel = IncidentExpiryWheel(8)
wheel.schedule(47, 3, "old-review")
wheel.schedule(19, 11, "valve-review")
wheel.schedule(47, 5, "new-review")
print("tick-3=", wheel.advance(3), " tick-5=", wheel.advance(5),
" tick-11=", wheel.advance(11), sep="")Output
tick-3=[] tick-5=[(47, 'new-review')] tick-11=[(19, 'valve-review')]Time, space, and tradeoff
Scheduling or canceling uses expected O(1) set and map operations. Advancing by T ticks costs O(T + V), where V counts all entries inspected in visited slots; a far-future timer may be inspected again on every revolution. Returning E expired entries also costs O(E) output space. The wheel uses O(S + N) storage for S slots and N active timers. Unlike a min-heap, it does not jump directly to the next due event when many empty ticks pass. A hierarchy or round counters could reduce repeated distant-timer inspections, but that is outside this one-level model. The trace makes no real-time latency or concurrency guarantee.
Common Mistakes
- Do not fire every item in a visited slot without checking its exact due tick.
- Do not leave an old slot membership after rescheduling the same ID.
- Do not move time backward after scheduling relative delays.
- Do not claim this wheel skips empty ticks or never revisits distant timers.
Connected lessons
- Stacks and Queues
- Data Structures
- Expiry heaps: invalidate stale TTL records on replacement
- Ring buffers: make capacity and overwrite rules explicit
- CLOCK caches: revisit pages through reference bits
- Projects
- Quizzes
Compare retention and timing contracts with CLOCK caches: revisit pages through reference bits, Segmented LRU: separate probation from protected reuse, Frequency admission: compare an arrival with an eviction victim, then run the policy audit and contract quiz.
