Strategy to acquire a machine ID lease from Redis, coordinating uniqueness across nodes through a single Redis hash.
A node prefers the lowest eligible machine ID (0..511) and holds it through a
token-protected lease with a TTL, renewing it periodically.
The strategy protects ID reuse between nodes against clock skew by recording the lease holder's local expiry and requiring the successor's local clock to pass that cutoff.
Requires Redis 8+ or Valkey 9+ for HSETEX's conditional (FNX/FXX) and per-field TTL support. Uses MULTI/EXEC, but no Lua scripts or server functions.
Configure the strategy when starting NoNoncense.MachineId, after your Redix instance or pool:
children = [
...
MyApp.Redix,
{NoNoncense.MachineId,
strategy: NoNoncense.MachineId.Strategy.RedisLease,
strategy_opts: [with_connection: MyApp.Redix],
instances: [[base_key: System.fetch_env!("BASE_KEY")]]}
]Options
:with_connection(required) supports two forms:- Pass the simple connection name or pid directly:
with_connection: MyApp.Redix - To support connection pools, pass a function that calls its argument function with a connection:
(or use the simpler poolboy transaction syntax)with_connection: fn fun -> conn = :poolboy.checkout(MyApp.RedixPool) try do fun.(conn) after :poolboy.checkin(MyApp.RedixPool, conn) end endwith_connection: &:poolboy.transaction(MyApp.RedixPool, &1)
- Pass the simple connection name or pid directly:
:key- the Redis hash key to store leases under (default"no_noncense_leases")
Summary
Types
@type opt() :: {:with_connection, with_connection()} | {:key, binary()}
@type with_connection() :: ((Redix.connection() -> any()) -> any()) | Redix.connection()