NoNoncense.MachineId.Strategy.RedisLease (NoNoncense v2.0.0)

Copy Markdown View Source

Strategy to acquire a machine ID lease from Redis, coordinating uniqueness across nodes through a single Redis hash.

Each machine ID (0..511) is a field in the hash; a node acquires the lowest free ID and holds it by keeping that field's value (a random token) alive with a TTL, renewing it periodically. Requires Redis 8+ or Valkey 9+ for HSETEX's conditional (FNX/FXX) and per-field TTL support.

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:

    1. Pass the simple connection name or pid directly:
      with_connection: MyApp.Redix
    2. To support connection pools, pass a function that calls its argument function with a connection:
      with_connection: fn fun -> :poolboy.transaction(MyApp.RedixPool, fun) end
  • :key - the Redis hash key to store leases under (default "no_noncense_leases")

Summary

Types

opt()

@type opt() :: {:with_connection, with_connection()} | {:key, binary()}

with_connection()

@type with_connection() ::
  ((Redix.connection() -> any()) -> any()) | Redix.connection()