Skip to content
Skills Library
Programming · Building & coding

distributed-locks

/distributed-locks

Use leases with fencing tokens for mutual exclusion across machines, or restructure to need no lock at all. Use when serializing distributed work or reviewing lock code that must not double-execute.

Repository
AI-SKILLS ↗
Licence
MIT
Last repository change
05.09.2026

Assessment and security

90/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content25 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 05.09.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Use leases with fencing tokens for mutual exclusion across machines, or restructure to need no lock at all. Use when serializing distributed work or reviewing lock code that must not double-execute.

Original author description

Use leases with fencing tokens for mutual exclusion across machines, or restructure to need no lock at all. Use when serializing distributed work or reviewing lock code that must not double-execute.

How it works

  1. Method
  2. Boundaries
Original SKILL.md headings · 50 lines, 374 words
SKILL.md preview
---
name: distributed-locks
description: Use leases with fencing tokens for mutual exclusion across machines, or restructure to need no lock at all. Use when serializing distributed work or reviewing lock code that must not double-execute.
---

# Distributed locks

A distributed lock is a lease plus a fencing token. Without the token it
is a probabilistic hint; with it, even an expired holder cannot corrupt
anything.

## Method

1. **Try to delete the lock first.** Unique constraints make duplicate
   inserts safe; idempotency keys serialize retries; queue partition
   keys serialize per entity; SKIP LOCKED hands out work items without
   coordination. Most "we need a distributed lock" problems are one of
   these in disguise.
2. **Locks are leases.** A lock without expiry plus a crashed holder is
   a permanent outage. Acquire with a TTL (store-side expiry:
   `SET key owner NX PX ttl`, or an etcd/ZooKeeper lease bound to a
   session), sized at several times the expected critical section.
3. **The expiry gap is real: fence it.** The holder can pause (GC,
   network) past its TTL and resume believing it still holds the lock,
   while a new holder proceeds. Fix: the lock service issues a
   monotonically increasing token on each acquisition; every protected
   resource rejects writes carrying a token lower than the highest seen.
   Without fencing, correctness depends on timing luck.
4. **Heartbeat long work, checkpoint longer work.** Renew the lease from
   the working process at TTL/3; if renewal fails, stop mutating
   immediately, because someone else may hold the lock. Work longer than
   a few TTLs should checkpoint so a successor resumes rather than
   restarts.
5. **Release safely, verify ownership.** Release only your own lock
   (compare owner id atomically with delete: Lua script in Redis,
   version check in a database row). Blind DELETE releases the next
   holder's lock.
6. **Choose the store by guarantee needed.** Efficiency locks (avoid
   duplicate cache fills, mostly-harmless double work): single Redis
   with NX+TTL is fine. Correctness locks (must never double-execute):
   a consensus-backed store (etcd, ZooKeeper) or your primary database
   row locks, always with fencing at the resource.

## Boundaries

- Redlock-style multi-node timing schemes still lack fencing; treat
  them as efficiency locks regardless of marketing.
- If the protected resource cannot check a token (a third-party API),
  no lock protocol fully protects it; make the operation idempotent at
  that boundary instead.
- Lock-per-request patterns serialize your whole system; if everything
  needs the same lock, the design needs sharding, not a better lock
  (see leader-election for the one-active-node case).

Versions

The database has no other skills named distributed-locks.

Database snapshot: 13.11.2026 · package version c90a95e4efe9 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 95backend-distributed-locking
    Use this skill when the user says 'distributed lock', 'Redis lock', 'Redlock', 'ZooKeeper lock', 'advisory lock', 'PostgreSQL lock', 'pg_advisory_lock', 'lease', 'distributed mutex', 'lock timeouts', 'fencing token'. This skill implements distributed locking using Redis Redlock,…
  • 90ai-core/locks
    LockStore, InMemoryLockStore, LocksCapability and withLocks for multi-instance coordination in TanStack AI. Ships in @tanstack/ai — NOT in @tanstack/ai-persistence. Separate from AIPersistence state stores — not a stores key, not composable. InMemoryLockStore vs a distributed…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.