willchennn.com / projects

Every pod was green and nothing worked

DistTaskQueue: a distributed task queue hand-rolled in Go on raw Redis and Postgres, running my Gmail → PDF → Google Drive backup on a €8/month single-node k3s cluster — and the production incidents it taught me.

Open the live dashboard Source on GitHub All eight war stories

The dashboard is the real production system. Press Flood and watch 1,000 jobs drain while Kubernetes scales the workers — it's rate-limited, so go ahead.

What it is

Every email that lands in my inbox is fetched from Gmail, rendered to PDF, and filed into a dated folder tree in Google Drive with its attachments. The interesting part is what runs it: a task queue built deliberately without Asynq, Celery, or SQS — raw Redis lists for queues, Postgres as the source of truth, and every delivery, retry, and failure-recovery decision hand-written and tested against real infrastructure.

scheduler ──polls Gmail──▶ Postgres row + Redis queue
                              │
             fetch pool ──▶ render pool ──▶ upload pool     (each scales 1–5 on queue depth)
                 │              │                │
             Gmail API     Gotenberg/Chromium   Drive API
                              │
     sweeper: revives orphans, promotes retries, reclaims dead workers' jobs

Delivery is at-least-once. The only lock in the system is a conditional UPDATE … WHERE status='queued' in Postgres; everything else — duplicate pushes, worker crashes, Redis restarts — is absorbed by that claim plus idempotent side effects. Getting to say that sentence with a straight face required the following incidents.

The pipeline was dead for weeks and every pod was green

I moved the cluster from DigitalOcean to Hetzner to save a few dollars. Mid-migration, the copied Google OAuth token failed with invalid_grant — and when I checked the old cluster, it had been failing identically, once a minute, for weeks. Ten pods Running, 77 days of uptime, zero emails backed up. Google expires refresh tokens after 7 days while an OAuth app is in "Testing" mode, and nothing I had built could tell the difference between a healthy pipeline and a corpse with a heartbeat.

The fix was one gauge — seconds since last successful poll — shipped to Grafana Cloud with an alert at 30 minutes. The lesson is bigger than the fix: liveness probes measure the process, not the job. Every pipeline needs a freshness signal for the thing it actually exists to do.

A queue pop that can't lose the job

The original worker did BRPOP then claimed the row in Postgres. Die between those two steps and the job ID is gone from Redis with nothing coming back for it. The upgrade is the classic reliable-queue pattern, built by hand: BLMOVE into a per-worker processing list, acknowledge with LREM only after the database settles, and let the sweeper move any dead worker's processing list back onto the queue. Both recovery paths can produce duplicates; the conditional claim absorbs them — one worker wins, the rest shrug and drop.

The fix that nearly shipped a worse bug

That upgrade required moving heartbeats from per-job goroutines to one process-lifetime loop — which silently broke the invariant orphan recovery depended on: "live heartbeat means the claimed job is being worked." Under the new scheme, one transient Postgres error after a claim would strand a job in running forever, because its owner never stops heartbeating. Adversarial review caught it before merge. The fix is crash-only design: if a worker can't record the outcome of a job it claimed, it exits — the pod restarts, the heartbeat expires, and the ordinary recovery path takes over. When you refactor, enumerate the invariants your recovery machinery assumes; the compiler won't.

The autoscaler saw a burst my own chart missed

First flood demo: KEDA scaled workers 1→5, and my queue-depth chart stayed flat at zero. The metrics gauge refreshes every 10 seconds from a cache; the burst drained in about ten seconds and fell entirely between two refreshes. KEDA polls Redis directly, so it reacted to a spike my own observability never recorded. Every metric has a freshness, and a cached gauge cannot represent states shorter than its refresh window.

Two-panel chart: queue depth spiking to ~973 jobs and draining within about 50 seconds, while ready replicas step from 1 to 5 and later back to 1
The captured flood run: 1,000 synthetic jobs, replicas stepping 1→5, the drain accelerating, and the scale-down five minutes later.

Kill a worker mid-job, on purpose

The recovery story, demonstrated rather than claimed — a worker is SIGKILLed while holding a job, the sweeper notices the expired heartbeat, and a replacement pod finishes the work:

16:17:33  enqueue slow synthetic job (5s sleep)
16:17:36  job running on workertest…d71c — killing worker pod now
16:17:37  running  worker=workertest…d71c  attempts=0
16:17:49  running  worker=workertest…99bb  attempts=1  err=worker died
16:17:54  done     worker=workertest…99bb  attempts=1
16:17:54  job survived the crash

What I'd tell you in an interview

Postgres is the source of truth and Redis is just a fast queue, because Redis is superb at throughput and bad at authoritative state. Heartbeats-plus-sweeper beats keyspace notifications because expiry events aren't guaranteed delivery and you end up building the catch-up scan anyway. Three worker pools instead of one because fetch is network-bound, render is CPU-bound (Chromium), and upload is API-bound — independent scaling and surgical retries. And single-node everything, on purpose: at 100 emails a day, high availability would be cosplay. The full reasoning — and the alternatives I rejected — is in ARCHITECTURE.md.