← Meridian
Recipe

Distributed Locks

Patterns for mutual exclusion across processes, containers, and availability zones — built on Meridian's atomic primitives.

Redlock Algorithm

Acquire a lock on a majority of N independent Meridian nodes with identical keys and random nonces. Each node grants the lock with a TTL. If the quorum is reached within the validity window, the lock is held. Release via Lua script that checks the nonce before deleting — preventing accidental unlock by expired holders.

SET lock:resource NX PX 30000

Fencing Tokens

Every lock acquisition returns a monotonically increasing token. Downstream services reject operations whose token is less than the last seen value. This prevents split-brain writes when a paused process resumes after its lock has expired and been re-acquired by another instance.

Lease-Based Heartbeating

Holders extend their lease by refreshing the TTL at intervals shorter than the lock timeout. If the holder crashes, the lock auto-expires. Combine with a watchdog thread that voluntarily releases the lock if the application-level health check fails — graceful degradation over stale ownership.

Anti-Deadlock Guardrails

  • Always set a TTL — never create unbounded locks.
  • Use random backoff with jitter on contention.
  • Cap retry attempts; escalate to a dead-letter queue.
  • Monitor lock-hold duration percentiles for drift.