Strategies

Absolute

Sliding-window allow or reject admission with best-effort concurrency semantics.

Choose absolute strategy when application needs a binary admission decision.

A comparison gate branches into a replacement history stack and an ordered history-preservation path.
Conditional writes change state only after a comparator matches, then replace history or preserve one side of it.

Decisions

  • Allowed: increment may be recorded.
  • Rejected: increment is not recorded; best-effort backoff metadata is included.

Admission check and increment are not a strict cross-caller transaction. Concurrent callers may temporarily overshoot. Redis operations are individually atomic; overall admission remains best-effort.

Read and update

  • is_allowed(key) checks admission without recording usage.
  • get(key) returns live total as u64; unknown keys return 0 without creating state.
  • set_if(...) replaces history only when RateLimitComparator matches live total.
  • set_if_preserve_history(...) changes total while retaining newest or oldest history.

Conditional methods return ConditionalSetOutcome: matched, previous_total, and current_total. Comparator miss changes nothing. Matched updates replace sticky window capacity; matched zero removes key.

RateLimitComparator supports Always, Eq, Ne, Lt, and Gt. The numeric variants compare against the current live total. Use conditional updates to reconcile an external counter, reset a key, or change its stored capacity without a blind overwrite.

use trypema::RateLimitComparator;

let outcome = limiter.set_if(
    &key,
    &rate,
    RateLimitComparator::Lt(used),
    used,
);

Use RateLimitComparator::Always for unconditional update through conditional path.

Preserve history

  • PreserveNewest: reductions consume oldest buckets; increases extend newest bucket.
  • PreserveOldest: reductions consume newest buckets; increases extend oldest bucket.

If gradual load shedding fits better, use suppressed strategy.