Skip to content
All tutorials
SampleIllustrative

Make a job queue retry safely

Decide what a worker may repeat, and what it must not do twice.

Topic
Custom software
Format
Tutorial
Difficulty
Intermediate
Reading time
1 min read (estimate)
Steps
2
Working time
25 min to work through (estimate)

Learning outcome

Separate work that is safe to repeat from work that needs a recorded result.

Prerequisites

  • Familiarity with a background worker
Contents, 2 steps

This sample is an authored example. It is not a tested implementation.

Step 1: Name the side effect

Write down the one action that must not happen twice, such as charging a card or sending a notice. Everything else can usually run again.

Checkpoint

You can point at the side effect, and you can say where its result will be recorded.

Step 2: Record the result before you rely on it

Store a row that says the side effect finished, using the job id as the key. If the row is already there, the worker stops.

Snippet
insert into job_results (job_id, finished_at)
values ($1, now())
on conflict (job_id) do nothing;

Checkpoint

Running the worker twice for one job id leaves one result row.

Review

This guide is an authored example. It hasn't been verified as a complete, production-ready implementation.

Reaching the end of this page marks your reading position, not a verified completion.

Contact

Have something worth building?

Share a few sentences about what you're trying to change. That's all it takes to start the conversation.