Backends
The backend is Trypema's own provider or limiter, in an Arc. Build it with Trypema's
RateLimiterBuilder and give it to TrypemaLimiter::builder. The type that you annotate selects
what build() returns. The middleware reads the window from the backend, so the limit advertised
in headers always agrees with the limit enforced.
| Backend | Feature | Provider (both strategies) | Absolute limiter | Suppressed limiter |
|---|---|---|---|---|
| Local | none | LocalRateLimiterProvider | AbsoluteLocalRateLimiter | SuppressedLocalRateLimiter |
| Redis | redis | RedisRateLimiterProvider | AbsoluteRedisRateLimiter | SuppressedRedisRateLimiter |
| Hybrid | redis | HybridRateLimiterProvider | AbsoluteHybridRateLimiter | SuppressedHybridRateLimiter |
The local types are in trypema::local, the Redis types in trypema::redis, and the hybrid
types in trypema::hybrid. Import them through actix_trypema::trypema.
- A provider holds both strategies.
.strategy(Strategy::Suppressed)selects the suppressed strategy; the default is absolute. - A single limiter holds one strategy, and its type sets the strategy. Its builder has no
.strategy(..). It does not create the state of the other strategy. - A limiter and a provider with the same prefix use the same Redis keys, so they share counts.
| Backend | Shared across instances | Cost per request |
|---|---|---|
| Local | No: each process counts on its own. | In memory. |
| Redis | Yes, exactly. | One Redis round trip; Redis 7.2+. |
| Hybrid | Yes, eventually: counts sync every interval. | In memory for most requests. |
Build a backend once and clone the Arc; clones share one set of counts.
Local
use std::sync::Arc;
use actix_trypema::{Strategy, TrypemaLimiter, extract::PeerIp};
use actix_trypema::trypema::{BucketSize, RateLimit, RateLimiterBuilder, WindowSize, local::LocalRateLimiterProvider};
// Both strategies, in process. Other Trypema settings go on the same builder.
let backend: Arc<LocalRateLimiterProvider> = RateLimiterBuilder::local()
.window_size(WindowSize::seconds_or_panic(60))
.bucket_size(BucketSize::milliseconds_or_panic(50))
.build()?;
// The absolute strategy: the default.
let per_ip = TrypemaLimiter::builder(backend.clone())
.namespace("ip")
.extractor(PeerIp::default())
.rate(RateLimit::per_minute_or_panic(600.0))
.build()?;
// The suppressed strategy, on the same provider.
let search = TrypemaLimiter::builder(backend)
.namespace("search")
.extractor(PeerIp::default())
.rate(RateLimit::per_minute_or_panic(120.0))
.strategy(Strategy::Suppressed)
.build()?;
Use a local backend for a single instance, or for limits that are fine per instance.
Redis
Every instance pointing at the same Redis and prefix shares one limit. Redis 7.2 or newer is required.
use std::sync::Arc;
use actix_trypema::{TrypemaLimiter, extract::PeerIp};
use actix_trypema::trypema::{
RateLimit, RateLimiterBuilder, WindowSize,
redis::{RedisKey, SuppressedRedisRateLimiter},
};
let connection = redis::Client::open("redis://127.0.0.1:6379/")?
.get_connection_manager()
.await?;
// The suppressed strategy only, exact across instances. Services sharing one Redis keep their
// counts apart with their own prefix.
let backend: Arc<SuppressedRedisRateLimiter> = RateLimiterBuilder::redis(connection)
.prefix(RedisKey::try_from("checkout-api")?)
.window_size(WindowSize::minutes_or_panic(1))
.build()?;
let limiter = TrypemaLimiter::builder(backend)
.namespace("ip")
.extractor(PeerIp::default())
.rate(RateLimit::per_minute_or_panic(60.0))
.build()?;
The prefix separates services or environments sharing one Redis; the namespace separates
limiters inside one service. Without .prefix(..), the builder uses Trypema's default prefix.
Hybrid
A hybrid backend decides most requests from in-process state and syncs counts to Redis in the background. It shares limits across instances at close to local speed. The trade-off: an instance's view of the other instances lags by up to one sync interval, so the combined limit can briefly overshoot.
use std::sync::Arc;
use actix_trypema::trypema::{
RateLimiterBuilder, WindowSize,
hybrid::{AbsoluteHybridRateLimiter, SyncInterval},
};
let connection = redis::Client::open("redis://127.0.0.1:6379/")?
.get_connection_manager()
.await?;
// The absolute strategy only, shared through a local fast path.
let backend: Arc<AbsoluteHybridRateLimiter> = RateLimiterBuilder::hybrid(connection)
.window_size(WindowSize::minutes_or_panic(1))
.sync_interval(SyncInterval::milliseconds_or_panic(50))
.build()?;
Most hybrid requests are decided without awaiting anything. Only requests that need Redis (a
key's first request, a refresh, or a key whose local share ran out) take the async path.
remaining_header(true) sends every request down the async path, so leave it off where speed
matters.
When Redis fails
Every Redis and hybrid call is bounded by backend_timeout (200 ms by default). A timeout or an
error is a backend failure, handled by on_backend_error. Only the Redis and hybrid backends
have these two settings.
| Policy | On failure |
|---|---|
BackendErrorPolicy::FailOpen (default) | Admit the request. A limiter outage should not become an API outage. |
BackendErrorPolicy::FailClosed | Reject with 503 Service Unavailable. Use it on login, OTP and other abuse-prone routes. |
BackendErrorPolicy::Fallback(provider) | Decide with an Arc<LocalRateLimiterProvider> on the same window; each instance enforces the limit on its own. The provider runs the strategy of the primary backend. |
BackendErrorPolicy::Custom(fn) | Decide per BackendFailure (timeout or error). |
use std::{sync::Arc, time::Duration};
use actix_trypema::{BackendErrorPolicy, BackendFailure, ErrorAction, TrypemaLimiter, extract::PeerIp};
use actix_trypema::trypema::{
RateLimit, RateLimiterBuilder, WindowSize,
local::LocalRateLimiterProvider,
redis::AbsoluteRedisRateLimiter,
};
use actix_web::http::StatusCode;
let window = WindowSize::minutes_or_panic(1);
let connection = redis::Client::open("redis://127.0.0.1:6379/")?
.get_connection_manager()
.await?;
let backend: Arc<AbsoluteRedisRateLimiter> = RateLimiterBuilder::redis(connection)
.window_size(window)
.build()?;
// Login: never let attempts through unchecked.
let login = TrypemaLimiter::builder(backend.clone())
.namespace("login")
.extractor(PeerIp::default())
.rate(RateLimit::per_minute_or_panic(5.0))
.on_backend_error(BackendErrorPolicy::FailClosed)
.backend_timeout(Duration::from_millis(30))
.build()?;
// API: keep limiting per instance while Redis is down. The fallback must use the same window.
let fallback: Arc<LocalRateLimiterProvider> = RateLimiterBuilder::local()
.window_size(window)
.build()?;
let api = TrypemaLimiter::builder(backend.clone())
.namespace("api")
.extractor(PeerIp::default())
.rate(RateLimit::per_minute_or_panic(600.0))
.on_backend_error(BackendErrorPolicy::Fallback(fallback))
.build()?;
// Search: admit on errors, but shed load when Redis is merely slow.
let search = TrypemaLimiter::builder(backend)
.namespace("search")
.extractor(PeerIp::default())
.rate(RateLimit::per_minute_or_panic(120.0))
.on_backend_error(BackendErrorPolicy::Custom(Arc::new(|failure| match failure {
BackendFailure::Timeout(_) => ErrorAction::Reject {
status: StatusCode::SERVICE_UNAVAILABLE,
},
_ => ErrorAction::Admit,
})))
.build()?;
If the fallback provider uses another window, build() returns
ConfigError::FallbackWindowMismatch. With Fallback, n instances admit up to n times the
limit while Redis is down, and those counts are not carried back when it recovers. Failures are
logged as warnings at most once per second per process, with the namespace and the failure kind
only.

