How dragon-in-the-slumber/distributed-task-queue works
Purpose and Entry Points
This is a distributed asynchronous task-processing system. Clients submit work through a Go HTTP API; independent Go workers execute it; a result service durably resolves final state. A Next.js dashboard exposes task, queue, worker, result, and DLQ views.
Service entry points:
• API: cmd/api/main.go
• Worker: cmd/worker/main.go
• Result consumer: cmd/result/main.go
• Dashboard shell/API client: frontend/src/app/layout.tsx, frontend/src/lib/api.ts
API Ingestion and Control Plane
The API is the producer and operational control plane. Submission creates durable task state, then enqueues the work for workers. It also exposes task lookup/listing, logs, DLQ inspection/redrive/clear, task clearing, and demo reset operations.
Key boundaries:
• HTTP routing and middleware: internal/api/routes/routes.go, internal/api/routes/middleware.go
• Task submission and task lifecycle queries: internal/api/handlers/task_handler.go, internal/api/handlers/get_task_handler.go
• DLQ and operational actions: internal/api/handlers/dlq.go, internal/api/handlers/redrive_dlq_handler.go
Durable State and Queue Contracts
Task records and statuses are modeled centrally and persisted in PostgreSQL; Redis carries asynchronous messages between services. The API-to-worker queue, worker-to-result queue, and dead-letter queue are distinct transport boundaries, allowing execution and final persistence to scale separately.
Core contracts/infrastructure adapters:
• Task model: pkg/models/task.go
• PostgreSQL connection/storage adapter: pkg/db/postgres.go
• Redis adapter: pkg/cache/redis.go
• Queue operations and queue names/protocol: pkg/queue/queue.go
Worker Execution and Failure Handling
Workers block on Redis work retrieval, mark tasks running, execute processing, and publish outcomes for resolution. Processing is intentionally decoupled from request handling and can run as multiple replicas. Failures follow exponential-backoff retry behavior; exhausted tasks go to the Redis DLQ rather than being discarded.
Execution stages:
• Worker process bootstrap and signal lifecycle: cmd/worker/main.go
• Task consumption/processing and state transitions: internal/worker/processor/processor.go
• Retry scheduling/backoff policy: internal/worker/retry/retry.go
Result Resolution
The result service independently consumes worker result messages and updates PostgreSQL to terminal task states. This isolates workers from final database-write latency and creates eventual consistency between execution completion and dashboard/API-visible completion.
• Result-service entry point: cmd/result/main.go
• Result queue consumer and persistence handling: internal/result/handler/result_handler.go
Observability and User Interface
Prometheus metrics instrument processing duration and status counts. The frontend is a separate Next.js runtime that calls the backend API and provides operational pages for submission, queues, workers, results, bulk results, and DLQ management.
• Metrics integration: pkg/metrics/prometheus.go
• Dashboard component: frontend/src/components/Dashboard.tsx
• Stateful client task context: frontend/src/app/context/TaskContext.tsx
• Queue/DLQ views: frontend/src/app/queue/page.tsx, frontend/src/app/dlq/page.tsx
Deployment Runtime
The backend consists of API, worker, and result containers plus Redis 7 and PostgreSQL 14, orchestrated locally with Docker Compose; Fly deployment manifests split the three Go services. Worker replication is an intended scaling mechanism. Database initialization is supplied by PostgreSQL bootstrap SQL.
• Compose topology/environment: deployments/docker-compose.yml, deployments/.env
• Service images: build/docker/api.Dockerfile, build/docker/worker.Dockerfile, build/docker/result.Dockerfile
• Managed deployment manifests: deployments/fly.api.toml, deployments/fly.worker.toml, deployments/fly.result.toml
Main entry points: processor.go Task processor · queue.go Redis queues · task_handler.go Task handlers · main.go API service
Components 18
API control plane
- API service · HTTP producer and operational control plane.
cmd/api/main.go - Routes and middleware
internal/api/routes/routes.go - Task handlers · Submission and task lifecycle queries.
internal/api/handlers/task_handler.go - DLQ operations · Inspect, redrive, clear, and reset actions.
internal/api/handlers/dlq.go
Async processing
- Worker service · Replica-scalable execution process.
cmd/worker/main.go - Task processor · Consumes work, marks running, executes, and publishes outcomes.
internal/worker/processor/processor.go - Retry policy · Exponential backoff sends exhausted tasks to DLQ.
internal/worker/retry/retry.go - Result service · Independent terminal-state resolver.
cmd/result/main.go - Result consumer · Consumes outcomes and writes terminal task states.
internal/result/handler/result_handler.go - Prometheus metrics · Processing duration and status counters.
pkg/metrics/prometheus.go
State and transport
- Task model
pkg/models/task.go - PostgreSQL · Stores task records and lifecycle state.
pkg/db/postgres.go - Redis queues · Separates work, result, and dead-letter queues.
pkg/queue/queue.go
Operations UI
- Next.js dashboard · Operational task, queue, worker, result, and DLQ views.
frontend/src/components/Dashboard.tsx - Task client state
frontend/src/app/context/TaskContext.tsx - Backend API client
frontend/src/lib/api.ts
Deployment
- Compose topology · Runs API, workers, result service, Redis, and PostgreSQL.
deployments/docker-compose.yml - Fly service manifests · Deploys API, worker, and result services independently.
deployments/fly.api.toml
Connections 25
- API service → Routes and middleware: serves HTTP
- Routes and middleware → Task handlers: dispatches
- Routes and middleware → DLQ operations: dispatches
- Task handlers → Task model: uses
- Task handlers → PostgreSQL: creates and reads tasks
- Task handlers → Redis queues: enqueues work
- DLQ operations → Redis queues: manages DLQ
- Worker service → Task processor: runs
- Task processor → Redis queues: consumes work; publishes results
- Task processor → PostgreSQL: marks running
- Task processor → Retry policy: on failure
- Retry policy → Redis queues: requeues or dead-letters
- Task processor → Prometheus metrics: records execution metrics
- Result service → Result consumer: runs
- Result consumer → Redis queues: consumes result queue
- Result consumer → PostgreSQL: writes terminal state
- Next.js dashboard → Task client state: maintains UI state
- Task client state → Backend API client: uses
- Backend API client → API service: calls backend API
- Compose topology → API service: runs locally
- Compose topology → Worker service: runs replicas locally
- Compose topology → Result service: runs locally
- Fly service manifests → API service: deploys
- Fly service manifests → Worker service: deploys
- Fly service manifests → Result service: deploys