Skip to content

Discard jobs over max_blocked without the semaphore's lock - #18

Open
jpcamara wants to merge 4 commits into
mainfrom
max-blocked-skip-lock
Open

jpcamara wants to merge 4 commits into
mainfrom
max-blocked-skip-lock

Conversation

@jpcamara

@jpcamara jpcamara commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

A job on a key with no free slot and max_blocked jobs already waiting is discarded. Until now, it took the semaphore's row lock to find that out, and held it until the transaction enqueuing it committed. When many enqueues arrive for one busy key, they wait in line for that lock, one transaction at a time, only to be discarded. In pg_stat_activity this shows as many sessions waiting on Lock:tuple for the semaphore lookup.

What the lock was for

A discarded job counts on the blocked job to run in its place, after the transaction enqueuing it commits. The semaphore's lock guaranteed that: while it was held, the running job couldn't give its slot back and the blocked job couldn't be released. It also made discarded enqueues wait for each other, which nothing needs.

What changes

dispatch first calls blocked_queue_full?. It's one statement that checks that the key's semaphore has no free slot and takes a shared lock (FOR SHARE SKIP LOCKED) on the last of the key's max_blocked blocked jobs. If it gets the lock, the job is discarded without touching the semaphore.

  • Discarded jobs hold the shared lock together, so they don't wait for each other.
  • A blocked job is already skipped for release while it's locked (FOR UPDATE SKIP LOCKED), so it stays blocked until those transactions end.
  • A job that finishes meanwhile can't release the locked blocked job. So each discarding transaction releases it when it ends, by commit or rollback, if a slot is free by then.
  • If the blocked job is being released at that moment, the check doesn't get the lock and the enqueue takes the locked path, as before.
  • Otherwise dispatch goes on as before, and block's locked check still keeps the cap exact while the key fills up.

This applies on PostgreSQL with Active Record 7.2 or later. On MySQL, SQLite and Active Record 7.1, jobs over max_blocked are discarded under the semaphore's lock, as before. Keys without max_blocked, and keys with on_conflict: :discard, don't change.

What to know

  • While enqueues keep being discarded on a key, its blocked job stays locked. A job that finishes then frees the slot without releasing it, and the next enqueue takes the slot. The blocked job runs once there's a moment with no discarding transaction open.
  • If a process dies inside a discarding transaction right after the running job finished, nothing releases the blocked job until the dispatcher's maintenance does, after the key's duration.
  • Enqueues that arrive while the transaction that filled the blocked queue is still open don't see it as full. They take the locked path and wait in line, as before.

Tests

In job_test.rb:

  • a job over the cap is discarded without calling Semaphore.lock;
  • two open transactions each discard a job without waiting for the other;
  • the blocked job isn't released while a discarding transaction is open, and is released when it commits or rolls back with a slot free;
  • it stays blocked after a discarding transaction commits if the slot is still taken;
  • a job whose slot is free still takes it while the blocked queue is full;
  • max_blocked works with a key containing ? and :name.

Full suites, 0 failures: Rails 8.1 on PostgreSQL, SQLite and MySQL (433 runs each); Rails 7.2 on PostgreSQL (the job tests); Rails 7.1 on PostgreSQL (431 runs).

Measured

PostgreSQL 17 on a laptop. 16 processes each run transactions that write a row, enqueue a job on one max_blocked: 1 key, and stay open for a while. Two workers run the jobs, 20 ms each.

Transaction stays open main this branch
5 ms transactions per second 107–108 415–472
enqueue p50 140 ms 1.2–1.3 ms
sessions waiting on Lock:tuple 14.1 8.0–8.9
25 ms transactions per second 31 116
enqueue p50 482 ms 2.9 ms
sessions waiting on Lock:tuple 14.6 9.0

In every run, each job saw every row committed before it started or a later job did, at most one job ran at a time, and nothing was left blocked.

An earlier version of this PR checked without any lock. It was faster (659–722 transactions per second at 5 ms), but a blocked job could run before a discarding transaction committed. The random-sequence comparison against main in the earlier description was run on that version and hasn't been repeated.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF

jpcamara and others added 2 commits November 27, 2026 00:31
When a key has no free slot and already has max_blocked jobs waiting,
a new job on it is discarded. Until now it took the semaphore's row lock
first, twice, and held it while counting the blocked jobs, deleting the
job and committing. Many enqueues on one busy key then waited in line
for that lock only to be discarded.

dispatch now checks for that case first, in one unlocked statement that
reads the semaphore and the blocked jobs together. The job is discarded
in exactly the cases it was before. Skipping the lock is safe because a
blocked job seen by the check hasn't started, so it runs after this
enqueue.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF
The check put the blocked jobs' subquery, with the key as a literal, into
a SQL string that was then scanned for ? placeholders, so a concurrency
key with a ? in it failed to enqueue. It now uses an EXISTS for the
max_blocked-th blocked job, built with Arel, which keeps the key a bound
value. With max_blocked: 0, only the slot is checked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF
jpcamara and others added 2 commits December 2, 2026 15:31
…commits

A job discarded for being over max_blocked counts on the blocked job to
run in its place, after the transaction enqueuing it commits. The
semaphore's lock used to guarantee that, since it was held until then.
The check before locking didn't: the blocked job could be released and
run while that transaction was still open, and miss its changes.

The check now takes a shared lock on the last blocked job. Discarded
jobs hold it together, so they still don't wait for each other, and a
blocked job is skipped for release while it's locked. Since a job that
finishes meanwhile can't release it, it's released when the transaction
ends, if a slot is free by then.

This needs PostgreSQL and Active Record 7.2. Elsewhere jobs over
max_blocked are discarded under the semaphore's lock, as before.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF
@jpcamara jpcamara changed the title Discard jobs over max_blocked before locking the semaphore Discard jobs over max_blocked without the semaphore's lock Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant