distkit

Distributed primitives for Rust, backed by Redis.

distkit gives you the building blocks distributed services keep reinventing - counters, instance-aware counters, locks, and rate limiting - behind small, async APIs that mirror the std and tokio types you already know. #![forbid(unsafe_code)], no panics in library code.

Cargo.toml
[dependencies]
distkit = "0.5"
main.rs
use distkit::{DistkitRedisKey, counter::{StrictCounter, CounterOptions, CounterTrait}};

let conn = redis::Client::open("redis://127.0.0.1/")?
    .get_connection_manager().await?;
let prefix = DistkitRedisKey::try_from("my_app".to_string())?;

let counter = StrictCounter::new(CounterOptions::new(prefix, conn));
let key = DistkitRedisKey::try_from("page_views".to_string())?;
counter.inc(&key, 1).await?;

One toolkit, four primitives

Each primitive is its own opt-in feature. Pull in only what you use - the core counters ship by default.

    Counters

    Strict counters are atomic per call; lax counters buffer in memory and flush on an interval for sub-microsecond writes. Conditional and batch operations on both.

    Instance-aware counters

    Each instance owns a slice of the total. The cumulative is the sum of live instances, and dead instances are cleaned up automatically via heartbeats.

    Distributed locks

    Redis-backed Mutex and writer-preferring RwLock that mirror tokio::sync. RAII guards, background lease renewal, and an awaitable release().

    Rate limiting

    Sliding-window rate limiting via the trypema crate, re-exported under distkit::trypema with local, Redis, and hybrid providers.

Built to be predictable

    Safe by default

    #![forbid(unsafe_code)] and no panics in library code. Errors surface through one DistkitError enum.

    Strict or lax, your call

    Choose immediate consistency when accuracy is critical, or buffered writes when throughput matters. Same trait, different guarantees.

    Atomic where it counts

    Strict operations execute Lua scripts so a single Redis round-trip is atomic - no read-modify-write races across instances.

    Self-cleaning background tasks

    Flush and lease-renewal tasks hold Weak references and stop on their own when the owning value is dropped.

Ready to add distkit?

Install the crate, point it at Redis, and reach for the primitive you need.