Skip to content
C.W.K.
Stream
Lesson 02 of 05 · published

One Worker, One Brain Lane

~12 min · serialization, worker, resource-boundary, ordering

Level 0Dry Nib
0 XP0/36 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
"Serialize the scarce capability, not the whole application."

Bound the scarce lane

Parallelism is not automatically progress. Brain calls share routing, budgets, and attention with sibling systems. One serial worker makes the scarce lane explicit and keeps load predictable.

Release the database before the brain

The worker claims the oldest eligible queued row in a short transaction, marks it running, commits, then performs the slow call outside the lock. Completion returns through a second guarded transaction.

Do not turn serial into one giant lock

Holding a database transaction during a model call blocks unrelated work. Starting extra workers breaks the single-consumer contract and can duplicate calls. Serial does not mean one giant lock; it means one accountable consumer.

Operational invariant. Serialize the scarce capability, not the whole application.

Put crashes on the timeline

Test “One Worker, One Brain Lane” on a timeline where the process may disappear before acceptance, after commit, after claim, after the external call, or after output storage. At every cut, state what the database knows. A repeated request must not duplicate cost or prose.

Design it

Draw the worker loop and mark exactly where the database lock is released. Name statuses, timestamps, and attempt rows. Explain which evidence authorizes recovery instead of letting a cleanup script guess.

Observability is recoverability

An operator should distinguish queued, running, failed, and done with one query and know the next safe action. A silent unknown called a queue has already lost the work.

Code

Claim one eligible row quickly·sql
BEGIN IMMEDIATE;
SELECT id FROM jobs
WHERE status = 'queued'
ORDER BY created_at, id
LIMIT 1;
-- Bind the selected id as :job_id.
UPDATE jobs
SET status = 'running', started_at = CURRENT_TIMESTAMP
WHERE id = :job_id AND status = 'queued';
INSERT INTO job_attempts(
  job_id, attempt_number, status, started_at
)
SELECT :job_id, COALESCE(MAX(attempt_number), 0) + 1,
       'running', CURRENT_TIMESTAMP
FROM job_attempts WHERE job_id = :job_id;
COMMIT;
-- Perform the slow brain call only after COMMIT.

External links

Exercise

Draw the worker loop and mark exactly where the database lock is released.
Hint
Move the crash point one step at a time.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.