Prompt
Troubleshoot duplicate background jobs being processed twice
Latest observation
Oct 24, 2026
OpenAI APIWeb search: off
Here’s a practical checklist to troubleshoot duplicate background jobs being processed twice. This applies to systems like Sidekiq, Celery, Hangfire, Resque, BullMQ, Quartz, custom queue workers, etc.
1) First determine: are jobs duplicated at enqueue time or at processing time?
There are two different problems:
- Duplicate enqueued jobs: the job appears twice in the queue/store.
- Duplicate processing: one job is handled twice by workers, often due to retries, visibility timeouts, or acknowledgment failures.
Check:
- Queue size / job table / Redis entries
- Worker logs with job IDs
- Whether both runs share the same job ID or have different IDs
2) Common causes
A. The job is being enqueued more than once
Typical reasons:
- Request retries or double-submits from the UI/API
- Event handler fired twice
- Message consumer re-publishing on failure
- Scheduled job registered in multiple places
- Deploy/startup code enqueues jobs every time an app boots
What to inspect:
- Code paths calling
enqueue/perform_later/delay/addJob - Logs around the enqueue event
- Idempotency of upstream requests
B. Workers are processing the same job twice because of ack/visibility issues
Typical reasons:
- Worker crashes before acknowledging completion
- Queue uses visibility timeout and job becomes visible again
- Heartbeat/lock expired
- Worker timeout shorter than job runtime
- Network issues between worker and broker
- Prefetch + retry behavior causing re-delivery
Look for:
- “job timed out”, “lost lock”, “ack failed”, “requeued”, “visibility timeout expired”
- Job duration > worker timeout or broker visibility timeout
- Worker restarts during execution
C. Multiple worker instances are not coordinated properly
Typical reasons:
- No distributed lock / unique constraint
- Cron/scheduler runs on every app instance
- Same job scheduled by multiple pods/servers
- Race condition when multiple consumers select the same record
D. Retries are mistaken for duplicates
A failed job may be retried automatically, and if the first attempt partially succeeded, it can look like duplication.
Check:
- Retry policy
- Dead-letter queues
- Side effects that happen before failure
- Whether the handler is idempotent
3) What to log for diagnosis
Add or verify these fields in logs:
- Job ID
- Correlation/request ID
- Enqueue timestamp
- Start timestamp
- Worker hostname/pod
- Attempt number / retry count
- Payload fingerprint (hash of job arguments)
- Trace/transaction ID if available
This helps answer:
- Same job ID processed twice?
- Different job IDs with identical payloads?
- Two workers picking up the same job?
- Retry vs true duplicate?
4) Quick checks by system type
Redis-based queues
- Check
visibility timeout/reservedjob behavior - Confirm workers
ACKcorrectly - Verify job locks are not expiring early
- Inspect whether
retryre-enqueues duplicates
Database-backed queues
- Ensure row locking is correct (
SELECT ... FOR UPDATE SKIP LOCKED, etc.) - Confirm transactions commit after enqueue
- Check for multiple schedulers running the same task
- Make sure a unique constraint prevents double inserts
Kafka / pub-sub style systems
- At-least-once delivery means duplicates are possible
- Consumer offset commits may happen after processing
- Implement idempotency and dedupe keys
5) Immediate mitigations
Even before root cause is fixed:
- Make the job idempotent
- Use unique keys in your DB
- Upsert instead of insert
- Check “already processed” state before side effects
- Deduplicate at enqueue time
- Unique job keys / locks
- “enqueue if absent” semantics
- Increase visibility/lock timeout
- Ensure it exceeds worst-case job runtime
- Prevent multiple schedulers
- Run cron/scheduler in one leader only
- Add circuit breakers / retries with backoff
- Record processed job IDs
- Temporary dedupe store (Redis/DB) if needed
6) Common anti-patterns
- Doing non-idempotent side effects first, then marking job complete
- Assuming “exactly once” delivery from a system that only guarantees at-least-once
- Enqueuing jobs from every web server instance startup
- Not handling worker restarts or timeouts
- Ignoring duplicate events from upstream services
7) A simple debugging approach
- Pick one duplicate incident.
- Compare the two executions:
- Same job ID or different?
- Same payload hash?
- Same enqueue timestamp?
- Same worker or different workers?
- Trace back to enqueue source.
- Check worker ack/retry logs.
- Check runtime vs timeout/visibility settings.
- Verify whether the system is designed for at-least-once delivery.
8) Best long-term fix
The safest approach is:
- Assume duplicates will happen
- Make processing idempotent
- Use dedupe keys/locks where practical
- Monitor for repeated job IDs / payload hashes
If you want, I can also help you debug a specific stack like Sidekiq, Celery, Hangfire, AWS SQS, RabbitMQ, or a custom queue.