Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GCRA backends

GCRA (the generic cell rate algorithm) gives each client key a continuously replenished request quota. It differs from a fixed window: capacity returns one request at a time rather than all at a window boundary. FixedWindow remains available when that simpler boundary-based behavior is the right fit.

The crate includes three GCRA policies:

PolicyState locationShared across replicasRuntime / service
MemoryGcraprocess memorynonone; governor
RedisGcraRedisyesTokio or Smol Redis connection
PostgresGcraPostgreSQLyesSQLx Tokio PgPool and an installed migration

Choose a backend according to where the authoritative state must live. Do not use MemoryGcra when a limit must be global across independently running replicas. Redis and PostgreSQL make one backend state transition per charge; their availability and latency are part of the request path.

Common quota and decision contract

Construct each policy from a GcraQuota:

#![allow(unused)]
fn main() {
use std::time::Duration;
use tower_rate_limiter::{GcraQuota, GcraQuotaError};

fn configure_quota() -> Result<GcraQuota, GcraQuotaError> {
    GcraQuota::new(100, Duration::from_secs(60), 20)
}
}
  • rate requests replenish over period.
  • burst is the total immediate capacity of a fresh key. It is not capacity added on top of rate.
  • Each included adapter charges exactly one request. Weighted charges and refunds are not part of this API.
  • The configured emission interval is rounded up to whole microseconds so quota quantization itself does not increase the configured rate. This does not remove backend arithmetic error: Redis retains the upstream Lua artifact’s floating-point behavior.
  • A complete burst must replenish within one year. The constructor rejects longer windows.

All adapters produce RateLimitPolicy Decision values with the same meanings: an allowed result contains the configured limit, advertised window, remaining capacity, and reset duration; a rejection has zero remaining capacity plus an earliest retry_after. These values drive the middleware’s RateLimit and Retry-After fields.

The contract describes quota and decision meanings, not identical admission or duration values at every precision boundary. Memory uses its process clock, Redis uses TIME, and PostgreSQL captures clock_timestamp() after acquiring a row lock. Network latency, elapsed time while producing a response, and backend-specific rounding can make individual reset_after or retry_after values differ slightly. Treat fields as pacing guidance, not a distributed-clock comparison API. Redis can admit fewer or more requests than exact GCRA arithmetic at very small intervals, including exceeding the configured burst. See the Redis precision limits before choosing microsecond-scale quotas.

The shared backend tests cover burst = 1, a non-divisible rate/period, key isolation, clone-shared state, rejection without extending the penalty, recovery after retry, and a full burst after an idle window. They compare those behaviors and metadata bounds rather than exact duration equality between independently clocked backends. Separate Redis tests characterize the pinned artifact at 1µs, 10µs, and 100µs recovery boundaries and record known floating-point burst deviations.

Identity, namespace, and versioning

The layer scopes the extracted client key with policy_name before calling the policy. Give every semantically distinct quota a stable policy name. RedisGcra and PostgresGcra additionally offer .with_namespace(...) to separate applications or deployments that share the same backend.

All replicas that charge the same backend state identity must use the same effective quota. A quota change changes the interpretation of stored theoretical-arrival-time state. During a rollout, version the policy_name or backend namespace (for example, public-api-v2 or service-v2) so old and new instances cannot reinterpret the same stored state with different emission intervals. Allow the old state to expire before retiring its namespace.

Backend guides

  • Memory GCRA covers local governor state and its process boundary.
  • Redis GCRA covers the pinned upstream Lua artifact, Redis ACLs, and topology limits.
  • PostgreSQL GCRA covers applying the migration, permissions, and expired-state cleanup.

Failure behavior

MemoryGcra normally has no remote dependency. Redis and PostgreSQL errors are policy failures: PolicyFailureMode::Reject is the default and returns the configured middleware rejection (normally 503); PolicyFailureMode::Allow must be selected explicitly and passes the request to the inner service without rate-limit fields or context. In either mode, configure dependency timeouts and monitor backend latency and failures.