Skip to content

Rolling windows and concurrency

By default, a minute, hour or day limit resets at the UTC boundary. Say each of your API’s customers may make 1,000 requests an hour with their key. A key could send 1,000 requests at 12:59 and 1,000 more at 13:01, and both bursts would get a yes. Set rolling: true and the limit counts the last 60 seconds, 60 minutes or 24 hours at the moment of each check instead.

import { createClient } from "@limitry/sdk";
// Uses the LIMITRY_API_KEY environment variable.
const limitry = createClient();
const limit = await limitry.limits.create({
name: "API per hour",
subject: { kind: "apikey" },
action: "api-request",
amount: 1000,
window: "hour",
rolling: true,
});
console.log(limit.id);

With every, a rolling limit counts the last every windows, such as any 5 hours. A rolling minute limit spans at most 60 minutes. An hour limit spans at most 24 hours, and a day limit at most 30 days. A rolling limit takes no start, because it always counts back from each check.

Limitry counts use in 60 slices of the window. With every at 1, a slice is a second for a minute, a minute for an hour, and 24 minutes for a day. The oldest slice counts in full, so a rolling limit never allows more than its amount. It may say no up to one slice early.

In a check’s response, resetsAt is when enough use has left the window for the subject to try again. On a no, retryAfterSeconds gives the same moment. Send it as Retry-After with your 429. The Rate-limit your API’s customers guide adds a second rolling limit of 60 in any minute, against bursts.

week, month, year, total and concurrent limits can’t be rolling. In the app, choose In any 60 seconds, In any 60 minutes or In any 24 hours as the window.

A limit with the window concurrent caps work in progress, and its amount is how many may run at once. Say exports are heavy, and each workspace in your product may run at most 3 at the same time. Each reservation takes one slot, whatever its cost. It gives the slot back when you commit or release it, or when it expires. The cost still counts against the subject’s other limits.

This limit allows at most 3 exports at once per workspace.

import { createClient } from "@limitry/sdk";
// Uses the LIMITRY_API_KEY environment variable.
const limitry = createClient();
const limit = await limitry.limits.create({
name: "Exports at once",
subject: { kind: "workspace" },
action: "export",
amount: 3,
window: "concurrent",
});
console.log(limit.id);

Then reserve a slot while each export runs. Limitry says no to a 4th export until one of the 3 ends. reserve gives the slot back when the work finishes or fails.

const file = await limitry.reserve(
{ subject: { kind: "workspace", id: "ws_42" }, action: "export" },
() => runExport(),
);
  • While a slot is free, a consume or preview check against a concurrent limit gets a yes, but takes no slot.
  • When work holds every slot, retryAfterSeconds is when the earliest reservation expires. A release can free one sooner.
  • If you reserve, commit and release yourself (see Reservations), release in a finally block. A reservation you never commit or release keeps its slot until it expires.
  • When your code can’t reach Limitry and the limit’s fail mode is open, reserve still runs at most amount at once in your process for each subject and action. The SDK counts the reservations it has going, and remembers the amount from Limitry’s last response. Beyond that, reserve throws UnavailableError, as it would for a closed limit. The count is per process, so several servers together may run more until Limitry responds again. A process that hasn’t yet had a response for that subject and action doesn’t hold anything back, just as a plain open limit wouldn’t.

A reservation lasts ttlSeconds, which is 300 by default and 3,600 at most. For work that runs longer, extend it as a heartbeat.

await limitry.reserve(
{ subject: { kind: "workspace", id: "ws_42" }, action: "export" },
async (reservation) => {
for (const part of parts) {
await exportPart(part);
await reservation.extend(600);
}
},
);

If your worker dies, it stops extending, and the slot frees itself.

A limit can’t change to or from concurrent, because one counts use and the other counts open reservations. You can’t change a limit’s window once it exists, so create a new limit instead. See What can change.

Rolling limits report like any other. On the Usage page, Right now shows each subject’s use in the last window.

For a concurrent limit, the chart shows how much work the limit let start each day. Right now shows the slots each subject has reserved, for subjects checked in the last hour.

For both kinds, Limitry sends limit.reached at most once an hour per subject.