IN PROGRESSDistributed Systems

Concurrent Distributed Job Queue

Go + PostgreSQL queue engine using FOR UPDATE SKIP LOCKED for contention-free job claiming, with priority dispatch and orphan recovery.

Private while in progress — source on request
SKIP LOCKED
Worker Contention
Workers never block on each other's rows
Goroutine Pool
Execution Engine
Context cancellation & graceful shutdown
Orphan Reaper
Crash Recovery
Timed-out claims return to pending
01 / PROBLEM STATEMENT & OVERVIEW

Executive Architecture Overview

Concurrent Distributed Job Queue is a high-reliability background task execution engine written in Go and PostgreSQL. Designed to coordinate asynchronous background work across multiple concurrent worker goroutines without requiring external broker infrastructure like RabbitMQ or Redis.

The Core Engineering Problem

Database-backed queues suffer row lock contention when multiple workers poll for pending tasks at the same time: without SKIP LOCKED, workers serialize behind each other on the same candidate rows instead of working in parallel.

02 / BLUEPRINT & FLOW

System Architecture & Data Pipeline

Concurrent Queue State MachineFOR UPDATE SKIP LOCKED
01 / PRODUCER
HTTP Enqueue
Atomic INSERT into jobs table with priority, JSON payload & schedule.
Transactional enqueue
02 / DEQUEUE ENGINE
Postgres Locks
SELECT FOR UPDATE SKIP LOCKED eliminates worker thread lock waits.
Claims never queue behind each other
03 / EXECUTION
Worker Goroutines
Bounded pool with recover() shield & context deadline cancellation.
Panic isolation
04 / RECOVERY
Orphan Reaper
Sweeps claims left in processing past a timeout back to pending.
Retries & DLQ: not yet built

Producers enqueue tasks into PostgreSQL via HTTP POST. Concurrent worker goroutines poll pending tasks using SELECT FOR UPDATE SKIP LOCKED, atomically claiming jobs with zero lock contention.

LAYER 01

HTTP Producer API

Enqueues background jobs with priority levels, JSON payloads, and scheduled run_at timestamps.

LAYER 02

Queue Engine (SKIP LOCKED)

Executes SELECT FOR UPDATE SKIP LOCKED queries to claim pending jobs atomically without blocking other worker goroutines.

LAYER 03

Worker Pool Dispatcher

Manages N worker goroutines, job context timeout cancellation, and graceful shutdown signal handling.

LAYER 04

Orphan Reaper

Periodically sweeps jobs left in 'processing' past a configurable timeout and returns them to 'pending', recovering work abandoned by crashed workers.

03 / DECISION LEDGER

System Design Trade-Offs

TRADE-OFF 01

PostgreSQL FOR UPDATE SKIP LOCKED vs External Broker (RabbitMQ/Redis)

Engineering Rationale: Using PostgreSQL directly enables transactional enqueuing (job inserted in the exact same DB transaction as business domain data), preventing phantom job execution.

TRADE-OFF 02

In-Process Worker Pools vs Separate Worker Binaries

Engineering Rationale: Embedding worker pools directly in Go with channels and sync primitives simplifies container orchestration and local development.

04 / STORAGE INTERNALS

Database Schema & Indexing Strategy

Task state machine schema with composite partial indexes on status, priority, and run_at timestamp for sub-millisecond dequeue lookups.

SQL DDL & PARTIAL INDEX DEFINITIONS
postgres-definition-1.sqlPostgreSQL 15
CREATE TABLE jobs (id UUID PRIMARY KEY, payload JSONB NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending', priority INT NOT NULL DEFAULT 0, attempts INT NOT NULL DEFAULT 0, max_attempts INT NOT NULL DEFAULT 5, locked_by UUID, locked_until TIMESTAMPTZ, run_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), created_at TIMESTAMPTZ DEFAULT NOW());
postgres-definition-2.sqlPostgreSQL 15
CREATE INDEX idx_jobs_dequeue ON jobs (status, priority DESC, run_at) WHERE status = 'pending';
Indexing Strategy & Query Plan Optimization:Composite partial index filtering status = 'pending' ensures that dequeue queries select eligible tasks in sub-millisecond time regardless of historical job volume.
07 / TECHNOLOGY LEDGER

Technology Stack Justifications

Go (Golang)

Native channels, select statements, and lightweight goroutines provide robust worker pool primitives.

PostgreSQL 9.5+

Native support for FOR UPDATE SKIP LOCKED enabling lock-free concurrent row selection.

08 / INTERFACE CONTRACT

REST API Specification

MethodEndpoint PathSemantics & Status
POST/jobsEnqueue new background job with payload, priority, and schedule
GET/jobs/:idQuery job execution status, attempts count, and result
GET/jobsList jobs with status/kind filtering and keyset pagination
GET/healthLiveness probe
09 / RETROSPECTIVE & ROADMAP

Production Takeaways & Next Steps

Engineering Lessons

  • •SELECT ... FOR UPDATE SKIP LOCKED lets each worker claim a row without blocking on rows another worker already holds, so adding workers adds throughput instead of lock contention. An integration test asserts that exactly one worker claims a given job.
  • •Recovering crashed workers by sweeping stale claims on a timeout is far simpler than a heartbeat protocol, and it degrades safely: the worst case is a job running twice, which is why processors must be idempotent.

Future Improvements

  • •Retry scheduling: increment retry_count and reschedule via next_run_at with exponential backoff and full jitter (currently Fail is terminal).
  • •Dead-letter queue for jobs that exhaust their retry budget (MoveToDLQ is defined in the repository interface but not yet implemented).
  • •Prometheus /metrics endpoint exposing queue depth and processing rates.
ELVINRODRIGUES.DEV
ERElvin Rodrigues

Backend Engineer focused on Go, PostgreSQL, Redis, and distributed systems.

CURRENTLY BUILDINGConcurrent Distributed Job Queue
Go + PostgreSQL FOR UPDATE SKIP LOCKED
AVAILABLE FOR
Backend Engineering · Distributed Systems · Infrastructure
Nitte, India · Relocation / Remote (2027)
© 2026 Elvin Rodrigues · Nitte, Karnataka, IN
Back to top