Building a Resilient Job Queue in Go: Postgres, Worker Pool and Graceful Shutdown

Building a Resilient Job Queue in Go: Postgres, Worker Pool and Graceful Shutdown

●1 ●6 ●15
calendar_today • schedule2 min read

Building a Resilient Job Queue in Go: Postgres, Worker Pool and Graceful Shutdown

I am Pires Aurélio, Go Backend Engineer from Luanda, Angola. I build production systems in public.

For two years I built scrapers and simple REST APIs. I got tired of tutorials that only show Hello World. No jobs, no retries, no real production problems.

So I built a real one: A Production-Ready Background Job Queue in Go.

This is the production deep-dive companion to my DEV article. All links are in the first comment.

WHAT WE ARE BUILDING

A REST API where you POST a task, it goes to Postgres, and Go workers process it in background with retries and graceful shutdown. Like Sidekiq and Celery, but in pure Go.

Stack: Go + Gin + Postgres + Goroutines + Docker
Features: Clean Architecture, Postgres as queue, Worker Pool with 5 Goroutines, Graceful Shutdown, Exponential Backoff, Supervisor pattern.

  1. ARCHITECTURE

Client -> POST /jobs -> API Gin -> Postgres jobs table -> Worker Pool -> Process -> Update Status

Why Postgres and not Redis? One less service and ACID guarantees. This proves you understand transactions and row-level locking.

  1. THE PRODUCTION SCHEMA

Table jobs has: id UUID, payload JSONB, status pending processing done failed, attempts int, max_attempts int, last_error text, created_at and next_retry_at.

The most important part is the index on status and next_retry_at where status is pending. This makes the picker O log n with 1 million rows. See Image 1.

  1. THE SAFE PICKER

The mistake in tutorials is SELECT then UPDATE. Two workers get the same job.

The solution is an atomic UPDATE where id comes from a sub-select with row-level locking and SKIP LOCKED. Only one worker gets a job. This is what separates toy from production. See Image 1.

  1. WORKER POOL

Start with concurrency 5. For each worker start a goroutine. Loop select on ctx Done for shutdown, default picks a job and processes with retry.

If fails, increment attempts. If attempts >= max, mark failed. Else backoff = 2 power attempts seconds and set next_retry_at.

See Image 2.

  1. SUPERVISOR - NEVER LOSE A JOB

If server crashes while processing, job stays processing forever.

On startup: Reset all jobs where status is processing and updated_at older than 5 minutes back to pending. This is what real companies do.

  1. GRACEFUL SHUTDOWN - THE INTERVIEW QUESTION

Use signal NotifyContext for Interrupt and SIGTERM. Start workers with that context. Start HTTP server in goroutine. Block on ctx Done. On signal, create timeout context 10 seconds and call server Shutdown. Workers stop via ctx Done.

See Image 3. This proves you know context propagation.

  1. BENCHMARK

Python + Celery + Redis: 120 jobs per sec, 180 MB RAM
Go + Postgres: 1850 jobs per sec, 18 MB RAM

15x faster, 10x less memory.

  1. WHAT RECRUITERS SEE

You understand concurrency, you understand databases beyond SELECT, you think about failure, you can build infrastructure not just CRUD.

Full code with metrics is coming to my GitHub next week. Links in first comment.

Built from Luanda, Angola.

Want more #architecture & #backend deep dives?

Join 6,182+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

1 Comment

More Posts

The Sovereign Vault — A Comprehensive Guide to Protocol-Driven AI

Ken W. Algerverified - Jun 4

Stop Turning Every REST Endpoint into an MCP Tool: A Better Approach to AI Tool Design

zeekwrites - Nov 30

Short links that outlive the vendor: a newsroom URL shortener on WordPress and AWS

jaafarabazid - Nov 10

Three Go Concurrency Mistakes I See in Almost Every Worker Pool

prasadekke - Jul 20

Kafka Just Learned to Be a Queue. Postgres Has Been One All Along.

firatcelik - Oct 9
chevron_left
368 Points • 22 Badges
6Posts
5Comments
6Connections
Go Developer | Open Source | API Integration`/ Independent Go Developer building open-source project... Show more

Related Jobs

View all jobs →

Commenters (This Week)

13 comments
5 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!