r/codereview • • 17h ago

PostgreSQL persistent task queue: concurrency, idempotency and restart recovery

Hello everyone,

I'm extending an existing Python application that uses PostgreSQL and Supabase for multichain blockchain research.

We have implemented an offline candidate queue with duplicate prevention, retry limits, expiration and restart recovery.

During testing, we identified a potential issue where a stale processing result could affect a newly queued instance of the same candidate.

I would appreciate technical guidance on:

  • Using FOR UPDATE SKIP LOCKED for concurrent processing.
  • Preventing stale worker results through unique claim identifiers.
  • Maintaining idempotency and reliable restart recovery.
  • Choosing appropriate indexes and expiration strategies.

Would a dedicated PostgreSQL queue table be the recommended approach?

Any technical feedback or suggestions would be appreciated. Thank you.

1 Upvotes

6 comments sorted by

1

u/buzziebee 3h ago

Have a look at how things like pg-boss and dbos work if you're looking for inspiration to help you build your own solution. There's lots of prior art but it's a complicated topic so takes a lot to get it all right.

1

u/fervent7 2h ago

Thank you for the recommendations. I'll review pg-boss and DBOS, particularly their approaches to worker ownership, stale-result prevention, idempotency and durable recovery.

We're extending an existing Python and PostgreSQL architecture, so understanding established implementation patterns would be valuable before introducing additional dependencies.

Are there any specific implementation details or failure scenarios in either project that you would recommend focusing on first?

1

u/SingsheimKeely 1h ago

for the stale worker problem, you just need a fencing token or claim UUID on the row when the worker grabs it. When writing the result back, include that token in the WHERE clause so the update simply fails with 0 rows affected if another worker re-claimed it after a timeout

1

u/fervent7 33m ago

Thank you, this aligns with the fencing-token approach recommended by several other developers.

We're planning to verify that the final database transaction checks the job instance, current claim UUID, processing state and lease validity before allowing any observation or event writes.

We'll also test worker reassignment while the original worker is still running, along with duplicate-result prevention and recovery after a lost commit response.

Would you recommend a monotonically increasing fencing token over a unique claim UUID for this particular PostgreSQL-based implementation?

I appreciate your technical input.

1

u/Ordinary_Hall_6638 16h ago

Maybe pgmq will be enough for your tasks?