Prevent n8n workflows from processing the same job twice after a worker failure

I have a self-hosted n8n setup where workflows process jobs stored in PostgreSQL. I’m using multiple workers, and sometimes a worker crashes or loses its connection while processing a job.
The problem is that the job can remain in processing even though the worker never completed it. If I simply retry all failed jobs, I’m worried about two workers processing the same job simultaneously.
My current table looks like:

CREATE TABLE jobs (
    id BIGSERIAL PRIMARY KEY,
    status TEXT NOT NULL,
    locked_at TIMESTAMP,
    worker_id TEXT,
    updated_at TIMESTAMP DEFAULT NOW()
);

I’m considering claiming jobs using:

UPDATE jobs
SET
    status = 'processing',
    locked_at = NOW(),
    worker_id = $1
WHERE id = $2
  AND status = 'pending'
RETURNING *;

Then periodically resetting jobs where locked_at is older than a certain threshold.

What would be the safest production pattern for job claiming, worker failures, and retries in n8n + PostgreSQL?
Would SELECT … FOR UPDATE SKIP LOCKED be better here, and how would you prevent a recovered/stale worker from updating a job that has already been picked up by another worker?

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Gabby_Timothy

For this case, I’d use FOR UPDATE SKIP LOCKED + a lease/lock timeout + a worker token.

The important part is making sure a crashed worker doesn’t permanently hold a job, while also preventing an old worker from modifying a job that another worker has already reclaimed.

Claim jobs atomically

WITH job AS (
SELECT id
FROM jobs
WHERE status = ‘pending’
ORDER BY id
FOR UPDATE SKIP LOCKED
LIMIT 1
)
UPDATE jobs
SET
status = ‘processing’,
locked_at = NOW(),
worker_id = $1,
updated_at = NOW()
FROM job
WHERE jobs.id = job.id
RETURNING jobs.*;

SKIP LOCKED is useful with multiple workers because one worker won’t wait for another worker’s locked row.

Add a lease timeout

If a worker crashes, the job can eventually become available again: UPDATE jobs
SET
status = ‘pending’,
worker_id = NULL,
locked_at = NULL
WHERE status = ‘processing’
AND locked_at < NOW() - INTERVAL ‘10 minutes’;

The timeout should be longer than the normal processing time to avoid unnecessary retries.

Protect against stale workers

I wouldn’t let a worker update a job using only id.

Use the worker token as part of the condition: UPDATE jobs
SET
status = ‘completed’,
updated_at = NOW()
WHERE id = $1
AND status = ‘processing’
AND worker_id = $2;

If the worker lost its lease and another worker reclaimed the job, this update affects 0 rows, so the stale worker cannot overwrite the newer worker’s result.

Recommended pattern

Claim job → process → heartbeat/extend lease if necessary
→ complete → retry expired jobs

I’d keep the actual locking logic in PostgreSQL and let n8n handle the orchestration.

The key idea is: don’t trust the worker to know whether it still owns the job the database should decide.

Hi @Gabby_Timothy
In queue mode the duplicate can come from n8n itself, before any of your claim logic runs. Each worker holds a Redis lease on the execution for QUEUE_WORKER_LOCK_DURATION (60000 ms) and renews it every QUEUE_WORKER_LOCK_RENEW_TIME. If a worker crashes, or is just too busy to renew in time, the execution is flagged as stalled and re-delivered to another worker, which runs the workflow again from the trigger. On n8n 1.x that replay happens once by default, so switch it off and set the lease above your slowest job:

QUEUE_WORKER_MAX_STALLED_COUNT=0
QUEUE_WORKER_LOCK_DURATION=300000

On n8n 2.0 the variable and the Bull stalled retry are both gone, so remove the first line after upgrading and drive retries from your own table instead.