Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0059

BullMQ on a metered Redis: quiet when idle, paused when Redis is unusable

Trạng thái
accepted
Ngày
2026-11-24
Người quyết định
SmartLearn team (production incident, 2026-11-21/22)

Context

Production Redis is Upstash's free plan: 500 000 commands a month, every command counted, blocking ones included. On 2026-11-21/22 it answered ERR max requests limit exceeded. From then on every BullMQ worker hit the error in waitForJob, waited BullMQ's fixed 100 ms and tried again: about ten failing commands and ten error lines a second per worker. The log and Redis traffic reached ~1.3 GB an hour, Render's 5 GB free bandwidth ran out and the api was suspended.

Two causes. Idle traffic: BullMQ's defaults poll an empty queue every 5 s (drainDelay: one BZPOPMIN, then one moveToActive script) and sweep stalled jobs every 30 s. A job scheduler keeps a delayed job in its queue, and while one exists BullMQ caps the wait at 10 s: the payments worker polled every 10 s forever. The api's unused QueueEvents on grading ran a blocking XREAD every 10 s. No backoff: nothing stopped a worker when Redis kept failing.

Decision

  • One set of worker options for every consumer, QUEUE_WORKER_OPTIONS in @smartlearn/shared: drainDelay: 300 (seconds), stalledInterval: 300_000, maxStalledCount: 2. Latency is unchanged: adding a job pushes a marker that wakes the blocked worker at once, and a delayed job or retry still caps the wait at 10 s. A crashed job is re-queued within 5 min instead of 30 s; two stalls (two redeploys) before it fails.
  • One guard on every worker, guardQueueWorker() in @smartlearn/shared. It is the worker's error listener: the first error is logged, then at most one line a minute with the count it swallowed. On the quota error (/max requests limit exceeded/i), or 20 errors within 60 s, it calls worker.pause(true) (no commands at all, stalled checker included) and resumes after 30 min; still broken, it pauses again on the first error.
  • Reconnects back off to once a minute (redisReconnectDelay: 2 s, 4 s … 60 s) on the worker's BullMQ and budget connections and on the api's BullMQ and budget connections. The api's cache client already gives up after three tries.
  • The purchase-expiry sweep is an in-process timer in the worker (every 5 min, once at boot), not a BullMQ scheduler; the payments consumer is gone. The sweep is one idempotent UPDATE (ADR-0045), so two processes running it is harmless. Each boot removes the old scheduler and its delayed job from Redis.
  • The api's QueueEvents on grading is removed. Nothing listened to it.

Estimate (idle, container up all month, 2 592 000 s)

| Source | Before | After | | ------------------------------------------ | ------------- | ----------- | | ingest, quiz, grading, course-refund polls | 4 × 1 036 800 | 4 × 17 280 | | stalled sweeps (5 workers before, 4 after) | 5 × 86 400 | 4 × 8 640 | | payments poll at the 10 s cap | 518 400 | 0 | | payments ticks (8 640 jobs × ~10 commands) | ~86 400 | 0 | | QueueEvents XREAD every 10 s | 259 200 | 0 | | Total | ≈ 5.47 M | ≈ 104 k |

Before, idle traffic at full uptime was ~11× the quota; real use was lower while the free instance slept. ADR-0045's scheduler, added on 2026-11-21, brought the 10 s payments poll. After, idle traffic is ~21 % of the quota, leaving ~400 k commands a month for real jobs, the AI cache and the token budget. Upstash may count a script as more than one command; the order of magnitude holds either way.

Consequences

  • A Redis outage now costs at most a few commands per worker every 30 min and one log line a minute; jobs wait (they are durable in Redis) and the api keeps its in-process fallbacks.
  • A worker paused by the guard picks up work up to 30 min after Redis recovers.
  • Every new new Worker( must spread QUEUE_WORKER_OPTIONS and call guardQueueWorker; every new ioredis client needs a backing-off retryStrategy. No new QueueEvents or job scheduler without counting its commands against the quota first.