Locks

One worker per task

Pick one agent or function for each task, renew its ownership while it works, and let the others move on.

locks
# three agents reach for the same ticketagent-a  withLock('ticket:4821')  acquired · fencing 17agent-b  withLock('ticket:4821')  acquired: false · takes ticket:4822agent-c  withLock('ticket:4821')  acquired: false · takes ticket:4823✓ released · ticket 4821 handling complete

What it does

Stop duplicate work

Name a lock for the task. One caller gets it, and the rest skip it.

  • Leases

    If a holder dies, its lock frees itself when the TTL ends.

  • Renewal

    withLock renews while your callback runs.

  • Fencing

    A rising token lets your data reject stale writes.

What you need

  • A Volcano account

    Locks are built in.

  • A service key

    Locks run server-side. Browsers can’t acquire them.

  • Volcano SDK

    JavaScript, Python or Ruby.

Try it

Three steps from zero.

  1. Claim a task

    A held lock returns acquired: false right away.

    Claim

    JavaScript
    const result = await volcano.locks.withLock( `ticket:${ticket.id}`, { ttl: 60 }, ({ signal, lease }) => answerTicket(ticket, lease.fencingToken, signal), );
  2. Skip what is taken

    Move on to the next task.

    Skip

    JavaScript
    if (result.error) throw result.error; if (result.acquired) return result.data; // acquired: false, so try the next ticket
  3. Fence your writes

    A stalled worker with an older token can’t overwrite newer work.

    Fence

    SQL
    update job_state set cursor = $1, fencing_token = $2 where id = $3 and fencing_token <= $2;

Check it works

  • Run two workers on one ticket. Only one gets the lock.
  • Kill the holder mid-task. Another worker takes over when the lease runs out.
  • Replay a write with an old token. It updates zero rows.

Works with

Questions

Does a distributed lock wait when another worker holds it?

No. JavaScript returns acquired: false and Python raises ConflictError. Skip the task, try another key, or retry later.

What happens to a distributed lock when a worker crashes?

Its lease expires when renewals stop. Another caller can take the lock one second after expiry; a shorter TTL means a faster takeover.

What is a fencing token?

A number that rises with each new holder of a lock. Store the highest one you’ve seen and reject writes carrying a lower one.

More answers in the docs

It will blow your mind!

Pun intended. Build, deploy & scale on the Volcano platform for free.

Start building