Context
Parsing a PDF, embedding its chunks and drafting quiz questions take seconds to minutes and call paid APIs. AGENTS.md rule 5 says none of that may run on a request path: a student taking a quiz must never wait on a model. The work needs a queue with retries, and it must survive an API restart.
Decision
- BullMQ on Redis 7, consumed by a separate process,
apps/worker. The API only enqueues. - Queue names are constants in
packages/shared(QUEUES:ingest,embed,quiz). - Payloads are Zod schemas in
packages/shared/src/schemas/jobs.ts(ingestJobDataSchema→IngestJobData).jobDataSchemasis keyed byQueueName, so adding a queue without a payload schema fails to compile. - The worker parses
job.databefore any work. It is untrusted JSON from Redis. A payload that fails the schema would fail every retry too, so the worker throwsUnrecoverableErrorand BullMQ does not retry it. - Processors take plain data, not a BullMQ
Job, so they are unit-tested without Redis. - One Redis connection per worker process, with
maxRetriesPerRequest: nullas BullMQ requires. Workers logfailed,errorandstalledevents; an unhandlederrorevent would crash the process. - On
SIGTERMthe worker closes everyWorker(in-flight jobs finish) and then exits. Compose gives itstop_grace_period: 30s.
Alternatives considered
- Postgres-backed queue (pg-boss). One fewer service, but Redis is needed for caching anyway, and BullMQ has better tooling and documentation.
- Running jobs inside the API process. One process fewer, but an ingest spike would slow every request, and a deploy would drop in-flight jobs.
Consequences
- The producer and the consumer share one contract and cannot drift silently.
- The API will need its own BullMQ connection for producers. Its
RedisServiceconnection has a 2-second ping timeout for the readiness probe, which BullMQ must not inherit. - Two services to run locally.
pnpm devstarts both.