Claiming Work from a Shared Database Queue

A shared queue needs an atomic claim decision so two workers do not both treat one pending row as their exclusive work.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace an ordinary claim
  2. Track recovery
  3. Keep two workers from claiming the same active item
  4. Define recovery without confusing ownership

Trace an ordinary claim

Two workers can see the same pending row. A database-supported lock or state-checked update can make one claim succeed and the other choose again. A scoped update expresses expected current state.

Track recovery

If the successful worker fails, the queue policy should say when another may reclaim the item. Work queue invariants describe the resulting states.[1]

Keep two workers from claiming the same active item

Worker A and B both see job seven as ready. A claim can use a guarded update or a database locking design so only one takes ownership. The other can find another job or wait according to policy. See per-key work queues.

Ready jobs: 7,8
A claims 7 under guard
B's stale claim on 7 cannot succeed under same guard
B can claim 8 when eligible

Define recovery without confusing ownership

A crashed worker can leave a claim behind. When reassigning, use an identity or version that lets completion verify it still owns the job. Test concurrent starts and a late completion.

References

  1. PostgreSQL Global Development Group, Concurrency Control ↩