For three years our background jobs ran on a pile of cron entries on a single server. Sending receipts, cleaning up expired sessions, syncing inventory with a partner every fifteen minutes. It worked, mostly. The problem was the “mostly.” When that server restarted at the wrong moment, jobs silently skipped a run, and we usually found out from a customer email two days later.
Last winter I proposed moving everything into a job table inside our existing Postgres database. No new infrastructure, no new vendor, just a table with a status column, a run_at timestamp, and workers that pick up rows with SELECT ... FOR UPDATE SKIP LOCKED. Our team is five engineers. Adding a message broker felt like buying a truck to carry groceries.
What went well
Visibility improved overnight. Instead of grepping cron logs on one machine, anyone on the team could run a query and see which jobs were pending, which had failed, and how many times each had been retried. Our support lead learned to check the table herself, which cut a surprising number of chat pings. Retries with exponential backoff took about forty lines of code.
What I'd do differently
I underestimated table bloat. We were inserting around 80,000 rows a day and never deleting them, and after six weeks the query that picked up new jobs got noticeably slower. A nightly cleanup job and a partial index on pending rows fixed it, but I should have planned for that from the start. I also wish we had built a simple dashboard earlier instead of relying on ad hoc queries.
Would I recommend this approach? For a small team that already runs Postgres and processes jobs in the thousands rather than the millions per hour, yes. It removed a whole category of mystery failures without adding anything new to operate. When we outgrow it, the job table will already have taught us exactly what we need from whatever comes next.
コメント2
コメントするにはログインしてください。
ログインBuying a truck to carry groceries. Stealing that line.
Great write-up. The cleanup part saved us a headache.