# Batch Worker # Run self-recovering queue workers in Convex by providing a work query and worker mutation, with debouncing, polling, and automatic restart built in. # Category: Durable Functions # Agents: fetch the skill at https://www.convex.dev/components/batch-worker/SKILL.md before writing code that uses Batch Worker. It contains setup steps, configuration, and usage patterns. ## Install - Command: npm install @convex-dev/batch-worker ## Links - Directory: https://www.convex.dev/components/batch-worker - Markdown: https://www.convex.dev/components/batch-worker/batch-worker.md - llms.txt: https://www.convex.dev/components/batch-worker/llms.txt - Skill: https://www.convex.dev/components/batch-worker/SKILL.md - npm: https://www.npmjs.com/package/%40convex-dev%2Fbatch-worker - GitHub: https://github.com/convex/batch-worker - Demo: https://capable-fish-350.convex.site/ ## Details - Version: 0.3.7 - Weekly downloads: 1,859,632 - Author: convex - Tags: batch, worker, daemon, singleton, queue ## Description Batch Worker provides a durable, self-recovering loop for processing queued work stored in your own Convex tables. You supply a work query that returns the next batch and a worker mutation that processes it, and the component handles scheduling, debouncing, single-loop enforcement, snapshot reads to avoid OCC conflicts, and automatic restart on failure. It is the underlying pattern behind components like Workpool, exposed so you can build custom queue processors with full control over your data model. ## Use Cases - Draining a queue of pending events or tasks inserted by concurrent mutations, without hitting OCC retry limits on the queue table - Updating denormalized aggregates from a write-ahead log of changes, decoupling high-concurrency writers from a single aggregate document - Batching outbound API calls (LLM requests, webhooks) by having the mutation claim rows and schedule an action, then committing results back - Rate-limiting external API usage by integrating with the Rate Limiter component and returning debounceMs or timeoutMs to throttle the loop - Building custom queue-processing components that need independent named queues with separate work query and worker mutation pairs ## How It Works After installing the component via `app.use(batchWorker)` in `convex/convex.config.ts`, you call `ping(ctx, components.batchWorker, { name, workQuery, workerMutation })` after inserting work. The component ensures exactly one processing loop runs per named queue. The loop calls your `internalQuery` (typed with `vBatchQueryArgs` and `vBatchResult`) to fetch the next batch using a snapshot read with no read dependencies, then calls your `internalMutation` to process it in a normal transaction. The snapshot read in the work query avoids OCC conflicts from concurrent inserts, since range reads there do not create read dependencies. The mutation receives the batch data directly through its arguments, so it only takes dependencies on the specific document IDs it updates, not on the queue range. Your mutation is responsible for deleting or advancing past processed rows so the query does not return them again. The loop supports debouncing via `config.debounceMs` on ping (to accumulate bursts before the first batch) and via returning `{ debounceMs }` from the worker mutation (to throttle while running). When the queue is empty, your query returns `{ kind: 'idle' }` and the loop parks. An optional `timeoutMs` field wakes the loop after a fixed interval even without a ping. The component monitors the loop and automatically restarts it if it dies due to an unexpected error, logging the failure for alerting. For batching work that requires actions (external API calls, LLM requests), the recommended pattern is for the worker mutation to claim rows by marking them in-progress, then schedule the action or enqueue it in a Workpool. The action performs the external call and writes results back via a mutation. This keeps the action separate from the transactional mutation while still benefiting from the batch loop.