Skip to content

fix(usage): cap the usage queue so a backlog cannot evict the Redis cache - #14193

Closed
HarshMN2345 wants to merge 4 commits into
mainfrom
cursor/cap-usage-queue-redis-0de9
Closed

HarshMN2345 wants to merge 4 commits into
mainfrom
cursor/cap-usage-queue-redis-0de9

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Stops a v1-stats-usage backlog from evicting the document cache. Cache, locks, abuse, pub/sub, and every queue share one Redis. Official Compose runs that instance with --maxmemory 512mb --maxmemory-policy allkeys-lru. Each API request enqueues one usage job of about 2KB on utopia-queue.queue.v1-stats-usage. Under write load the list grows faster than worker-stats-usage drains it (about 1,000 jobs/s). In the report, 150,396 jobs occupied 391MB of the 512MB limit and Redis had evicted 7.2M keys. allkeys-lru can also evict queue lists, which drops every job in them.

This is maxmemory eviction on a shared instance, not a key collision. Queue keys (utopia-queue.queue.*) and cache hashes do not overlap. Another Redis logical database would not help: maxmemory and eviction are instance-wide.

Usage::enqueue() refuses another job once the pending list reaches _APP_STATS_USAGE_QUEUE_MAX (default 32768, about 64MB). That is well under the roughly 240k jobs that fill the 512MB instance, and it still absorbs a short worker stall instead of dropping metrics after a few seconds. Further metrics are dropped, with a warning at most once a minute per process that includes the current depth and the limit. Publishing resumes as soon as the worker drains the list below the cap. A Redis error while reading depth is logged and does not escape the request. Usage writes are already best-effort.

Set the variable to -1 to leave the list uncapped. Any other value that is not a positive integer, including 0, keeps the default. The variable is documented in app/config/variables.php and forwarded, with no inline value, on every process that calls Usage::enqueue(): the API, realtime, MQTT, the combined worker, the scheduler, and the separate webhooks, deletes, builds, jobs, functions, and messaging workers. Services that extend the combined worker or scheduler inherit it. Containers that do not receive it still apply the code default.

Fixes #14084

Test Plan

  • vendor/bin/phpunit tests/unit/Event/Publisher/UsageTest.php (14 tests): explicit and default caps, -1 uncaps, other negatives / 0 / non-numeric values keep the default, publishing resumes after the queue drains, a depth-read failure and a publish failure are swallowed, and disabled usage stats never touch the queue.
  • vendor/bin/pint --test and PHPStan on the touched PHP files.
  • tests/unit/Docker/Compose/GeneratorTest.php still parses docker-compose.yml.
  • npx prettier --check docker-compose.yml.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?
Open in Web Open in Cursor 

…ache

The usage list and the document cache share one Redis. Official Compose
runs that instance at 512mb with allkeys-lru, so a v1-stats-usage backlog
fills it and evicts cache keys. Logical databases do not isolate
maxmemory, so the publisher stops enqueueing once the pending list
reaches _APP_STATS_USAGE_QUEUE_MAX (default 8192).

Fixes #14084
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

32 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (17)
  • php.appwrite.permissive-write-permission (8)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → cursor/cap-usage-queue-redis-0de9 (after).

Metric Before After Change
🚀 Requests/sec 178.42 185.27 ⚪ +3.8%
⏱️ Latency P50 95.83 ms 93.14 ms ⚪ -2.8%
⏱️ Latency P95 234.17 ms 219.66 ms 🟢 -6.2%
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 93.14 219.66 11,514 185.27 -14.5
Account 182.77 348.91 606 10.06 -27.44
TablesDB 89.23 172.52 6,262 102.11 -11.34
Storage 86.91 184.51 3,030 51.26 -7.59
Functions 133.88 269.15 1,616 28.12 -1.8

Top API waits (after)

API request Max wait (ms)
functions.create 513.27
functions.variables.create 490.97
account.name.update 490.91
tablesdb.rows.decrement 450.54
account.prefs.update 445.45

8192 jobs is only a few seconds at the stats-usage drain rate, so a brief
worker stall discarded metrics that still fit in Redis. 32768 jobs is about
64MB at the measured ~2KB payload, under the ~240k jobs that fill the 512MB
instance. Only -1 removes the cap. Depth is checked again after the queue
drains, and a Redis error on that read is logged and does not escape.
Rector's PHPUnit set rewrites count() comparisons on the published-event
list. The Checks job runs that dry-run over tests/.

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost

ghost commented Oct 7, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The previously reported configuration gap is fixed, and no additional defects were identified in the reviewed changes.

Adds a configurable pending-job cap to usage publishing, defaulting to 32,768, with rate-limited warnings when metrics are dropped and best-effort handling of depth-read failures. Documents and forwards the setting through Compose and adds unit coverage for limits, recovery, failures, and disabled statistics.

Verdict New comments Fixed Still open
✅ Approved 0 1 0
📂 Walkthrough · 5
File Change
.env Sets the default usage queue cap to 32,768 pending jobs.
app/config/variables.php Documents the queue cap and the -1 opt-out.
docker-compose.yml Forwards the cap to the API, realtime, MQTT, scheduler, and usage-producing workers.
src/Appwrite/Event/Publisher/Usage.php Checks queue depth before publishing and drops metrics at capacity with throttled warnings.
tests/unit/Event/Publisher/UsageTest.php Covers configuration parsing, capacity, drain recovery, failure handling, and disabled usage.
✅ Fixed since the last review · 1
  • Forward the limit to the remaining usage producers · docker-compose.yml:476

Reviewed 10a1278 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment thread docker-compose.yml Resolved
Separate workers and MQTT call Usage::enqueue() with their own
environment lists, so an operator's _APP_STATS_USAGE_QUEUE_MAX never
reached them. Pass the variable through on MQTT and the webhooks,
deletes, builds, jobs, functions, and messaging workers.

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@HarshMN2345

Copy link
Copy Markdown
Member Author

Superseded by #14112, which fixes the root cause without dropping usage jobs.

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.

🐛 Bug Report: Usage queue backlog evicts the cache from Redis

1 participant