本文へスキップ

Moving Our Job Queue From Cron to Postgres, and What I'd Do Differently

2分で読めますいいね 18件

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.

이

この記事が気に入ったら、クリエイターを応援しましょう

いいねやフォローが이준호さんの励みになります。

この記事を書いた人

@junho_dev

결제 시스템을 만드는 백엔드 개발자입니다. 장애 회고와 작은 사이드 프로젝트 이야기를 씁니다.

記事
9
フォロワー
30
フォロー中
5
作者プロフィール RSS