The Scheduled Workflow That Ran Twice: Overlapping Runs in n8n
Описание и применение
В n8n планировщик запускает работу каждый час, но без блокировки предыдущий запуск может ещё работать, что приводит к дублированию данных. Решение – атомарный блокировочный запрос в PostgreSQL с истекающим сроком аренды, который гарантирует, что только один запуск выполняется одновременно, а остальные получают уведомление о пропуске.
Задача
A scheduled n8n workflow ran twice concurrently, causing duplicate updates and duplicate emails.
Результат
The lock mechanism prevented overlapping runs, eliminated duplicate updates, and provided transparent alerts for skipped or reclaimed executions.
Рабочий процесс
- 1. Acquire lock atomically in Postgres; if locked, skip run with alert.
- 2. If lock acquired, proceed with data pull, LLM enrichment, and write.
- 3. Release lock at end only if token matches, in always‑run path.
- 4. Log outcomes: acquired, already locked, stale lease reclaimed.
Доказательства
The schedule is a clock, not a queue. It will happily start run N+1 while run N is still running.
Acquiring the lock is a single atomic statement: update the row only if it is unlocked, or if its lease has expired.
The release belongs in the workflow's always‑run path, the same place error handling lives, so a failed run frees the lock immediately and the next scheduled run can proceed on time.
- Дата записи: 2026-11-02T03:43:45.093Z