Skip to content

feat(queue): bound the Redis recovery sweep by attempts and age - #14689

Merged
levivannoort merged 1 commit into
mainfrom
fix/queue-redis-reap-limits
Oct 20, 2026
Merged

levivannoort merged 1 commit into
mainfrom
fix/queue-redis-reap-limits

Conversation

@levivannoort

Copy link
Copy Markdown
Member

What does this PR do?

Adds two optional limits to the Redis broker's automatic recovery sweep: reapMaxAttempts and reapMaxAge.

maintain() calls reap() every 60s and requeues up to 1,000 stranded claims older than reapAfter. It passes no attempt limit and no age limit, and the constructor exposes only reapAfter, so a consumer can't bound the sweep. reap() already supports both limits (maxAttempts, newerThan) and parks matching claims on the dead queue. The sweep just never passed them.

That broke Appwrite Edge's upgrade to queue 6 (appwrite-labs/edge#1525, reverted in appwrite-labs/edge#1564):

  • Queue 1.6 never cleared stale entries from the processing list, and their payloads have no TTL. In one region, utopia-queue.processing.v1-regions held about 9,060 such entries going back several months.
  • On the first sweep after the upgrade, all of them were replayed. Cross-region invalidations from months earlier ran again, and worker throughput went from 10–20 msg/s to 60–70 msg/s until the rollback.
  • Old cdn_purge jobs used a payload shape the current code no longer accepts, so they failed on decode and landed on failed.

Change

  • Broker\Redis::__construct gets ?int $reapMaxAttempts = null and ?int $reapMaxAge = null (seconds).
  • maintain() passes them to reap() as maxAttempts and newerThan.
  • Both default to null, which keeps the current behaviour.
  • The constructor refuses reapMaxAttempts < 1, and a reapMaxAge at or below reapAfter. Every claim the sweep reaches is already at least reapAfter old, so a ceiling that low would park every claim and recover none.

Test Plan

  • tests/E2E/RedisBrokerRecoveryTest.php, against the package's compose Redis:
    • testMaintainParksClaimsOlderThanReapMaxAge: a claim backdated an hour, with reapMaxAge: 600, goes to dead, not back to the queue.
    • testMaintainParksClaimsPastReapMaxAttempts: with reapMaxAttempts: 1, the first stranding is requeued (attempts 0 → 1) and the second is parked on dead.
    • With the maintain() wiring reverted, both fail (Failed asserting that 0 is identical to 1, on the dead-queue size). With it, all 24 tests in the class pass.
  • tests/RedisReapLimitsTest.php (unit): covers the two constructor checks, and that valid limits are accepted.
  • rector --dry-run, pint --test and phpstan are clean for packages/queue.
  • Locally, the unit suite has 52 failures on a host without Swoole. The same 52 fail on origin/main without this change.

Related PRs and Issues

  • appwrite-labs/edge#1525: the queue 6 upgrade this blocked
  • appwrite-labs/edge#1564: its revert, which names this limit as the follow-up

Once this is released, edge will set reapMaxAttempts: 3, reapMaxAge: 172800 and re-apply the upgrade.

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? (N/A: no API changes)

@luxor-code-review

luxor-code-review Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — reapMaxAge measures last requeue, not original publish · Redis.php:508–510 · unchanged line

// packages/queue/src/Broker/Redis.php
508	            $dead = ($maxAttempts !== null && $job->getAttempts() >= $maxAttempts)
509	                || ($newerThan !== null && $job->getTimestamp() < $now - $newerThan);
510	            $moved = $this->script($this->commands, 'reclaim', [

reap() parks a claim when its timestamp is older than reapMaxAge (packages/queue/src/Broker/Redis.php:508-510), but retryPayload() resets timestamp to time() on every requeue (packages/queue/src/Broker/Redis.php:539-547), so after the first recovery the age is measured from the last requeue. A poison message requeued once per sweep never crosses the age ceiling again and is only stopped by reapMaxAttempts; the documented "published longer ago than this many seconds" guarantee does not hold for recovered claims, and the original publish time is unrecoverable by any caller.


Adds poison-claim handling to the Redis queue broker: maintain() sweeps now count recovery attempts per claim and park claims on a dead queue once they exceed reapMaxAttempts or exceed reapMaxAge, replacing the previous unconditional requeue behavior. Tests cover both the recovery flow and the new reap limits.

Files Change
packages/queue/src/Broker/Redis.php Adds reap limit plumbing (constructor options, dead-queue parking) to the reap()/maintain() sweep logic.
packages/queue/tests/RedisReapLimitsTest.php Unit-style tests pinning the reap limit semantics (attempt counting, age threshold, parking behavior).
packages/queue/tests/E2E/RedisBrokerRecoveryTest.php E2E test exercising claim recovery with the new limits in a real Redis instance.

Reviewed commit: 36b6582

@ghost

ghost commented Oct 19, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Adds optional attempt and age limits to Redis broker recovery and passes them from maintain() into the existing reap logic. Adds constructor validation and recovery tests for claims parked on the dead queue by either limit.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
packages/queue/src/Broker/Redis.php Adds recovery limit options, validates them, and wires them into maintenance sweeps.
packages/queue/tests/E2E/RedisBrokerRecoveryTest.php Covers automatic recovery parking claims that exceed the age or attempt limit.
packages/queue/tests/RedisReapLimitsTest.php Covers constructor validation and acceptance of valid recovery limits.

Reviewed 59d8ce8 · 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 S · Looks good to merge. Summary

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • 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 19, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/queue-redis-reap-limits (after).

Metric Before After Change
🚀 Requests/sec 192.02 202.37 🟢 +5.4%
⏱️ Latency P50 89.31 ms 86.01 ms ⚪ -3.7%
⏱️ Latency P95 215.37 ms 201.18 ms 🟢 -6.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 86.01 201.18 12,540 202.37 -14.19
Account 156.16 296.57 660 11.36 -18.46
TablesDB 81.28 147.36 6,820 112.37 -29.89
Storage 85.18 198.48 3,300 55.76 +1.05
Functions 120.52 245.06 1,760 30.51 -7.12

Top API waits (after)

API request Max wait (ms)
account.name.update 437.78
storage.buckets.create 403.12
account.prefs.update 391.75
functions.variables.update 358.94
storage.files.update 350.32

maintain() reaped every stranded claim older than reapAfter with no attempt
or age limit, and the constructor exposed only reapAfter. A processing list
left behind by a release without the sweep was replayed in full on upgrade:
months-old jobs ran again, and ones whose payload shape had since changed
failed on decode.

reapMaxAttempts and reapMaxAge pass through to reap() as maxAttempts and
newerThan, so the sweep parks those claims on the dead queue instead. Both
default to null, which keeps the current behaviour. reapMaxAge must exceed
reapAfter, because every claim the sweep reaches is at least that old and a
lower ceiling would park all of them.
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.

1 participant