Skip to content
pushqdocs
Get started

This pattern enqueues a large number of tasks at once (a nightly digest for every user, a backfill, a fan-out) with the batch endpoint, POST /v1/queues/{queue}/tasks:batch. Each request takes up to 500 tasks, creates every valid one, and reports a result per task, so you chunk big lists and handle the few that fail.

const users = await loadUsers(); // e.g. 12,000 rows
const failed: string[] = [];
for (let start = 0; start < users.length; start += 500) {
const chunk = users.slice(start, start + 500);
const results = await pq.createTasks("emails", chunk.map((u) => ({
taskId: `digest-2026-11-30-user-${u.id}`,
path: "/hooks/send-digest",
jsonBody: { userId: u.id },
})));
for (const r of results) {
// already_exists: created by an earlier run, so it counts as done
if (r.error && r.error.code !== "already_exists") {
failed.push(`user ${chunk[r.index].id}: ${r.error.code} ${r.error.detail}`);
}
}
}

The response lists one result per input task, in input order. Each result has the task’s index and either the created task or an error:

{
"results": [
{ "index": 0, "task": { "id": "digest-2026-11-30-user-42", "state": "pending", "...": "..." } },
{ "index": 1, "error": { "code": "already_exists", "detail": "already exists" } }
]
}

The examples assume a client named pq (see install and configure an SDK). The SDK method is createTasks (create_tasks in Python, CreateTasks in Go, CreateTasksAsync in .NET). Each item takes the same fields as a single task create, with your language’s casing.

Use this when you enqueue more than a handful of tasks at a time: one request per 500 tasks is far faster than 500 separate requests.

Rule Value
Tasks per request 1 to 500. An empty list or more than 500 rejects the whole request with 400 invalid_argument.
Request body size 8 MiB for the whole JSON request. Larger requests get 413 payload_too_large.
Per-task limits The same as a single create: 1 MiB body, 16 KiB headers, one-year schedule-ahead (self-hosted defaults).
Atomicity None. Valid tasks are created even when others in the same request fail.
HTTP status 200 whenever the request itself was accepted, even if every item failed. Check each result.
Queue One queue per request. An unknown queue fails the whole request with 404 not_found.

Task bodies count toward the 8 MiB request limit, and a raw body_base64 grows by a third when base64-encoded. With large bodies, send fewer than 500 tasks per request.

Every item that fails has an error code. Decide per code:

Code Cause What to do
already_exists A task with this task_id exists (or was finished or deleted recently), including a duplicate ID earlier in the same batch. Usually treat as success: it was enqueued before.
invalid_argument The item failed validation: bad task_id, missing url/path, body_base64 with json_body, delay_seconds with schedule_time, body or headers too large. Fix the data. Resending won’t help.
failed_precondition The item uses path but the queue has no target.base_url, or the queue is disabled. Fix the queue, then resend those items.
internal A server-side error for that item. Resend the item.

Set a deterministic task_id on every item. Then retrying is always safe: if a request times out and you don’t know what happened, send the same chunk again, and the tasks that were already created come back as already_exists instead of being enqueued twice. Without IDs, a resend after a timeout can create duplicates. See deduplicate tasks with task IDs.

A batch targets one queue. To enqueue to several, group the tasks by queue and send one batch (or several chunks) per queue:

const byQueue = new Map<string, TaskCreate[]>();
for (const job of jobs) {
const queue = `tenants/${job.tenantId}`;
if (!byQueue.has(queue)) byQueue.set(queue, []);
byQueue.get(queue)!.push({ taskId: job.id, path: "/hooks/sync", jsonBody: job.payload });
}
for (const [queue, tasks] of byQueue) {
for (let start = 0; start < tasks.length; start += 500) {
const results = await pq.createTasks(queue, tasks.slice(start, start + 500));
// handle per-item errors as above
}
}

In TypeScript, TaskCreate is the task input type exported by the pushq package. Queue names containing / are URL-encoded (tenants%2Facme) in curl; the SDKs do it for you.

If some of the queues may not exist yet, create them first (see per-tenant queues): a batch to an unknown queue fails as a whole with 404 not_found.

Why bulk enqueue doesn’t flood your endpoint

Section titled “Why bulk enqueue doesn’t flood your endpoint”

The batch endpoint validates each item, then inserts all valid items in a single database transaction, isolating conflicts per item so one duplicate doesn’t undo the rest. It wakes the dispatcher once per batch. Enqueueing and delivery are separate: 100,000 new tasks don’t mean 100,000 requests at once. The queue’s rate limits (max_dispatches_per_second, max_burst_size, max_concurrent_dispatches) pace delivery, so you don’t need to spread enqueues out with delay_seconds to protect your endpoint. Use delay_seconds or schedule_time only when the work should really happen later.

Sending chunks one after another is usually fast enough. If you send chunks in parallel, keep it to a few at a time.

  • Treating 200 as “all created”. Always walk the results.
  • No task IDs. Without them, resending a chunk after a timeout creates duplicates.
  • Retrying invalid_argument items. They fail the same way every time; log and fix them.
  • Large bodies in a 500-item batch. The 8 MiB request cap is hit long before 500 items with 1 MiB bodies.
  • Mixing queues in one batch. Not possible; group by queue first.