agentsclimarketplace

Distributed lock mongodb

Skill kjuhwa/skills-hub/skills/backend/distributed-lock-mongodb

Self-correcting knowledge corpus for Claude Code — 9 stable shape clusters, bias-correction pipeline baked into contribution flow. 47 papers, 45 techniques, 1.1k skills.

Install
npx -y skills add kjuhwa/skills-hub --skill distributed-lock-mongodb

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its author says it does

Copied from the file, not written here

MongoDB findAndModify-based distributed lock with TTL expiry, owner tagging, and retry loop for multi-instance Spring Boot services.

SKILL.md

1.8 KB, 368 tokens by cl100k_base, as published. Nobody here has run it

Distributed Lock over MongoDB

Shape

A DistributedLockService exposes tryLock(key, ttl) / release(key, owner).

Implementation uses atomic findAndModify on a single locks collection:

  • Document: { _id: key, owner: "<node>:<thread>", expiresAt: Date }
  • Acquire: findAndModify(query = { _id: key, $or: [{expiresAt: {$lt: now}}, {_id: {$exists: false}}] }, update = { owner, expiresAt: now + ttl }, upsert: true)
  • Release: deleteOne({ _id: key, owner: self }) (never blind delete)
  • Retry: loop with 100 ms sleep up to configured timeout before giving up

Steps

  1. Create collection locks with TTL index on expiresAt as a safety net (not authoritative — acquire query checks expiry).
  2. Implement tryLock using Mongo atomic upsert; return false when a different non-expired owner holds the key.
  3. Always stamp owner = "<hostname>:<threadName>" so release is idempotent and crash-safe.
  4. Wrap critical section in try { if (tryLock) ... } finally { release }.
  5. For retry-until-timeout semantics, add a thin loop around tryLock rather than blocking inside the lock method.

Counter / Caveats

  • NTP drift across nodes skews TTL comparison — keep TTLs generous (seconds, not ms).
  • Do not use a TTL index as the only expiry mechanism; the background deleter runs every 60 s.
  • Prefer Redis/Redisson if you need fair queuing or pub-sub release notifications.

What ships with it: 1 file

711 B alongside SKILL.md

Keep looking

Skills are one crate of 327,069. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.