Skip to content

Parallel and batch

Parallel and batch execution reduces waiting time when work is genuinely independent. Anvia provides two pipeline primitives for two different shapes of work.

Choose the execution shape

RequirementPrimitiveResult
Run several operations from one value.parallel({...})One object keyed by branch name.
Run the same pipeline for many valuespipeline.batch(inputs, options)One ordered array of outputs.
Survive restarts or process a large backlogWorker queueDurable jobs with application-owned status.
text
Parallel                              Batch

one ticket                            ticket A ─┐
   ├─ classify                        ticket B ─┼─ same pipeline
   ├─ detect risk                     ticket C ─┘
   └─ estimate priority
        ↓                                  ↓
one combined result                   ordered results

Explore parallel and batch work

PageLearn how to
Parallel branchesFan one value into independent named operations.
Batch runsProcess many inputs through one pipeline.
Concurrency limitsBound pressure on models, services, and local resources.
Failures and resultsChoose fail-fast or per-item outcomes.
Long-running jobsMove durable work behind a queue and worker.
Retries and idempotencyRetry the smallest safe boundary.
Production checklistReview dependencies, limits, state, and observability.

Use parallel branches

ts
const triage = new PipelineBuilder(ticketSchema)
  .parallel({
    classification: classifyTicket,
    policy: checkPolicy,
    urgency: estimateUrgency,
  })
  .step(({ classification, policy, urgency }) => ({
    category: classification.category,
    allowed: policy.allowed,
    priority: urgency.priority,
  }))
  .build()

Every branch receives the same parsed ticket. The next step receives an object whose keys match the branch names.

Run a batch

ts
const results = await triage.batch(tickets, {
  concurrency: 3,
})

At most three inputs run concurrently, and the returned results preserve the order of tickets.

Parallelism is not durability

Both primitives run inside the current JavaScript process. They do not create durable jobs, persist progress, or survive a process restart.

Use them for bounded in-process work. Use BullMQ, Trigger.dev, or another job system when work must continue independently of an HTTP request or application instance.

Built for Anvia.