{"name":"adr-template","title":"Architecture decision record template","description":"An architecture decision record (ADR) template in Markdown with status, context, decision, consequences and alternatives, shown on a real example.","category":"specs","updated":"2026-11-30","url":"https://sniptide.com/templates/adr-template","files":[{"name":"docs/adr/0007-use-postgres-for-background-jobs.md","language":"markdown","content":"# 7. Use Postgres for background jobs\n\n- Status: Accepted\n- Date: 2026-11-30\n- Deciders: Platform team\n- Supersedes: [3. Use Redis for background jobs](0003-use-redis-for-background-jobs.md)\n\n## Context\n\nThe app sends emails, generates exports and calls webhooks in background jobs. Since ADR 3, jobs\nrun on a Redis queue. Two problems keep coming up:\n\n- A job and the database write that created it are not atomic. When Redis is unavailable after a\n  commit, the job is lost, which happened three times last quarter.\n- Redis is the only reason we run a second stateful service. It needs its own backups, monitoring\n  and upgrades.\n\nWe process about 40,000 jobs a day, with peaks of 30 a second. Postgres handles our current load\nat under 20% CPU.\n\n## Decision\n\nWe will run background jobs from a `jobs` table in Postgres, using `SELECT ... FOR UPDATE SKIP\nLOCKED` to claim work. Jobs are inserted in the same transaction as the change that creates them.\nWe will use an existing library for the queue instead of writing our own.\n\n## Consequences\n\n- A job exists if and only if its transaction committed. Lost jobs stop.\n- We remove Redis from production, its backups and its alerts.\n- Polling adds load to Postgres. We will alert when job claims take longer than 50 ms at the 95th\n  percentile, and revisit this decision if they do.\n- The workers need a migration path: both queues run side by side for two weeks, then the Redis\n  queue is drained and removed.\n\n## Alternatives considered\n\n- **Keep Redis and add an outbox table.** Fixes lost jobs, but keeps both services and adds a relay\n  process.\n- **A managed queue such as SQS.** Removes operations work, but jobs still aren't transactional and\n  local development needs an emulator.\n"}]}