Guides

Configuration

Validated settings controlling windows, grouping, suppression, cleanup, and synchronization.

RateLimiterBuilder builds every provider and limiter. The Redis and hybrid builders add their connection and namespace settings. Start with defaults, then tune only for a measured need.

A paper timeline with request buckets inside a moving rectangular window and one expired bucket outside its left edge.
Only buckets inside the moving window contribute to the live total; older buckets expire as time advances.

Choose settings by behavior

GoalSettingTradeoff
Change enforcement horizonWindowSizelonger windows smooth bursts but retain history longer
Change time precisionBucketSizesmaller buckets improve precision and increase state work
Shape gradual sheddingHardLimitFactorlarger values spread suppression across more traffic
Reduce factor recomputationSuppressionFactorCachePeriodlonger periods can make pressure briefly less current
Bound inactive statestale_after, cleanup_intervalfaster cleanup performs more background work
Balance hybrid flush delay and Redis loadSyncIntervalshorter intervals send increments to Redis sooner and increase Redis work
Bound the age of hybrid checksAllowedLagshorter lags show other instances sooner and increase Redis reads
Bound hybrid absolute rejection cachesallowed_lag_applies_to_rejectionstrue shows deleted or raised limits within AllowedLag and reads Redis more often
Isolate Redis stateprefixcooperating instances must use the same namespace

See Configuration Types for meanings, defaults, units, and validation. build() also validates relationships such as BucketSize <= WindowSize regardless of setter order.

Example

use std::{sync::Arc, time::Duration};
use trypema::{
    BucketSize, HardLimitFactor, RateLimiterBuilder, SuppressionFactorCachePeriod, WindowSize,
    local::LocalRateLimiterProvider,
};

let provider: Arc<LocalRateLimiterProvider> = RateLimiterBuilder::local()
    .window_size(WindowSize::minutes_or_panic(1))
    .bucket_size(BucketSize::milliseconds_or_panic(10))
    .hard_limit_factor(HardLimitFactor::new_or_panic(1.5))
    .suppression_factor_cache_period(
        SuppressionFactorCachePeriod::milliseconds_or_panic(100),
    )
    .stale_after(Duration::from_secs(600))
    .cleanup_interval(Duration::from_secs(30))
    .build()
    .unwrap();

Keep settings consistent for Redis-backed instances sharing one namespace.

Backends and targets

Start from the backend: RateLimiterBuilder::local(), RateLimiterBuilder::redis(connection), or RateLimiterBuilder::hybrid(connection). The type that the caller expects from build() selects the target: the provider of the backend or one of its two limiters. Without an annotation, name the target with a turbofish:

use std::time::Duration;
use trypema::{HardLimitFactor, RateLimiterBuilder, WindowSize};
use trypema::local::SuppressedLocalRateLimiter;

let limiter = RateLimiterBuilder::local()
    .window_size(WindowSize::minutes_or_panic(1))
    .hard_limit_factor(HardLimitFactor::new_or_panic(1.5))
    .stale_after(Duration::from_secs(600))
    .build::<SuppressedLocalRateLimiter>()
    .unwrap();

A target of another backend, such as a hybrid limiter from RateLimiterBuilder::local(), does not compile. Each provider and limiter also has a builder() shortcut, such as SuppressedLocalRateLimiter::builder(). A shortcut returns the same builder with its type as the target, so its build() needs no annotation.

  • The settings in the table above, except SyncInterval, AllowedLag, allowed_lag_applies_to_rejections, and prefix, are on every builder.
  • prefix is only on the Redis and hybrid builders.
  • sync_interval, allowed_lag, and allowed_lag_applies_to_rejections are only on the hybrid builder.
  • A target ignores the settings that its strategy does not use. The absolute limiters do not use hard_limit_factor and suppression_factor_cache_period. The suppressed hybrid limiter does not use allowed_lag_applies_to_rejections.
  • Each provider and limiter returns the window it was built with from window_size().

The Builder table lists the targets of each backend.