Lesson 13.03 · Phase 13 · Production Agents

Queues, workers & long jobs

A worker holding a job is making a promise it may not keep. Leases make the promise expire, heartbeats renew it, and fencing tokens stop a worker that broke it from writing anyway.

Learning objectives

  • Build a job queue on a database table: atomic leasing, priorities, and no job ever handed to two workers at once.
  • Use leases and heartbeats so a dead worker's job is picked up by another, and long jobs can report progress.
  • Use a fencing token to refuse a stale worker's result after its lease was taken over.
  • Retry retryable failures with exponential backoff and jitter, and send permanent failures and exhausted retries to a dead-letter queue.

Lesson 1 ran agents on a thread pool inside the web server. That falls apart as soon as runs get long: - A deploy kills every run in progress. - Traffic spikes compete with running agents for the same machine. - A run that needs 20 minutes and three model retries has nowhere to wait between attempts.

Loading the full lesson…