~/problems / Streams & durability / Rolling windows and rate counters

Logger Rate Limiter

easy ~12 min

A monitoring screen gets flooded when a broken service fires the same alert over and over. Build a filter that shows an alert, then hides any repeat of that exact text for the next 10 seconds.

Build AlertMuter() with one method:

  • should_show(t: int, message: str) -> bool: an alert with text message arrives at integer second t. Return True (and show it) if this text has never been shown, or was last shown at a time s with s + 10 <= t. Otherwise return False.

Hidden alerts don't restart the 10 seconds: only shown ones do. Across all calls t never decreases, and several alerts can arrive in the same second.

m = AlertMuter()
m.should_show(1, "disk full")     # True
m.should_show(2, "cpu hot")       # True
m.should_show(3, "disk full")     # False  (shown at 1)
m.should_show(8, "cpu hot")       # False
m.should_show(11, "disk full")    # True   (1 + 10 <= 11)
m.should_show(11, "disk full")    # False  (just shown at 11)
m.should_show(12, "cpu hot")      # True

Up to 300,000 calls with many different messages. Each call should take O(1) time on average (plus the length of the message); don't scan the history.

Show hint

For each message you only need one number: when it was last shown.

Topic: Rolling windows and rate counters. QPS over the last N seconds, hit counters, rate limiters.

0:00
Ctrl ' run · Ctrl ↵ submit
esc