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

Key-value store with rolling QPS

medium 3 levels ~60 min Databricks

Level 1 Store plus five-minute QPS

Build KVStore(clock). clock is a function with no arguments that returns the current time as a whole number of seconds. Time never goes backwards, but many calls can happen in the same second. Always read the time from clock(); tests control it.

Store operations:

  • put(key, value) stores or overwrites the value. It counts as a write.
  • get(key) returns the value, or None if the key is missing. It counts as a read, even on a miss.
  • delete(key) -> bool removes the key and returns whether it existed. It counts as a write.

Load operations (these don't count as requests):

  • write_qps() -> float and read_qps() -> float return the average requests per second of that kind over the last five minutes.

The five-minute window covers the 300 whole seconds now - 299 .. now. A store that is younger than that must not be averaged over 300 seconds. Let start be the second of the store's first request of either kind. The window is then min(300, now - start + 1) seconds long, and the average is the number of requests of that kind in those seconds divided by that length. If there has been no request yet, return 0.0.

The time comes from clock() when you ask, so a store that goes idle sees its QPS fall to zero as old requests age out.

t = [100]
kv = KVStore(lambda: t[0])
kv.put("a", 1); kv.put("b", 2); kv.get("a")     # all at second 100
kv.write_qps()    # 2.0   window is 1 second long (100..100)
t[0] = 103
kv.get("zz")      # None, still a read
kv.read_qps()     # 0.5   2 reads over 4 seconds (100..103)
t[0] = 399
kv.write_qps()    # 2/300: the window is now 100..399
t[0] = 400
kv.write_qps()    # 0.0   second 100 has aged out

Expect hundreds of thousands of requests, with QPS asked for after almost every one. Don't rescan the full history on each call.

Level 2 unlocks when level 1 passes.

Level 3 unlocks when level 2 passes.

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

0:00
Ctrl ' run · Ctrl ↵ submit
esc