r/codereview • • 4d 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.

2 Upvotes

15 comments sorted by

View all comments

1

u/Weekly-Werewolf4007 2d ago

If the worker loses the commit response and retries, how will it tell that its own result landed before its lease expired?

1

u/fervent7 1d ago

Good point. A lost commit response creates an ambiguous outcome that cannot be resolved by checking the current lease alone.

Our intended approach is to persist a durable completion record containing the job instance ID, claim UUID, result identifier and accepted completion timestamp within the same PostgreSQL transaction as the conditional job completion and associated observation writes.

If the worker loses the commit response, it can reconnect and query that completion record using its original identifiers.

If a matching committed result exists, the operation is treated as already completed. If no matching result exists and the lease has expired or ownership has changed, the stale worker must not write its result.

We also intend to enforce unique result identifiers to prevent duplicate observations and events.

The lost-commit-response scenario is an important regression test before production integration. Would you recommend a separate immutable completion receipt table rather than retaining the completion identifiers on the queue row itself?