Skip to content
pushqdocs
Get started

This pattern gives each customer (tenant, user, workspace) its own pushq queue with its own rate limit, so one busy customer can’t slow everyone else down. You name the queues tenants/<id>, create each one the first time that tenant enqueues work, and pause, re-limit or delete one tenant’s queue without touching the others.

Enqueue into the tenant’s queue. If the queue doesn’t exist yet the API answers 404 not_found; create the queue with its full configuration and enqueue again. In the normal case this costs one API call per task.

import { Pushq, PushqError } from "pushq";
const pq = new Pushq({ apiKey: process.env.PUSHQ_API_KEY });
// The full config for one tenant: always send all of it (see the caution below).
function tenantConfig(perSecond: number, concurrent: number) {
return {
rateLimits: { maxDispatchesPerSecond: perSecond, maxConcurrentDispatches: concurrent },
retry: { maxAttempts: 10, maxBackoffSeconds: 600 },
target: { baseUrl: "https://api.example.com" },
};
}
async function enqueueForTenant(tenantId: string, perSecond: number, concurrent: number, jsonBody: unknown) {
const queue = `tenants/${tenantId}`;
const task = { path: "/hooks/sync", jsonBody };
try {
return await pq.createTask(queue, task);
} catch (err) {
if (!(err instanceof PushqError && err.code === "not_found")) throw err;
await pq.upsertQueue(queue, tenantConfig(perSecond, concurrent)); // first task for this tenant
return await pq.createTask(queue, task);
}
}

The examples assume a client named pq (see install and configure an SDK). JSON field names are used in the text; the SDKs use their language’s casing (maxDispatchesPerSecond in TypeScript and Java, max_dispatches_per_second in Python, MaxDispatchesPerSecond in Go and .NET).

Use this when each customer must get a fair share of delivery, when a customer’s own downstream system (their webhook endpoint, their API key with a third party) has its own limit, or when you need to stop one customer’s work without stopping anyone else’s.

If two of your processes hit not_found for the same new tenant at the same moment, both upsert the queue. That is safe: PUT is idempotent, and with the same config the second call changes nothing.

Why one queue per tenant isolates customers

Section titled “Why one queue per tenant isolates customers”
  • Each queue has its own token bucket and concurrency cap. The dispatcher claims tasks from a queue only while that queue has rate-limit tokens and fewer than max_concurrent_dispatches requests in flight. A tenant with a million queued tasks is drained at its own rate while other tenants’ queues keep dispatching at theirs. See rate limits.
  • Retries, pause and purge are per queue. A tenant whose endpoint is failing backs off on its own schedule, and you can pause or empty that tenant’s queue alone.
  • Folders are only names. tenants/acme is a queue called acme in the folder tenants. A folder has no config of its own and exists as long as some queue name uses it, so there is nothing to create up front. Folders can nest: tenants/acme/emails and tenants/acme/webhooks give one tenant several queues.

On a self-hosted server all queues still share the server’s total in-flight capacity (dispatcher.global_concurrency, 512 by default; see self-hosting configuration). On the hosted service, the Free plan allows up to 1,000 queues and Pay as you go has no queue limit (see pricing).

Pick one of two safe approaches and stick to it:

  1. Send the full config every time. Build it from the tenant’s record in your database (plan, limits, endpoint), as tenantConfig() does above. Any upsert, from any process, then produces the right config.
  2. Create once, then patch. Create the queue with POST /v1/queues (createQueue) and treat already_exists as success. Change single settings later with PATCH, and never call PUT for tenant queues.

What breaks is mixing the two: patching a tenant’s limit after a plan upgrade, then letting an enqueue path upsert a default config that silently undoes it.

When a tenant upgrades or a tenant’s endpoint asks you to slow down, patch only the rate limit. PATCH leaves every other field as it is.

await pq.patchQueue("tenants/acme", {
rateLimits: { maxDispatchesPerSecond: 20, maxConcurrentDispatches: 10 },
});

If you follow approach 1 (full config every time), update the tenant’s record in your database too, or the next upsert sets the old limit back.

In Python, patch_queue is on the sync Pushq client only; AsyncPushq doesn’t have it.

List queues with name_prefix set to the folder, including the trailing slash. Results are ordered by name and paged (100 per page by default, up to 1,000).

let pageToken: string | undefined;
do {
const page = await pq.listQueues({ namePrefix: "tenants/", pageSize: 1000, pageToken });
for (const q of page.items) console.log(q.name, q.state, q.rateLimits.maxDispatchesPerSecond);
pageToken = page.nextPageToken;
} while (pageToken);

name_prefix is a plain string prefix: tenants/acme also matches tenants/acme-labs. Use tenants/acme/ (with the trailing slash) to list only the queues inside one tenant’s folder.

When one tenant floods its queue or its endpoint is down, pause that queue. A paused queue keeps accepting tasks; they wait until you resume it. Every other tenant keeps running.

await pq.pauseQueue("tenants/acme");
const stats = await pq.queueStats("tenants/acme");
console.log(stats.pending, stats.oldestDueAgeSeconds);
// later
await pq.resumeQueue("tenants/acme");

Pausing is the right tool when you want to keep the tenant’s backlog. To slow a tenant down instead of stopping it, change its rate limit. Resume also re-enables a queue that was set to disabled.

When a customer leaves, delete the queue. Deleting removes the queue and all of its tasks; pending tasks are never delivered. To drop a tenant’s backlog but keep the queue and its config, purge it instead.

await pq.purgeQueue("tenants/acme"); // drop every task, keep the queue
await pq.deleteQueue("tenants/acme"); // remove the queue and its tasks (admin key)

To remove a tenant with several queues (tenants/acme/emails, tenants/acme/webhooks), list tenants/acme/ and delete each queue. Like listing, deleting a queue needs an admin key.

  • Mixing PUT and PATCH on the same queue. A later upsert with a default config undoes earlier patches. See upsert replaces the whole queue config.
  • Upserting before every enqueue. It works, but doubles your API calls and makes the full-config rule apply to every code path that enqueues. The not_found fallback above only upserts once per tenant.
  • Deleting a queue a producer still uses. The next enqueue gets not_found and the fallback recreates the queue. Stop producers for that tenant first, or pause the queue instead of deleting it.
  • One queue shared by all tenants with a high limit. A single tenant’s burst then fills the queue ahead of everyone else, and you can’t pause just that tenant.