Distributed Rate Limiting for Rust.
High-performance, ergonomic primitives for local (in-memory) and Redis-backed rate limiting. Built for Tokio with atomic Lua scripts and fractional rates.
cargo add trypema --features redis-tokio
use trypema::{RateLimit, RateLimitDecision}; use trypema::redis::RedisKey; // `rl`: a shared `RateLimiter` created once at startup let rate = RateLimit::try_from(10.0).unwrap(); // Local (sync, in-process) if matches!(rl.local().absolute().inc("user_123", &rate, 1), RateLimitDecision::Allowed) { // proceed } // Redis (async, distributed) let key = RedisKey::try_from("user_123".to_string()).unwrap(); if matches!(rl.redis().absolute().inc(&key, &rate, 1).await.unwrap(), RateLimitDecision::Allowed) { // proceed }
Why Trypema?
Hybrid Architecture
Seamlessly switch between Local (in-process RAM) for microsecond latency and Redis for distributed consistency.
Async & Atomic
Built for Tokio. Redis operations use atomic Lua scripts to prevent race conditions in distributed environments.
Fractional Rates
Define limits with precision. Support for f64 rates like 0.5 req/s (1 request every 2 seconds).
Strategies
Choose the enforcement strategy that fits your traffic pattern.
Absolute Strategy
Standard rate limiting. Requests are either Allowed or Rejected (with a retry_after duration). Best for strict API quotas.
Suppressed Strategy
Graceful degradation. Instead of hard rejections, returns a Suppressed signal near capacity to trigger load shedding or cheaper fallback logic.

