Getting Started

Introduction

What Trypema is and when to use it.

Trypema is a Rust rate limiting library for enforcing per-key request rates with either local (in-process) state or Redis-backed shared state for multi-instance deployments.

These docs target Trypema v1.0.

When to use Trypema

Use Trypema when you want a small, composable primitive you can call from HTTP middleware (per user/IP/endpoint), worker queues (per tenant/job type), or internal clients (protect downstream dependencies).

Providers and strategies

You pick a provider (where state lives) and a strategy (how to behave near/over the target rate):

ChoiceOptionsWhen to use
ProviderLocalSingle-process, lowest latency, no shared limits
ProviderRedisShared limits across multiple processes/hosts
StrategyAbsolutePredictable hard rejection when over limit
StrategySuppressedGraceful degradation near capacity, with a hard cutoff

What you get

Two providers (Local, Redis), two strategies (Absolute, Suppressed), and rejection hints (retry_after_ms, remaining_after_waiting).

Concurrency notes:
  • Local limiting can temporarily overshoot under high contention.
  • Redis absolute inc(...) performs the admission check and increment inside a single Lua script.
  • For any provider, a separate is_allowed(...) check followed by a later inc(...) call is not atomic.

A quick decision guide

If you want...Start with
Single instance + predictable behaviorLocal + Absolute
Multiple instances + predictable behaviorRedis + Absolute
Graceful degradation under spikesSuppressed (Local or Redis)

Key design tips

Keys define your isolation boundary. Prefer stable keys, keep cardinality bounded (avoid attacker-controlled explosions), and include enough scope to debug (e.g. tenant_42 or route_search).

If you may see many unique keys over time, enable the cleanup loop to evict inactive keys.

Where to start

If you want predictable rejection, start with Absolute. If you want graceful degradation, use Suppressed. If you run more than one instance, use the Redis provider.

Next: Installation, Quickstart (Local), Quickstart (Redis).