#826

Build a job queue that scales out cleanly

There's a job queue in the database — table jobs with status pending for the un-run rows. Five workers look at it concurrently: each wants to grab the oldest job and mark it as "in progress". A vanilla row-level lock with a limit makes all five workers pile up on the same row and serialize. The cure is to skip the rows that are already locked and take the next free one: every worker gets its own job and parallelism works without collisions. jobs(id SERIAL, status, payload) holds four pending rows (a, b, c, d). Grab one pending job from jobs with the smallest id, flip it to running — the other three stay pending.

Your query result will appear here