Thursday, December 3, 2026 | London 10°C · Drizzle
DailyGlimpse

AI

Lightweight Background Job Queue Using PostgreSQL LISTEN/NOTIFY for Solo Devs

Glad Labs 4 min read

Solo developers often face a paradox: building a robust application requires handling background tasks like sending emails or processing images, but adding a full-featured job queue system introduces complexity that outweighs the problem. Tools such as Celery or Sidekiq demand additional infrastructure, monitoring, and maintenance—overkill for small projects that might never scale beyond a single user. This creates a painful trade-off: either build something too complex from day one or delay critical features entirely. Premature optimization for scale is a common pitfall that can crumble apps before they even launch.

Why LISTEN/NOTIFY Fits the Bill

PostgreSQL's built-in LISTEN/NOTIFY mechanism solves this perfectly. It's a zero-cost, persistent feature requiring no external dependencies. Unlike message brokers (RabbitMQ, Redis), it leverages your existing database connection. This avoids:

  • Adding new infrastructure to manage
  • Learning a new system's idiosyncrasies
  • Worrying about queue persistence or retries

The key advantage is simplicity: you're using a feature already available in your database. The trade-off is clear: it's not for production systems needing guaranteed delivery, retries, or high throughput. But for a solo developer building a personal project or MVP, it's exactly the right tool. As the PostgreSQL documentation states: "The LISTEN and NOTIFY commands allow a client to register for notification events." This is precisely what's needed for a lightweight queue.

Setting Up the Queue: A Step-by-Step Guide

No new services or configurations are needed. Start with a simple jobs table to track pending work (optional but recommended for debugging):

CREATE TABLE jobs (
id SERIAL PRIMARY KEY,
type TEXT NOT NULL,
payload JSONB NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);

The queue itself is a database channel. Create a channel name (e.g., background_jobs) that's unique to your app. There's no setup—just use it directly.

Handling Jobs: Code Examples and Best Practices

Producer: Adding Work to the Queue

This script sends a job to the background_jobs channel. It's safe for a single connection (use a connection pool for multiple producers).

import psycopg2
from psycopg2.extras import Json

# Connect to your database (use environment variables for secrets!)
conn = psycopg2.connect(
dbname="your_db",
user="user",
password="pass",
host="localhost",
)
cur = conn.cursor()

# Insert the job and capture its id
cur.execute(
"INSERT INTO jobs (type, payload) VALUES (%s, %s) RETURNING id",
("send_email", Json({"to": "user@example.com", "subject": "Welcome!"})),
)
job_id = cur.fetchone()[0]
conn.commit()

# Notify the channel with the job id as the payload so the worker knows
# which row to process. Use pg_notify() rather than NOTIFY so psycopg2
# can safely parameterize the payload.
cur.execute("SELECT pg_notify(%s, %s)", ("background_jobs", str(job_id)))
conn.commit()

cur.close()
conn.close()

Worker: Processing Jobs

This worker listens on the channel and processes jobs. It uses psycopg2's connection.notifies to handle notifications asynchronously.

import psycopg2
import psycopg2.extensions
import time

def process_job(cur, job_id):
"""Fetch the job row and do the actual work (e.g., send email)."""
cur.execute(
"SELECT type, payload FROM jobs WHERE id = %s",
(job_id,),
)
row = cur.fetchone()
if not row:
print(f"job {job_id} not found -- maybe already processed")
return
job_type, payload = row
print(f"Processing job {job_id}: {job_type} -- {payload}")
# Real implementation would include error handling and logging

def start_worker():
conn = psycopg2.connect(
dbname="your_db",
user="user",
password="pass",
host="localhost",
)
# LISTEN must run outside a transaction
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()

# Subscribe to the channel
cur.execute("LISTEN background_jobs")
print("Worker started. Waiting for jobs...")

while True:
conn.poll()
while conn.notifies:
notify = conn.notifies.pop(0)
# notify.payload is the job id as a string
try:
job_id = int(notify.payload)
except ValueError:
print(f"unexpected payload: {notify.payload!r}")
continue
process_job(cur, job_id)
time.sleep(0.1)  # keep the loop responsive without burning CPU

if __name__ == "__main__":
start_worker()

This approach leverages existing database connections, keeps infrastructure minimal, and is ideal for solo developers or small projects where simplicity matters more than high throughput or guaranteed delivery.

React