A durable, idempotent, Postgres-backed distributed job processing system built with Node.js.
This project explores core distributed systems concepts including:
- Idempotent job submission
- Durable state machines
- Transactional concurrency control
- Worker coordination via database locking
- Retry and failure semantics (upcoming)
Current architecture:
Client
↓
API (Node.js, stateless)
↓
Postgres (durable job state machine)
Design principles:
- The API is stateless.
- Postgres is the single source of truth.
- Jobs are durable and survive crashes.
- Duplicate submissions are prevented via idempotency.
- Dockerized PostgreSQL (Infrastructure as Code)
- Versioned SQL migrations
- Durable job storage
- Idempotent job creation using unique constraints
- JSONB-based flexible payload storage
- Input validation with Zod
- Health endpoint
- UUID-based distributed-safe job identifiers
- Node.js (Express)
- PostgreSQL 16
- Docker Compose
- Zod (request validation)
- UUID (unique identifiers)
djps/
├── docker-compose.yml
├── db/
│ └── migrations/
│ └── 001_create_jobs.sql
├── packages/
│ └── api/
│ ├── index.js
│ └── package.json
└── docs/
Each job represents a durable state machine.
- queued
- running
- succeeded
- failed
- canceled
- dead
id(UUID)typepayload(JSONB)statusrun_at(scheduling support)attemptsmax_attemptsidempotency_key
Job creation supports idempotency via:
UNIQUE(type, idempotency_key)This guarantees:
- Safe client retries
- No duplicate job creation
- Deterministic behavior under network failure
If the same idempotencyKey is submitted again for the same job type, the existing job is returned instead of creating a new one.
docker compose up -ddocker compose exec -T postgres psql -U djps -d djps < db/migrations/001_create_jobs.sqlcd packages/api
npm install
npm run devAPI runs at:
http://localhost:3000
curl http://localhost:3000/healthInvoke-RestMethod -Method POST `
-Uri "http://localhost:3000/jobs" `
-ContentType "application/json" `
-Body '{"type":"email_send","payload":{"to":"a@b.com"},"idempotencyKey":"abc-123"}'Repeat the same request to verify idempotency.
curl http://localhost:3000/jobs/<JOB_ID>Postgres acts as both:
- Durable job storage
- Distributed coordination mechanism
It provides:
- ACID guarantees
- Row-level locking
- Transactional consistency
- Crash recovery
- Unique constraints for idempotency
This makes it well-suited for correctness-first job processing systems.
Planned enhancements:
- Worker implementation
- Atomic job claiming using:
FOR UPDATE SKIP LOCKED- Retry logic with exponential backoff
- Lease / heartbeat mechanism
- Dead-letter queue
- Multi-worker concurrency testing
- Metrics & observability
This project focuses on understanding:
- Distributed coordination
- Concurrency control
- Idempotency patterns
- Transaction boundaries
- Reliable background processing
- Production-grade system design
It intentionally avoids heavy ORM abstractions to maintain full control over SQL and locking semantics.
MIT
