Redundant Background Worker Runners, Postgres Advisory-Lock Leader Election & Zero-Split-Brain Failover
Written by Horizon Services LLC • Fixed-Price Single-Host Production Architecture
The Production Failure Mode
Teams operating financial keepers, scheduled epoch jobs, or outbound webhook dispatchers either run a single fragile worker (downtime on restart) or run two workers that double-fire side effects due to split-brain race conditions.
1. Session-Level PostgreSQL Advisory Locks
Acquire pg_try_advisory_lock(lock_key) on a dedicated heartbeat connection so standby worker replicas promote automatically within 1 second if the leader connection drops.
2. Idempotent Outbox + Fencing Tokens
Pair every external API/Stripe mutation with a monotonic epoch/idempotency key stored via INSERT ... ON CONFLICT DO NOTHING.
3. Systemd Watchdog & Bounded Memory
Supervise primary and standby worker units under systemd Restart=always with explicit RSS caps so transient OOMs never stall queue processing.
Single-Host TCO vs Fragmented Cloud SaaS
| Architecture Choice | Typical Monthly Spend | Operational Ownership |
|---|---|---|
| Fragmented Cloud SaaS Stack | $520–$1,100/mo (Managed K8s Control Plane + Multi-AZ Worker Nodes + Managed Queue) | Recurring seat/task tax, vendor rate limits, scattered auth |
| Owned Single-Host Blueprint | $20–$38/mo Single Host + Standby Process (Postgres Advisory Lock + Transactional Outbox) | 100% code & data ownership, predictable fixed monthly VM bill |
Frequently Asked Question
What happens if the active worker crashes mid-job?
PostgreSQL releases the session-level advisory lock as soon as the TCP socket closes, allowing the warm standby worker to acquire leadership immediately while transactional outbox locks prevent duplicate external calls.