Rate limits and quotas
A fixed window per API key, per user, or per tenant, in one command on the shard that owns the key.
LIMIT key max window_ms returns the remaining allowance, or a null bulk when the window is full. A rejected call is not counted. The window is the key's deadline, so a quiet key disappears by itself. SLIDING weighs the previous window by how much of it still overlaps, so a burst at a window edge is not doubled. TOKENS is a token bucket that refills continuously and takes COST tokens per call. Both keep a few bytes per key, and WITHRETRY adds the wait to the reply for a Retry-After header. In WAL-backed modes the accepted call is a log record.
Keep in mindA key keeps the mode it was created with. The sliding window is an estimate from two counters, not an exact log of calls.
ReferenceLIMIT
.NET packageRuvio.AspNetCore.RateLimiting
LIMIT api:key-7 100 60000
# 99 -> 99 calls left in this 60 s window
# (nil) -> window is full, nothing was counted
LIMIT api:key-7 100 60000 SLIDING
LIMIT api:key-7 10 1000 TOKENS COST 2 WITHRETRY
# [8, 0] -> 8 tokens left, go ahead
# [-1, 150] -> refused, retry in 150 msuse ruvio_client::Client;
let mut db = Client::connect("127.0.0.1", 6379)?;
let left = db.limit("api:key-7", 100, 60_000)?;
// Some(99) -> 99 calls left in this 60 s window
// None -> window is full, nothing was counted
// Smoother: sliding window, or a token bucket that tells you how long to wait
let sliding = db.limit_sliding("api:key-7", 100, 60_000)?;
let bucket = db.limit_tokens_with_retry("api:key-7:burst", 10, 1_000, 2)?;
// bucket.allowed, bucket.remaining, bucket.retry_after_msawait using var connection = await RuvioConnection.ConnectAsync("127.0.0.1", 6379);
var db = await connection.GetDefaultDatabaseAsync();
long? left = await db.LimitAsync("api:key-7", 100, 60_000);
// 99 -> 99 calls left in this 60 s window
// null -> window is full, nothing was counted
// Smoother: sliding window, or a token bucket that tells you how long to wait
long? sliding = await db.LimitSlidingAsync("api:key-7", 100, 60_000);
LimitOutcome bucket = await db.LimitTokensWithRetryAsync("api:key-7:burst", 10, 1_000, 2);c, err := ruvio.Connect(ctx, ruvio.Options{Addr: "127.0.0.1:6379"})
if err != nil { return err }
left, ok, err := c.Limit(ctx, "api:key-7", 100, time.Minute)
if err != nil { return err }
fmt.Println(left, ok)
// 99 true -> 99 calls left in this 60 s window
// 0 false -> window is full, nothing was counted
// Smoother: sliding window, or a token bucket that tells you how long to wait
left, ok, err = c.LimitSliding(ctx, "api:key-7", 100, time.Minute)
bucket, err := c.LimitTokensWithRetry(ctx, "api:key-7:burst", 10, time.Second, 2)
// bucket.Allowed, bucket.Remaining, bucket.RetryAftervar c = RuvioClient.connect("127.0.0.1", 6379);
Long left = c.limit("api:key-7", 100, 60_000);
// 99 -> 99 calls left in this 60 s window
// null -> window is full, nothing was counted
// Smoother: sliding window, or a token bucket that tells you how long to wait
Long sliding = c.limitSliding("api:key-7", 100, 60_000);
LimitOutcome bucket = c.limitTokensWithRetry("api:key-7:burst", 10, 1_000, 2);import ruvio
r = ruvio.Ruvio("127.0.0.1", 6379)
left = r.limit("api:key-7", 100, 60_000)
# 99 -> 99 calls left in this 60 s window
# None -> window is full, nothing was counted
# Smoother: sliding window, or a token bucket that tells you how long to wait
sliding = r.limit_sliding("api:key-7", 100, 60_000)
allowed, remaining, retry_after_ms = r.limit_tokens_with_retry("api:key-7:burst", 10, 1_000, 2)import { createClient } from 'ruvio-client';
const c = createClient({ host: '127.0.0.1', port: 6379 });
const left = await c.limit('api:key-7', 100, 60_000);
// 99 -> 99 calls left in this 60 s window
// null -> window is full, nothing was counted
// Smoother: sliding window, or a token bucket that tells you how long to wait
const sliding = await c.limitSliding('api:key-7', 100, 60_000);
const bucket = await c.limitTokensWithRetry('api:key-7:burst', 10, 1_000, 2);
// bucket.allowed, bucket.remaining, bucket.retryAfterMsbuilder.Services.AddRuvioClient(builder.Configuration.GetSection("Ruvio"));
builder.Services.AddRuvioRateLimiting(options =>
{
options.KeyPrefix = "orders:rate:v1:";
// Default budget owner for every policy that does not name its own: here the tenant.
options.PartitionKey = context => context.User.FindFirst("tenant_id")?.Value ?? "";
// A request without a tenant shares one budget instead of failing. It is never unlimited.
// The default (Throw) treats a blank key as a configuration error.
options.EmptyPartition = RuvioEmptyPartitionMode.SharedBucket;
// 100 requests a minute per tenant (uses options.PartitionKey).
options.AddPolicy("create-order", permits: 100, window: TimeSpan.FromMinutes(1));
// A policy can count something else: this one is per client IP, whatever the tenant is.
options.AddPolicy("login", permits: 5, window: TimeSpan.FromMinutes(1),
partitionKey: context => context.Connection.RemoteIpAddress?.ToString() ?? "");
// Chained limits: a request must pass all of them, in this order. Put the strictest first,
// because a request refused by a later limit has already used a permit of the earlier ones.
options.AddPolicy("search", policy => policy
.Limit(20, TimeSpan.FromSeconds(10),
partitionKey: context => context.Connection.RemoteIpAddress?.ToString() ?? "") // per IP
.Limit(1000, TimeSpan.FromMinutes(1), algorithm: RuvioRateLimitAlgorithm.SlidingWindow) // per tenant
.Concurrency(maxConcurrent: 8, maxRequestDuration: TimeSpan.FromSeconds(30))); // 8 at once per tenant
// Applies to every routed endpoint that has no limit of its own. Opt out with DisableRuvioRateLimit().
options.DefaultPolicy = "create-order";
// Runs after the default 429 and Retry-After are set; change the status or write a body here.
options.OnRejected = async (context, rejection) =>
{
await context.Response.WriteAsJsonAsync(new
{
error = "rate_limited",
policy = rejection.Policy,
retryAfterSeconds = (int)rejection.RetryAfter.TotalSeconds,
});
};
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseRuvioRateLimiting();
app.MapPost("/login", HandleLogin).WithRuvioRateLimit("login");
app.MapGet("/search", HandleSearch).WithRuvioRateLimit("search");
app.MapPost("/orders", HandleOrder).RequireAuthorization(); // limited by DefaultPolicy
app.MapGet("/health", () => "ok").DisableRuvioRateLimit(); // never limited