appwrite icon indicating copy to clipboard operation
appwrite copied to clipboard

🐛 Bug Report: Failed queue jobs never expire and there is no way to purge them

Open JoshiJoshiJoshi opened this issue 2 weeks ago • 0 comments

In short: A rejected job keeps its payload in Redis with no TTL, and nothing ever removes failed jobs. queue-retry is the only tool, and it brings back jobs of any age.

Impact:

  • Every outage leaves its failed jobs in Redis for good: 133 422 v1-stats-usage jobs from one three-week window on our instance, plus 6 318 in v1-jobs and 2 340 in v1-executions.
  • With allkeys-lru they take memory from the cache and the live queues (see #14084).

👟 Reproduction steps

  1. Self-hosted Appwrite 2.7.2. Make any queue reject jobs, e.g. stop ClickHouse for a minute while usage is written, or deploy sites on several nodes (lock contention in v1-jobs, separate issue).

  2. Check the failed list and a payload:

    docker exec appwrite-redis redis-cli llen utopia-queue.failed.v1-stats-usage
    pid=$(docker exec appwrite-redis redis-cli lindex utopia-queue.failed.v1-stats-usage -1)
    docker exec appwrite-redis redis-cli ttl utopia-queue.jobs.v1-stats-usage.$pid
    

👍 Expected behavior

Failed jobs are kept for a bounded time for inspection and queue-retry, then removed, or there is a task to purge failed jobs older than a given age.

👎 Actual Behavior

The failed lists only grow; every payload has TTL -1. Our oldest entries are four weeks old.

Cause: on reject, settle.lua pushes the pid onto utopia-queue.failed.<queue>, keeps the payload in utopia-queue.jobs.<queue>.<pid> and only sets EXPIRE when the queue's jobTtl is greater than 0 (settle.lua L28–L29). The worker server builds its queues with new Queue($queueName, $namespace) (Server.php L525), so jobTtl is 0 (Queue.php L12). maintain()/reap() only handle stuck claims, and the maintenance task does not touch queues.

The CLI does not help either:

  • queue-retry --name=<queue> requeues every failed job regardless of age. For v1-jobs that appends old log lines to long finished deployments, for v1-stats-usage it books weeks old usage again.
  • bin/queue-count-failed calls a queue-count task that is not registered in Services/Tasks.php.
  • GET /v1/health/queue/failed/:name does not know v1-jobs and v1-executions (#14632, closed as not planned).

Why it matters: an operator cannot see, bound or clean up failed jobs without writing Redis scripts, and the only safe way to delete them is pid by pid (LREM plus DEL of the payload), because the payloads of running jobs share the same key prefix.

Related: #14084 (Redis eviction), #14491 (payloads left by queue-retry, fixed in 2.7.2).

🎲 Appwrite version

2.7.3 (measured on 2.7.2, code unchanged in 2.7.3)

💻 Operating system

Linux (Ubuntu 26.04), Docker

🧱 Your Environment

Self-hosted, three API nodes and one database node, Redis 7.4 with allkeys-lru shared by cache and queues.

💡 Suggested fix

  • Give worker queues a retention for failed jobs (e.g. jobTtl of 7 days for the payload, and trim the failed list in maintain()), configurable by an environment variable.
  • Add queue-purge --name=<queue> --older-than=<days> next to queue-retry, and an age filter for queue-retry (the broker's retry() already supports newerThan).
  • Fix or remove bin/queue-count-failed.

👀 Have you spent some time to check if this issue has been raised before?

  • [x] I checked and didn't find a similar issue

🏢 Have you read the Code of Conduct?

  • [x] I have read the Code of Conduct

JoshiJoshiJoshi avatar Nov 17 '26 19:11 JoshiJoshiJoshi

Hi! Can you assign this issue to me? I'd like to work on adding the jobTtl retention fix and the queue cleanup commands.

Harshit-2208 avatar Nov 17 '26 20:11 Harshit-2208