distkit
A toolkit of distributed systems primitives for Rust, backed by Redis.
What is distkit?
distkit provides building blocks for distributed applications. It currently ships two counter implementations that share a common async trait, letting you choose between immediate consistency and high-throughput eventual consistency depending on your use case.
Features
- StrictCounter -- every operation executes a Redis Lua script atomically. Reads always reflect the latest write. Best for billing, inventory, or anything where accuracy is critical.
- LaxCounter -- buffers increments in memory and flushes to Redis every ~20 ms. Sub-microsecond latency on the hot path. Best for analytics, rate limiting, and high-throughput metrics.
- Shared trait -- both counters implement
CounterTrait, so generic code works with either. - Safe by default --
#![forbid(unsafe_code)], no panics in library code.
Installation
Or add to Cargo.toml:
[]
= "0.1"
distkit requires a running Redis instance (5.0+ for Lua script support).
Quick start
use ;
async
Counter types
StrictCounter
Every call is a single Redis round-trip executing an atomic Lua script. The counter value is always authoritative.
let key = try_from?;
counter.strict.inc.await?; // HINCRBY via Lua
counter.strict.set.await?; // HSET via Lua
counter.strict.del.await?; // HDEL, returns old value
counter.strict.clear.await?; // DEL on the hash
LaxCounter
Writes are buffered in a local DashMap and flushed to Redis in batched
pipelines every allowed_lag (default 20 ms). Reads return the local view
(remote_total + pending_delta), which is always consistent within the same
process.
let key = try_from?;
counter.lax.inc.await?; // local atomic add, sub-microsecond
let val = counter.lax.get.await?; // reads local state, no Redis hit
A background Tokio task handles flushing. It holds a Weak reference to the
counter, so it stops automatically when the counter is dropped.
Development
Prerequisites
- Rust (latest stable)
- Docker (for the test Redis instance)
Commands
Tests and benchmarks read the REDIS_URL environment variable
(default: redis://127.0.0.1:16379/). The make targets handle this
automatically.
License
MIT