Configuration
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.

Choose settings by behavior
| Goal | Setting | Tradeoff |
|---|---|---|
| Change enforcement horizon | WindowSize | longer windows smooth bursts but retain history longer |
| Change time precision | BucketSize | smaller buckets improve precision and increase state work |
| Shape gradual shedding | HardLimitFactor | larger values spread suppression across more traffic |
| Reduce factor recomputation | SuppressionFactorCachePeriod | longer periods can make pressure briefly less current |
| Bound inactive state | stale_after, cleanup_interval | faster cleanup performs more background work |
| Balance hybrid flush delay and Redis load | SyncInterval | shorter intervals send increments to Redis sooner and increase Redis work |
| Bound the age of hybrid checks | AllowedLag | shorter lags show other instances sooner and increase Redis reads |
| Bound hybrid absolute rejection caches | allowed_lag_applies_to_rejections | true shows deleted or raised limits within AllowedLag and reads Redis more often |
| Isolate Redis state | prefix | cooperating 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, andprefix, are on every builder. prefixis only on the Redis and hybrid builders.sync_interval,allowed_lag, andallowed_lag_applies_to_rejectionsare only on the hybrid builder.- A target ignores the settings that its strategy does not use. The absolute limiters do not use
hard_limit_factorandsuppression_factor_cache_period. The suppressed hybrid limiter does not useallowed_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.

