Menu

1 / 2

Make scheduled job handlers idempotent: safe to run twice

DEV Community: backend·delay.run·10 days ago
#tweDKXIy
#devtobackend#devto#dev#jobs#work
Reading 0:00
15s threshold

Any system that calls your endpoint on a schedule, or retries it after a failure, will sooner or later call it twice for the same piece of work. A network timeout after your handler finished, a retry after a slow response, two servers firing the same cron: each of these runs the job again. If your handler sends an email or charges a card, "again" is a real problem. The fix is to make the handler idempotent: running it twice has the same effect as running it once.

Why duplicates are unavoidable

Consider a scheduler calling POST /jobs/send-invoices . Your handler takes 35 seconds. The caller's timeout is 30 seconds, so it records a failure and retries. Your first run finishes anyway. Now the invoices go out twice.

You can't fix this purely on the caller's side.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.