feat(schedule): spread monitor runs evenly over their interval #174

Merged
Sebi merged 1 commits from feat/schedule-phase into main 2026-11-29 15:08:04 +00:00
Owner

Why

In prod, uptimesebi_checks_overdue rises and falls in triangle waves of about 10 minutes, peaking at 26K per region. The checkers keep up on average; the runs just arrive bunched:

  • A monitor's next run was at + interval, so monitors created together (import, load test, a new region, reconcile after a Redis loss) stay due in the same second for good.
  • Late runs were rescheduled to now + interval, gathering them at the dispatch time.

What

  • Every monitor has a fixed phase within its interval: uint64(id[8:16]) mod intervalMS, stored in the new hash sched:phases.
  • The first run stays immediate, as before.
  • Every later run lands on the monitor's next slot after now. A run off its slot, like the first one, skips a slot less than half an interval away, so a monitor never runs twice in quick succession.
  • Late runs snap to their own slot instead of bunching.
  • Reconcile adds lost monitors on their slot, not on now, so rebuilding a schedule causes no burst. Only Upsert from the API runs right away.
  • Remove and the prune script also drop the phase.
  • Monitors without a phase (written before this release) keep the old rule until the next reconcile, within a minute.

Existing clusters break up on their own within one interval of each monitor after rollout.

Tests

  • New: TestPhaseSpreadsBurst (600 monitors at once: all first runs in second 0, then at most 30 per second at a mean of 10), TestLateRunSnapsToPhase, TestReconcileAddsOnPhase, TestPhaseFallback, TestNextSlot.
  • Adapted TestDispatchReschedulesAndKeepsRegionsApart: the next run is on the phase, between half and one and a half intervals later.
  • go test ./... with DB and Redis, and golangci-lint: green.
  • Dev stack: 200 monitors at 60 s created at once. After the first burst, dispatch per second stays flat (about 3.4/s, max 9) instead of 200 every minute.

🤖 Generated with Claude Code

https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3

## Why In prod, `uptimesebi_checks_overdue` rises and falls in triangle waves of about 10 minutes, peaking at 26K per region. The checkers keep up on average; the runs just arrive bunched: - A monitor's next run was `at + interval`, so monitors created together (import, load test, a new region, reconcile after a Redis loss) stay due in the same second for good. - Late runs were rescheduled to `now + interval`, gathering them at the dispatch time. ## What - Every monitor has a fixed **phase** within its interval: `uint64(id[8:16]) mod intervalMS`, stored in the new hash `sched:phases`. - The **first run stays immediate**, as before. - Every later run lands on the monitor's next slot after `now`. A run off its slot, like the first one, skips a slot less than half an interval away, so a monitor never runs twice in quick succession. - Late runs snap to their own slot instead of bunching. - `Reconcile` adds lost monitors on their slot, not on `now`, so rebuilding a schedule causes no burst. Only `Upsert` from the API runs right away. - `Remove` and the prune script also drop the phase. - Monitors without a phase (written before this release) keep the old rule until the next reconcile, within a minute. Existing clusters break up on their own within one interval of each monitor after rollout. ## Tests - New: `TestPhaseSpreadsBurst` (600 monitors at once: all first runs in second 0, then at most 30 per second at a mean of 10), `TestLateRunSnapsToPhase`, `TestReconcileAddsOnPhase`, `TestPhaseFallback`, `TestNextSlot`. - Adapted `TestDispatchReschedulesAndKeepsRegionsApart`: the next run is on the phase, between half and one and a half intervals later. - `go test ./...` with DB and Redis, and golangci-lint: green. - Dev stack: 200 monitors at 60 s created at once. After the first burst, dispatch per second stays flat (about 3.4/s, max 9) instead of 200 every minute. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Sebi added 1 commit 2026-11-29 15:06:51 +00:00
feat(schedule): spread monitor runs evenly over their interval
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m40s
ci / test (pull_request) Failing after 13m52s
ci / build (pull_request) Skipped
ci / image (pull_request) Skipped
050c9dd0a1
A monitor's next run was its last due time plus the interval, so monitors
created together, by an import or a test, stayed due in the same second
for good, and late runs all gathered at the dispatch time. In prod, regions
fell behind in waves every interval although the checkers kept up on
average.

Each monitor now has a fixed phase within its interval, from its ID.
After the first run, still right away, it runs on its phase; late runs
snap to their next slot. Reconcile adds lost monitors on their phase, so
rebuilding a schedule causes no burst either. Monitors scheduled before
phases existed keep the old rule until reconciliation adds their phase.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Sebi merged commit 9d1d6a2544 into main 2026-11-29 15:08:04 +00:00
Sebi deleted branch feat/schedule-phase 2026-11-29 15:08:07 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ZB-Server/uptimesebi#174