Skip to content
pushqdocs
Get started

Task deduplication stops the same piece of work from being enqueued twice, for example when your code retries a create call after a timeout, or two requests race to schedule the same job. In pushq you deduplicate by setting your own task_id: a second create with an ID that is still reserved in that queue is rejected with 409 already_exists, and you treat that as success.

import { PushqError } from "pushq";
try {
await pq.createTask("emails", {
taskId: "welcome-user-42",
path: "/hooks/send-welcome",
jsonBody: { user_id: 42 },
});
} catch (err) {
if (!(err instanceof PushqError && err.code === "already_exists")) throw err;
// already enqueued: nothing to do
}

Use this when an event can reach your enqueue code more than once (a user double-clicks, an upstream webhook is redelivered, your job runner retries) and the task should still run once.

The duplicate create returns this problem response; the task that already exists is not changed:

{
"type": "about:blank",
"title": "Conflict",
"status": 409,
"code": "already_exists",
"detail": "already exists",
"instance": "/v1/queues/emails/tasks"
}

A task ID is unique within its queue. pushq keeps the ID reserved for as long as the task’s record exists, whatever its state, so a create with the same ID fails while the task is pending, running, finished, or recently deleted.

Task state ID stays reserved Self-hosted default
Pending or running Until the task finishes No limit
Succeeded The longer of retention.succeeded and limits.dedup_window, from when it finished 24 hours
Failed The longer of retention.failed and limits.dedup_window, from when it failed 7 days
Deleted limits.dedup_window, from when it was deleted 1 hour
Purged, or its queue deleted Not reserved: the ID is free immediately n/a

Old records are removed by a background cleanup pass (every minute by default), so an ID can stay reserved slightly longer than the table says, never shorter. Self-hosters set these values with limits.dedup_window, retention.succeeded and retention.failed (or PUSHQ_DEDUP_WINDOW, PUSHQ_RETENTION_SUCCEEDED, PUSHQ_RETENTION_FAILED); see server configuration. The values on the hosted service may differ.

Deduplication only compares IDs. pushq doesn’t look at the URL, body or schedule of the existing task, and the 409 response doesn’t return it. If you need the existing task, fetch it with a get-task call.

A deterministic ID is built from the work itself, so every attempt to enqueue the same work produces the same ID.

  • Use the business key. welcome-user-42, invoice-inv-881-reminder-1, refund-ord-1234.
  • Add a time bucket for “at most once per period”. digest-user-42-2026-11-28 sends one digest per user per day, however many times your scheduler fires.
  • Hash long or unsafe keys. Task IDs must match [A-Za-z0-9_-]{1,500}: letters, digits, _ and -, up to 500 characters. Characters such as :, ., / or @ are rejected with invalid_argument. Build the ID from a hex digest (for example SHA-256 of the email address plus the template name), prefixed with something readable.
  • Never include values that change between attempts: the current time, a random value, or a request ID generated per call. They make every create unique and turn deduplication off.

Leave task_id out when duplicates don’t matter; pushq then generates a UUIDv7 ID.

The batch endpoint reports already_exists per item instead of failing the whole request. Treat those items as enqueued.

const results = await pq.createTasks("emails", [
{ taskId: "welcome-user-42", path: "/hooks/send-welcome", jsonBody: { user_id: 42 } },
{ taskId: "welcome-user-43", path: "/hooks/send-welcome", jsonBody: { user_id: 43 } },
]);
const failed = results.filter((r) => r.error && r.error.code !== "already_exists");
if (failed.length > 0) throw new Error(`enqueue failed: ${JSON.stringify(failed)}`);

A batch holds up to 500 tasks and is not atomic: other items are created even when some fail. Two items with the same ID in one batch also dedupe; the second gets already_exists. See bulk enqueue for larger loads.

The SDKs don’t retry API calls themselves. If a create call times out, you can’t tell whether pushq stored the task. With a task_id set, retry the call: if the first one was stored, the retry gets already_exists, which you already treat as success. Without a task_id, a retry can create a second task.

  • Deleting a task doesn’t free its ID right away. A deleted task’s ID stays reserved for dedup_window (1 hour on self-hosted defaults). To re-enqueue sooner, use a new ID such as welcome-user-42-v2.
  • IDs are per queue. The same task_id in two queues creates two tasks. Pick one queue per kind of work, or put the queue’s purpose in the ID.
  • Purging frees IDs. After a purge, every purged ID can be used again immediately, so a replay of old events right after a purge enqueues them again.
  • Deduplication doesn’t stop duplicate deliveries. A single task can still reach your endpoint more than once because delivery is at-least-once. Make the handler idempotent as well.