# Queues

A queue holds work your application hands itself for later: sending receipts, resizing uploads, calling a slow outside service. Messages are kept in the project's database until they are finished, so a restart loses nothing.

- **At least once.** A message that was taken and not finished comes back when its time runs out. Write the work so that doing it twice does no harm (for example, check whether the receipt was already sent).
- **Delayed delivery.** A message can be held back for up to 7 days before it is first handed out.
- **Retries with a growing wait.** A message given back is tried again after the queue's retry delay, doubled with each attempt (up to 12 hours).
- **Dead letters.** After its last attempt a message is set aside. Nothing more happens to it until you put it back or remove it.
- **Batches.** Up to 100 messages are sent or taken at a time.

Queues are for your **own server**: every route needs the project's **secret key**, and a queue belongs to the whole project, not to one organization. Never send the secret key to a browser or an app. To let a signed-in person start work, have them call one of your functions, and let the function send the message.

## Send

```
POST /v1/queues/<queue>/messages
```

```
curl -s $API/v1/queues/send-receipts/messages -H "apikey: $SECRET_KEY" \
  -H "content-type: application/json" -d '{"body": {"orderId": 4211}}'
```

```json
{ "ids": ["9184"] }
```

- `body` is any JSON, up to 64 KB. Put large things in [storage](https://rezincore.com/docs/storage.md) and send their path.
- `delaySeconds` (0 to 604800) holds the message back first.
- Several at once: `{"messages": [{"body": …, "delaySeconds": 60}, …]}`, up to 100. A batch with one bad message is refused whole.
- Sending to a name that has no queue yet makes one with the usual settings (below).

## Take, finish, try again

```
POST /v1/queues/<queue>/receive     {"max": 10, "visibilitySeconds": 60}
```

```json
{ "messages": [ { "id": "9184", "receipt": "3f0c…", "body": { "orderId": 4211 }, "attempts": 1, "enqueuedAt": "2026-11-27T15:04:05.000Z" } ] }
```

A message taken is hidden from everyone else for `visibilitySeconds` (the queue's own setting if left out). Before that runs out, finish it or give it back with its **receipt**:

```
POST /v1/queues/<queue>/ack     {"receipts": ["3f0c…"]}
→ { "finished": 1 }

POST /v1/queues/<queue>/retry   {"receipts": ["3f0c…"], "error": "card service timed out"}
→ { "retried": 1, "dead": 0 }
```

- `retry` takes an optional `delaySeconds`; without it the queue's backoff decides.
- A receipt whose time ran out finishes nothing (`"finished": 0`): the message may already be with someone else.
- `GET /v1/queues/<queue>` gives the counts (ready, delayed, being worked on, dead) and the settings; `GET /v1/queues` lists them all with the plan's limits.

## Settings

Set in the console (**Queues** in the project) when a queue is made, and changed there at any time.

| Setting | What it does | Usual |
|---|---|---|
| Visibility | Seconds a taken message is hidden from others (1 to 43,200) | 30 |
| Attempts | How many times a message is handed out before it is set aside (1 to 100) | 5 |
| Retry delay | The first wait before a message given back is tried again, doubled each time (0 to 3,600 seconds) | 10 |
| Function | One of the project's functions that consumes the queue | none |
| Runs at once | How many runs of that function may work on the queue at the same time | 1 |
| Messages a run | How many messages each run is given (1 to 100) | 1 |

## Consumed by a function

Give a queue a function and Rezin Core runs it on what arrives, never more runs at once than the queue allows. The function is called with a POST whose body is:

```json
{ "type": "queue", "queue": "send-receipts", "messages": [ { "id": "9184", "body": { "orderId": 4211 }, "attempts": 1, "enqueuedAt": "…" } ] }
```

- A `2xx` answer finishes every message in the batch.
- To finish some and try others again, answer `2xx` with `{"retry": ["9184"]}`.
- Any other answer, a failure or a timeout tries the whole batch again by the queue's backoff.
- While the function is switched off, the queue is paused, or the plan has no function runs left, messages wait where they are and no attempt is used.
- A taken batch stays hidden for at least the function's time limit plus 30 seconds.

Each run is an ordinary [function](https://rezincore.com/docs/functions.md) run: shut in, logged, and counted as a function run. A function can also **send** to a queue: give it the project's secret key as one of its [secrets](https://rezincore.com/docs/secrets.md) and call `/v1/queues/<queue>/messages` on the project's own API.

```js
export default async (request, context) => {
  const { messages } = await request.json();
  const retry = [];
  for (const m of messages) {
    const sent = await fetch('https://receipts.example.com/send', { method: 'POST', body: JSON.stringify(m.body) });
    if (!sent.ok) retry.push(m.id);
  }
  return Response.json({ retry });
};
```

## Dead letters

A message whose attempts ran out (given back too often, or not finished before its last time ran out) is set aside with its last error.

```
GET  /v1/queues/<queue>/dead?limit=50
POST /v1/queues/<queue>/dead/redrive    {"ids": ["9184"]}     (leave out "ids" for all)
```

Putting a message back resets its attempts. The console's queue page lists dead letters with their bodies, and can put them back or remove them.

## From the client library

On your server, with the secret key:

```js
const receipts = rezin.queue('send-receipts');
await receipts.send({ orderId: 4211 });
await receipts.send({ orderId: 4212 }, { delaySeconds: 300 });

for (const m of await receipts.receive({ max: 10 })) {
  try { await sendReceipt(m.body.orderId); await receipts.ack(m.receipt); }
  catch (err) { await receipts.retry(m.receipt, { error: String(err) }); }
}
```

## Limits

| | Free | Paid plans |
|---|---|---|
| Queues a project | 5 | 50 |
| Messages held, all queues together | 10,000 | 500,000 |
| Messages sent a month | 500,000 | 5,000,000, then per million |

- One message is up to 64 KB of JSON; up to 100 are sent or taken at a time.
- A message nobody finished, waiting or dead, is removed 14 days after it was sent.
- **Queue messages** counts each message sent (a message tried several times counts once). It is a plan allowance under the [spending cap](https://rezincore.com/docs/spending-caps.md); on Free, sending stops at the allowance until the month turns. See [Plans and limits](https://rezincore.com/docs/plans-and-limits.md).

## Errors

| Status | Code | When |
|---|---|---|
| 400 | `bad_request` / `bad_name` | A message without a body, a value out of range, receipts that are not receipts, or a name that cannot be used |
| 402 | `limit_reached` | The plan's queue messages, or the spending cap, are used up |
| 403 | `secret_key_required` | A publishable key: queues need the secret key |
| 404 | `not_found` | No such queue |
| 409 | `too_many` / `queue_full` | The plan's number of queues, or of messages held, is reached |
| 413 | `too_large` | A message larger than 64 KB of JSON |
