by Nican · 3y ago · 0 comments
Using a transaction to retrieve an item from the queue, and locking the row using "SELECT FOR UPDATE" and "SKIP LOCKED". Such that the row gets locked on read, and several workers can read from the table at the same time. Within the same transaction, other work is done, and everything gets committed to the database as a single atomic operation.
CockroachDB (a consensus/raft distributed database) recently added supported for SKIP LOCKED, but I still have yet to work on this idea.
Of course there are other concerns with using a database as a queue (mostly at high throughput) but for most cases it will work well.
I guess everyone has to make this mistake once in their career.
Funny enough, when I searched for “database as a queue”, my own comment from four years ago came up as the fourth result.