fix(poller): use SKIP LOCKED for horizontal scalability#82
Draft
nicolasburtey wants to merge 1 commit into
Draft
fix(poller): use SKIP LOCKED for horizontal scalability#82nicolasburtey wants to merge 1 commit into
nicolasburtey wants to merge 1 commit into
Conversation
The poll_jobs query uses FOR UPDATE to claim pending jobs, but without SKIP LOCKED, concurrent pollers block on each other's row locks instead of claiming non-overlapping job sets. This is the standard Postgres pattern for competing consumers and is critical for horizontal scaling. Without SKIP LOCKED, adding more poller instances increases lock contention rather than throughput — pollers serialize on overlapping candidate rows. With SKIP LOCKED, each poller skips rows already locked by another poller and claims the next available jobs, enabling true parallel consumption. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SKIP LOCKEDto theFOR UPDATEclause in the job polling queryProblem
Immediate: pollers block each other
The
poll_jobsquery usesFOR UPDATE OF jeto atomically claim pending jobs. WithoutSKIP LOCKED, concurrent poller instances block on each other's row locks instead of claiming non-overlapping job sets. Adding more poller replicas increases lock contention rather than throughput.Deeper: permanent jobs pin slots to the primary forever
Several downstream consumers (lana-bank) use the job crate for permanent daemon-style jobs — infinite
loop {}insiderun()that only exit on shutdown:fetch-provider-priceloop { fetch(); sleep(300s) }self-custody-balance-syncloop { sync(); sleep(60s) }eod-process-managerloop { run phases; await children }sync-reportsloop { poll dagster; sleep(10s) }Each of these holds a
CurrentJob(which owns aPgPoolclone) and counts against theJobTrackerrunning count. Combined withmin_jobs = 30as the polling threshold, a handful of permanent jobs permanently consume slots.The keep-alive handler writes
UPDATE job_executions SET alive_at = ... WHERE poller_instance_id = ...everyjob_lost_interval / 4(75s by default). The lost-job detector reclaims jobs whosealive_athas drifted. Both are writes tojob_executions, so:job_executionsThis means horizontal scaling requires more primaries or partitioned write capacity, not just read replicas.
SKIP LOCKEDis the minimum fix to let multiple writers coexist efficiently.Solution
FOR UPDATE OF je SKIP LOCKEDis the standard Postgres pattern for competing consumers:This is safe because:
runningby the time B's transaction could see them anywayqueue_idordering constraint in thecandidatesCTE still ensures per-queue serializationmin_waitCTE operates on theeligibleCTE (pending jobs only), so skipped-but-locked rows don't affect wait timeWhat this doesn't fix (future work)
min_jobs, or use a separate "daemon" pool)PgPoolacross HTTP + jobs + infra; a dedicated job pool would isolate loadTest plan
🤖 Generated with Claude Code