Databases · Senior · Question 11 of 75
How can you build a job queue on PostgreSQL that many workers poll safely?
SELECT id FROM jobs
WHERE status = 'queued'
ORDER BY id
FOR UPDATE SKIP LOCKED
LIMIT 1;- ✓Correct answer:
FOR UPDATE SKIP LOCKEDlets each worker grab a different unlocked row without waiting for others - 2Each worker reads all jobs and picks one at random
- 3PostgreSQL can't be used for queues
Explanation
Libraries like pg-boss and graphile-worker use this. It works well for moderate throughput and keeps jobs transactional with your data.