Hard20 minDistributed Systems
UpdatedAug 6, 2026
Edit

NATS: Durable Image Jobs

Question Variations

  • "What happens if an image worker dies before acknowledging?"
  • "When would a pull consumer be preferable to a push consumer?"
  • "How should a JetStream worker make image processing idempotent?"

Why This Is Asked

Users upload images faster than processors can resize them, and workers can restart during processing. This tests whether a candidate can configure a JetStream work queue with durable consumers, acknowledgements, redelivery, and monitoring.

Key Concepts

  • Work-queue retention: Messages are retained until a consumer acknowledges their successful processing.
  • Durable consumer: Progress survives worker restarts and supports horizontal worker groups.
  • Acknowledgement policy: Ack after storage of the resized image, not on receipt.
  • Backlog operations: Monitor pending count and redeliveries; scale workers without losing ownership semantics.

Question Variations

  • “What happens if an image worker dies before acknowledging?”
  • “When would a pull consumer be preferable to a push consumer?”
  • “How should a JetStream worker make image processing idempotent?”

Answers by Technology

+ Add Variant
NATSImprove this answer ✏️

Expected Answer

Use a JetStream stream with work-queue retention and durable consumers for image jobs. The consumer acknowledges only after the resized image is durably stored and the output record is written. If a worker dies before acknowledgement, JetStream redelivers after the acknowledgement wait; the handler must make the output idempotent using the image/job ID. Pull consumers let workers request work at their actual capacity and are a good default for horizontal worker pools. Monitor pending messages, acknowledgement wait expiries, redelivery count, and processing latency. Scale consumers for backlog, but ensure each job has a stable identity so a redelivery does not create multiple stored outputs.

Why It Matters

Durable work queues absorb upload spikes and recover from worker failures. Acknowledging on receipt or omitting output deduplication trades those benefits for silently lost or duplicated processing.

Example Code

const messages = await consumer.consume();
for await (const message of messages) {
  const job = JSON.parse(sc.decode(message.data));
  await resizeOnce(job.id, job.sourceUrl);
  message.ack();
}

Common Mistakes

  • Acknowledging before writing the image: A worker crash loses the only durable job copy.
  • Using a transient consumer for a backlog: Progress disappears when the worker restarts.

Follow-up Questions

  • Why use a pull consumer? (Answer: Workers fetch at a controlled rate, supporting natural backpressure.)
  • What triggers redelivery? (Answer: No acknowledgement before the configured wait or an explicit negative acknowledgement.)

Related Questions

References