# Protect an endpoint: rate limit, priorities, lifetimes, automatic rollback

Once a [serverless endpoint](/docs/tutorial-serverless-endpoint) works, four settings keep it safe in daily use: a rate limit so one busy client cannot flood it, priorities so urgent jobs go first, lifetimes so old jobs do not wait for ever, and an automatic rollback so a broken update undoes itself. You need an endpoint and the `apidly` command (see [Use the CLI and API from scratch](/docs/tutorial-cli-and-api)). Changing endpoint settings needs a key with **Full access**; sending jobs needs **Servers**.

## 1. Set a rate limit

An endpoint takes at most 1,200 requests per minute through its address, all keys together, and one key at most 600. You can set a lower limit for one endpoint:

1. Open **Endpoints** and select the endpoint.
2. In **Scaling settings**, enter **Requests per minute**, for example `300`. Leave it empty for the platform limit of 1,200.
3. Select **Save**.

The same from a terminal:

```bash
apidly endpoints update EPN_ID --rate-limit 300
apidly endpoints update EPN_ID --rate-limit none
```

The second line goes back to the platform limit. Through the API:

```bash
curl -X PATCH https://apidly.com/api/v1/endpoints/EPN_ID \
  -H "Authorization: Bearer $APIDLY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"rateLimitPerMinute": 300}'
```

Send `null` to remove it. A request over the limit gets `429` with the code `rate_limited` and a `Retry-After` header, and it never reaches a worker, so it costs nothing. With your own limit set, every answer also carries `X-RateLimit-Limit`, `X-RateLimit-Remaining` and `X-RateLimit-Reset`. The limit can only lower the platform limit, and it does not apply to async jobs, which have their own limit of 100 waiting jobs. See [Rate limits](/docs/endpoints#rate-limits).

## 2. Put urgent jobs first with priorities

[Async jobs](/docs/endpoints#async-jobs) wait in a queue until a worker is free. Each job can have a `priority` from -10 to 10; the default is 0. Waiting jobs of one endpoint go to workers with the highest priority first, and jobs of the same priority oldest first.

```bash
apidly endpoints submit EPN_ID /generate -d '{"prompt": "Answer this customer now"}' --priority 5
apidly endpoints submit EPN_ID /generate -d '{"prompt": "Nightly report"}' --priority=-5
```

Write a negative value with `=`, as in the second line. Through the API:

```bash
curl -X POST https://apidly.com/api/v1/endpoints/EPN_ID/jobs \
  -H "Authorization: Bearer $APIDLY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"path": "/generate", "input": {"prompt": "Nightly report"}, "priority": -5}'
```

**The order is strict, and this is the rule to plan for:** a lower-priority job waits as long as a higher one is waiting for a worker. When there is always higher-priority work, it can wait until its queue time ends and then expire with `no_worker`. A higher-priority job that cannot be sent for a reason of its own (it failed, or another run already took it) does not hold the lower ones back. Give background work a negative priority rather than urgent work a high one, so that ordinary jobs at 0 are not held back. A job sent while a worker is free can start at once, unless a job with a higher priority is waiting. Jobs of different endpoints never affect each other.

## 3. Set how long jobs live

Two lifetimes can only shorten the defaults:

| Field | CLI flag | Range | Default | What it does |
|---|---|---|---|---|
| `queueTtlSeconds` | `--queue-ttl` | 60 to 3,600 | 3,600 | How long a job may wait for a worker. After that its status is `expired`, with `no_worker`. |
| `resultTtlSeconds` | `--result-ttl` | 60 to 86,400 | 86,400 | How long the finished job and its answer are kept. Then they are deleted. |

```bash
apidly endpoints submit EPN_ID /generate -d '{"prompt": "Hello"}' --queue-ttl 300 --result-ttl 3600
apidly endpoints job EPN_ID JOB_ID
```

The first line gives up on the job if no worker takes it within 5 minutes, and keeps the answer for an hour. The second shows the job's status. Through the API, add `"queueTtlSeconds": 300, "resultTtlSeconds": 3600` to the job body. Both are checked once a minute, so a job can end up to a minute later than the exact time.

## 4. Roll back a broken update automatically

Changing an endpoint's template, disk size, variables or hardware starts a [rolling update](/docs/endpoints#updating-without-downtime): new workers start, and old ones are released once the new ones are healthy. By default, when a new worker fails its health check, the update pauses and waits for you. With automatic rollback, it goes back to the earlier settings by itself.

1. Open the endpoint and, in **Scaling settings**, turn on **Roll back automatically**.
2. Select **Save**.

```bash
apidly endpoints update EPN_ID --auto-rollback on
```

Through the API, send `{"autoRollback": true}` with `PATCH /api/v1/endpoints/EPN_ID`. It is off by default.

When it acts, the activity list on the endpoint page shows **Rolled back automatically**. Old workers keep serving the whole time. It covers a new worker that fails its health check (three failed checks in a row after its start-up allowance, or not healthy within it). It does not cover a worker that could not be started at all, which still pauses the update; it never rolls back a second time; and after a template edit, where the earlier settings are unknown, the update pauses as usual.

## What it costs and how to stop spending

None of these settings costs anything. Rejected requests and expired jobs never reach a worker. Workers are billed by the second while they run, including the extra workers of a rolling update or a rollback. To stop spending, select **Pause** on the endpoint page (all workers are released), or run `apidly endpoints pause EPN_ID`. **Resume** brings it back; `apidly endpoints rm EPN_ID` deletes it for good.

## Next

- [Serverless endpoints](/docs/endpoints)
- [Your first endpoint with your own code](/docs/tutorial-serverless-worker)
