Back to results for “Error Handling”
Node.js › Background Jobs & Queues

What is job idempotency, and why is it critical when designing background job processing?

Intermediate

Answer

A job is idempotent if running it more than once produces the same end result as running it exactly once -- this matters because most queue systems provide 'at-least-once' delivery guarantees (a job might be redelivered and processed twice if a worker crashes after completing the work but before acknowledging it), so jobs that aren't idempotent (like 'charge the customer $50' run naively) can cause serious bugs like double-charging if ever retried or redelivered.
// Non-idempotent: retrying this job double-charges the customer
async function chargeCustomer(job) { await paymentGateway.charge(job.data.amount); }

// Idempotent: uses a unique idempotency key so a retry is a no-op
async function chargeCustomer(job) {
  await paymentGateway.charge(job.data.amount, { idempotencyKey: job.data.orderId });
}
Real-world example A payment-processing worker includes the order ID as an idempotency key on every charge request sent to the payment gateway, so that if the same job is accidentally processed twice due to a worker crash and retry, the gateway itself recognizes the duplicate request and returns the original charge result instead of billing the customer again.

Common follow-ups: How do you make a database-write job idempotent when the underlying operation isn't naturally idempotent?;What queue-level guarantees (at-most-once vs at-least-once vs exactly-once) does your specific queue technology actually provide?