StrictInstanceAwareCounter
StrictInstanceAwareCounter is immediately consistent with Redis on every call. set and del bump a per-key epoch, which causes any stale instance to reset its stored count on its next operation - this is what prevents double-counting when the global total is reset out from under an instance.

Construct
use distkit::DistkitRedisKey;
use distkit::icounter::{
InstanceAwareCounterTrait,
StrictInstanceAwareCounter, StrictInstanceAwareCounterOptions,
};
let prefix = DistkitRedisKey::try_from("my_app".to_string())?;
let counter = StrictInstanceAwareCounter::new(
StrictInstanceAwareCounterOptions::new(prefix, conn),
);
StrictInstanceAwareCounterOptions::new defaults dead_instance_threshold_ms to 30_000 (30 s). Set the field directly to change it.
Operations
Every call returns (cumulative, instance_count):
let key = DistkitRedisKey::try_from("connections".to_string())?;
// Add to / subtract from this instance's contribution.
let (total, mine) = counter.inc(&key, 5).await?;
let (total, mine) = counter.dec(&key, 2).await?;
// Read without modifying.
let (total, mine) = counter.get(&key).await?;
// Set just this instance's slice (no epoch bump).
let (total, mine) = counter.set_on_instance(&key, 10).await?;
// Set the global total and bump the epoch.
let (total, mine) = counter.set(&key, 100).await?;
// Remove only this instance's contribution.
let (total, removed) = counter.del_on_instance(&key).await?;
// Delete the key globally and bump the epoch.
let (old_total, _) = counter.del(&key).await?;
Dead-instance cleanup
Each instance sends a heartbeat on every operation. If a process silently dies, surviving instances remove its contribution the next time any of them touches the same key:
let opts = |conn| StrictInstanceAwareCounterOptions {
prefix: prefix.clone(),
connection_manager: conn,
dead_instance_threshold_ms: 30_000, // 30 s
cleanup_interval: None,
};
let server_a = StrictInstanceAwareCounter::new(opts(conn1));
let server_b = StrictInstanceAwareCounter::new(opts(conn2));
server_a.inc(&key, 10).await?; // cumulative = 10
server_b.inc(&key, 5).await?; // cumulative = 15
// server_a goes offline. After 30 s, server_b's next call removes
// server_a's slice automatically.
let (total, _) = server_b.get(&key).await?; // total = 5 once cleaned up
Cleanup is lazy by default: it happens on the next operation, not on a timer. Set cleanup_interval on the options to also sweep on a timer: a background task reaps dead instances and deletes records that no instance holds anymore, so a quiet deployment converges without traffic.
A record that reaches 0 is deleted, since a missing record reads as 0. An instance's slice field goes when that slice reaches 0; the shared record goes only when no registered instance holds a field for the key, so a key held at -1 and +1 keeps its records.
Each call has a limit. One call removes dead instances until it has read approximately 10,000 of their counts. If more dead instances stay, the next calls remove them. Thus, a large backlog does not block Redis in one call.
The work for one dead instance increases with the number of keys that the instance wrote. The number of keys in the counter has no effect.

