# Retry backoff and max attempts

> How pushq retries failed HTTP tasks: the exact backoff schedule with examples, max_attempts, max_retry_duration, Retry-After and per-task overrides.

Source: https://pushqueue.dev/docs/concepts/retries/

When a delivery fails, pushq retries the task after a backoff delay, and keeps doing so until it succeeds or its retry limits are reached. This page covers the retry settings, the exact exponential backoff schedule, `max_attempts` and `max_retry_duration`, `Retry-After`, and per-task overrides. The rules follow Google Cloud Tasks exactly, so the same settings give the same schedule.

The examples assume a client named `pq` (see [Install & configure an SDK](https://pushqueue.dev/docs/getting-started/sdks/)). Tables use JSON field names; SDKs use their language's casing (`maxAttempts`, `max_attempts`, `MaxAttempts`).

## Configure retries

Set these on the queue under `retry`, or on a single task to [override](#per-task-retry-overrides) the queue.

| Parameter | Type | Default | Description |
|---|---|---|---|
| `max_attempts` | integer | `100` | Total attempts, including the first. `-1` means unlimited. `0` is rejected. `1` means no retries. |
| `max_retry_duration_seconds` | number | `0` (unlimited) | How long, measured from the first attempt, retries may continue. |
| `min_backoff_seconds` | number | `0.1` | Delay before the first retry. |
| `max_backoff_seconds` | number | `3600` | No delay is ever longer than this. Must be at least `min_backoff_seconds`. |
| `max_doublings` | integer | `16` | How many times the delay doubles before it starts growing linearly, 0 to 31. |


**TypeScript**

```ts
await pq.patchQueue("emails", {
  retry: { maxAttempts: 10, minBackoffSeconds: 10, maxBackoffSeconds: 300, maxDoublings: 3 },
});
```

**Python**

```python
from pushq import Retry

pq.patch_queue(
    "emails",
    retry=Retry(max_attempts=10, min_backoff_seconds=10, max_backoff_seconds=300, max_doublings=3),
)
```

**Go**

```go
_, err := pq.PatchQueue(ctx, "emails", pushq.QueueConfig{
	Retry: &pushq.Retry{
		MaxAttempts:       pushq.Int(10),
		MinBackoffSeconds: pushq.Float64(10),
		MaxBackoffSeconds: pushq.Float64(300),
		MaxDoublings:      pushq.Int(3),
	},
}, "")
```

**Java**

```java
pq.patchQueue("emails", QueuePatch.builder()
    .retry(RetryConfig.builder()
        .maxAttempts(10).minBackoffSeconds(10).maxBackoffSeconds(300).maxDoublings(3).build())
    .build());
```

**.NET**

```csharp
await pq.PatchQueueAsync("emails", new QueuePatch
{
    Retry = new RetryConfig { MaxAttempts = 10, MinBackoffSeconds = 10, MaxBackoffSeconds = 300, MaxDoublings = 3 },
});
```

**curl**

```bash
curl -X PATCH https://api.pushqueue.dev/v1/queues/emails \
  -H "Authorization: Bearer $PUSHQ_API_KEY" \
  -d '{"retry": {"max_attempts": 10, "min_backoff_seconds": 10, "max_backoff_seconds": 300, "max_doublings": 3}}'
```

Retry settings are read when an attempt fails, so changing a queue's settings also changes the schedule of tasks that are already retrying.

## How the retry backoff schedule works

After the *n*-th failed attempt, the next attempt is scheduled this far in the future:

1. The first delay is `min_backoff`.
2. Each following delay doubles, `max_doublings` times.
3. After that, the delay grows linearly: each retry adds `2^max_doublings × min_backoff`.
4. No delay is ever longer than `max_backoff`.

The delay is counted from the moment the failed attempt finished, so the time your endpoint takes to respond adds to the gaps.

### Backoff example: 10-second minimum, 3 doublings

With `min_backoff_seconds: 10`, `max_backoff_seconds: 300` and `max_doublings: 3`:

| After failed attempt | Delay | Why | Time since first failure |
|---|---|---|---|
| 1 | 10 s | `min_backoff` | 0 s |
| 2 | 20 s | doubled once | 10 s |
| 3 | 40 s | doubled twice | 30 s |
| 4 | 80 s | doubled 3 times (`max_doublings` reached) | 70 s |
| 5 | 160 s | 80 + 80 (linear step = 2³ × 10 = 80) | 150 s |
| 6 | 240 s | 160 + 80 | 310 s |
| 7 | 300 s | 320 capped at `max_backoff` | 550 s |
| 8 and later | 300 s | capped | 850 s, 1150 s, ... |

(The last column ignores the time each request itself takes.)

### Default retry schedule

With the defaults (`min_backoff_seconds: 0.1`, `max_backoff_seconds: 3600`, `max_doublings: 16`), the delay doubles from 0.1 s until it reaches the one-hour cap:

| After failed attempt | Delay | After failed attempt | Delay |
|---|---|---|---|
| 1 | 0.1 s | 9 | 25.6 s |
| 2 | 0.2 s | 10 | 51.2 s |
| 3 | 0.4 s | 11 | 102.4 s (about 1.7 min) |
| 4 | 0.8 s | 12 | 204.8 s (about 3.4 min) |
| 5 | 1.6 s | 13 | 409.6 s (about 6.8 min) |
| 6 | 3.2 s | 14 | 819.2 s (about 13.7 min) |
| 7 | 6.4 s | 15 | 1638.4 s (about 27 min) |
| 8 | 12.8 s | 16 | 3276.8 s (about 55 min) |
| | | 17 and later | 3600 s (1 hour, capped) |

The first 16 retries together take about 1.8 hours. With the default `max_attempts` of 100, the remaining 83 retries are an hour apart, so a task that never succeeds keeps retrying for about 3.5 days before it is marked `failed`.

Use a larger `min_backoff_seconds` (a few seconds) when your endpoint fails because of something that takes time to recover, such as a deploy or a downstream outage; the default retries very quickly at first.

## max_attempts and max_retry_duration

Two settings limit retries: `max_attempts` (counts attempts, including the first) and `max_retry_duration_seconds` (time since the first attempt). How they combine depends on which ones are set:

| `max_attempts` | `max_retry_duration_seconds` | Retries stop when |
|---|---|---|
| `-1` (unlimited) | `0` (unlimited) | Never. The task retries until it succeeds or you delete it. |
| set | `0` (unlimited) | The attempt count is reached. |
| `-1` (unlimited) | set | The duration has passed. |
| set | set | **Both** limits are reached. |

The last row surprises people: with both limits set, a task keeps retrying until it has made `max_attempts` attempts **and** `max_retry_duration_seconds` has passed since its first attempt, whichever comes later. This matches Cloud Tasks. Because `max_attempts` defaults to 100, setting only `max_retry_duration_seconds: 3600` does not stop retries after an hour; it stops them after 100 attempts or one hour, whichever is later. To make the duration the only limit, also set `max_attempts: -1`.

The limits are checked after each failed attempt. A retry that was already scheduled still runs, even if it starts after `max_retry_duration_seconds` has passed.

## What counts as a failed delivery

An attempt **succeeds** only when your endpoint returns a 2xx status. Everything else is a failure and is retried:

| Outcome | `error_kind` in the attempt log |
|---|---|
| Any non-2xx status, including 3xx that isn't followed and every 4xx | `non_2xx` |
| No response within the task's `dispatch_deadline_seconds` (default 600) | `timeout` |
| Connection refused or reset | `connect` |
| DNS lookup failed | `dns` |
| TLS handshake or certificate error | `tls` |
| The request couldn't be built, for example a path task on a queue whose `base_url` was removed | `config` |
| The outcome was never recorded, for example the server restarted mid-delivery | `lease_lost` |

pushq does not treat 4xx as permanent: a `400` or `404` is retried like a `500`. If your endpoint knows a task can never succeed, return a 2xx (and log or record the problem) to stop the retries; see [stop retrying a task](https://pushqueue.dev/docs/patterns/stop-retrying/).

**Redirects** are followed automatically (up to 10 hops) and the whole chain counts as a single attempt; only the final response's status matters. Redirects follow standard HTTP client rules: `307` and `308` repeat the same method and body, while `301`, `302` and `303` switch to a `GET` without a body. Point tasks at the final URL so the body isn't dropped.

You can see the outcome of each attempt with the [attempts endpoint](https://pushqueue.dev/docs/concepts/tasks/#delivery-attempts-history).

## Retry-After and the queue cool-down

When your endpoint answers `429 Too Many Requests` or `503 Service Unavailable` with a `Retry-After` header (in seconds or as an HTTP date), pushq treats it as back-pressure:

- **The task** is retried after the `Retry-After` delay instead of the computed backoff. The delay is capped at the task's `max_backoff_seconds`.
- **The whole queue** cools down: it starts no new deliveries for the `Retry-After` delay, up to 30 seconds. Requests already in flight are not affected.

`Retry-After` on any other status is ignored, and a 429 or 503 without the header is an ordinary failure with the normal backoff. A `Retry-After` response is still a failed attempt and counts toward `max_attempts`.

Use this from your endpoint when it is overloaded: answering `429` with `Retry-After: 10` slows the whole queue down without you changing any settings. The [Rate limits guide](https://pushqueue.dev/docs/guides/rate-limits/#back-pressure-with-retry-after) shows an example handler.

## Per-task retry overrides

Any retry field can be set on a single task. Fields you leave out come from the queue's settings at the time of each failure.


**TypeScript**

```ts
await pq.createTask("emails", {
  path: "/hooks/send-receipt",
  jsonBody: { orderId: 1001 },
  retry: { maxAttempts: 3, minBackoffSeconds: 30 },
});
```

**Python**

```python
from pushq import Retry

pq.create_task(
    "emails",
    path="/hooks/send-receipt",
    json_body={"order_id": 1001},
    retry=Retry(max_attempts=3, min_backoff_seconds=30),
)
```

**Go**

```go
_, err := pq.CreateTask(ctx, "emails", pushq.TaskCreate{
	Path:     "/hooks/send-receipt",
	JSONBody: map[string]any{"order_id": 1001},
	Retry:    &pushq.Retry{MaxAttempts: pushq.Int(3), MinBackoffSeconds: pushq.Float64(30)},
})
```

**Java**

```java
pq.createTask("emails", TaskCreate.builder()
    .path("/hooks/send-receipt")
    .jsonBody(Map.of("order_id", 1001))
    .retry(RetryConfig.builder().maxAttempts(3).minBackoffSeconds(30).build())
    .build());
```

**.NET**

```csharp
await pq.CreateTaskAsync("emails", new TaskCreate
{
    Path = "/hooks/send-receipt",
    JsonBody = new { order_id = 1001 },
    Retry = new RetryConfig { MaxAttempts = 3, MinBackoffSeconds = 30 },
});
```

**curl**

```bash
curl -X POST https://api.pushqueue.dev/v1/queues/emails/tasks \
  -H "Authorization: Bearer $PUSHQ_API_KEY" \
  -d '{"path": "/hooks/send-receipt", "json_body": {"order_id": 1001}, "retry": {"max_attempts": 3, "min_backoff_seconds": 30}}'
```

The override is validated together with the defaults when the task is created, so an invalid combination (such as a `max_backoff_seconds` below `min_backoff_seconds`) is rejected with `400 invalid_argument`. A task's `retry` field in API responses shows only its override, not the effective settings.

Use this when one kind of task in a shared queue needs different handling, for example a time-sensitive notification that isn't worth delivering after three tries.

## What happens when retries run out

When the limits are reached after a failed attempt, the task becomes `failed`:

- It is never attempted again, and `finished_at` is set.
- It stays visible, with its full attempt history, for the failed-task retention period (7 days by default), and its ID stays reserved for that long.
- If the queue has a `dead_letter_queue`, a copy of the task is created there. See [Dead-letter queues](https://pushqueue.dev/docs/concepts/dead-letter-queues/).

To find failed tasks, list them with `state=failed` (see [List tasks](https://pushqueue.dev/docs/concepts/tasks/#list-and-filter-tasks)) or watch the `failed` count in [queue stats](https://pushqueue.dev/docs/concepts/queues/#queue-stats-and-monitoring). There is no endpoint to retry a failed task in place; create a new task with the same content (with a new `task_id`, if you set one), or use a dead-letter queue to replay it.

## Read more

- [Stop retrying a task](https://pushqueue.dev/docs/patterns/stop-retrying/): end retries from your handler on purpose.
- [Dead-letter queues](https://pushqueue.dev/docs/concepts/dead-letter-queues/): keep and replay tasks that run out of retries.
- [Rate limits and concurrency](https://pushqueue.dev/docs/guides/rate-limits/): how retries share a queue's rate budget.
- [Troubleshooting](https://pushqueue.dev/docs/guides/troubleshooting/): find out why tasks keep failing.
