Skip to content

Add opt-in spread for scheduled function enqueues - #13121

Merged
levivannoort merged 2 commits into
mainfrom
feat/schedule-spread
Aug 6, 2026
Merged

levivannoort merged 2 commits into
mainfrom
feat/schedule-spread

Conversation

@levivannoort

@levivannoort levivannoort commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Adds an opt-in _APP_FUNCTIONS_SCHEDULE_SPREAD env var that spreads scheduled function enqueues sharing the same cron slot across a window, instead of enqueuing them all in the same second.

Cron schedules cluster on shared slots — a large share of all scheduled functions run 0 * * * * or similar. ScheduleFunctions::enqueueResources() groups schedules by identical delay and enqueues each bucket in one tight loop, so every same-slot execution hits the v1-functions queue at exactly :00:00. At scale this is a thundering herd: a burst of simultaneous invocations and runtime cold starts at the top of every hour.

With _APP_FUNCTIONS_SCHEDULE_SPREAD=N, each function gets a deterministic offset — abs(crc32(functionId)) % N seconds — added to its enqueue delay:

  • Stable slot per function: an hourly cron always fires at e.g. :00:37, so run intervals stay exact.
  • Selection is untouched: the offset applies to the coroutine sleep only, so no occurrence is double-enqueued or skipped (the 60s selection windows tile exactly with the enqueue loop).
  • Telemetry stays honest: enqueue-delay is recorded against the intended (offset) time, so the deliberate spread doesn't read as scheduler lateness while a genuine stall still shows.
  • Default 0 = disabled: behavior is byte-for-byte unchanged unless the env var is set.

Also registers the variable in app/config/variables.php and forwards it to the appwrite-task-scheduler-functions service in docker-compose.yml.

Test plan

  • New unit tests for spreadOffset(): determinism, bounds ([0, window)), disabled windows (<= 1 → 0), and dispersion across distinct function IDs. Verified red with the offset neutered to a constant, green with the real implementation.
  • The enqueue-loop wiring itself has no unit-testable seam (coroutine timing); verified by watching scheduler logs with the spread enabled — enqueues land at distinct offsets within the window instead of one second.
  • Pint, Rector, and PHPStan (level 4) pass.

🤖 Generated with Claude Code

Cron schedules cluster on shared slots (everyone runs `0 * * * *`), so
the scheduler enqueued every same-slot execution in the same second — a
thundering herd of simultaneous invocations and runtime cold starts at
the top of the hour.

When _APP_FUNCTIONS_SCHEDULE_SPREAD is set to N seconds, each function
gets a deterministic offset (abs(crc32(functionId)) % N) added to its
enqueue delay. Functions keep a stable slot so run intervals stay exact,
and enqueue-delay telemetry measures against the intended (spread) time.
Default is 0: behavior is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
@greptile-apps

greptile-apps Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds an opt-in environment setting that deterministically distributes scheduled function enqueues across a configured time window.

  • Registers and forwards _APP_FUNCTIONS_SCHEDULE_SPREAD to the function scheduler service.
  • Applies stable per-function offsets to enqueue timing and telemetry.
  • Adds unit coverage for deterministic, bounded, disabled, and dispersed offsets.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/Appwrite/Platform/Tasks/ScheduleFunctions.php Adds deterministic per-function enqueue offsets and adjusts the expected telemetry timestamp to the spread enqueue time.
app/config/variables.php Registers the opt-in schedule-spread variable with a disabled default.
docker-compose.yml Forwards the schedule-spread variable to the function scheduler service.
tests/unit/Platform/Tasks/ScheduleFunctionsTest.php Covers offset determinism, bounds, disabled windows, and dispersion.

Reviews (2): Last reviewed commit: "Merge branch 'main' into feat/schedule-s..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Aug 6, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → feat/schedule-spread (after).

Metric Before After Change
🚀 Requests/sec 230.25 229.67 ⚪ -0.3%
⏱️ Latency P50 61.75 ms 61.94 ms ⚪ +0.3%
⏱️ Latency P95 215.62 ms 223.44 ms ⚪ +3.6%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 61.94 223.44 14,307 229.67 +7.81
Account 121.65 305.13 753 12.48 +0.99
TablesDB 59.35 206.86 7,781 126.58 +11.39
Storage 54.82 206.95 3,765 63.24 +8.22
Functions 95.68 273.39 2,008 34.22 -13.5

Top API waits (after)

API request Max wait (ms)
functions.variables.create 787.69
tablesdb.rows.create 762.52
tokens.update 703.23
account.name.update 644.82
functions.delete 629.11

@levivannoort
levivannoort merged commit fd293a1 into main Aug 6, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants