# dragon-in-the-slumber/distributed-task-queue architecture

> Architecture diagram of the GitHub repository dragon-in-the-slumber/distributed-task-queue, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dragon-in-the-slumber/distributed-task-queue
- Repository: https://github.com/dragon-in-the-slumber/distributed-task-queue

## Overview

#### 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: [`internal/worker/processor/processor.go`](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/worker/processor/processor.go) (Task processor), [`pkg/queue/queue.go`](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/queue/queue.go) (Redis queues), [`internal/api/handlers/task_handler.go`](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/api/handlers/task_handler.go) (Task handlers), [`cmd/api/main.go`](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/cmd/api/main.go) (API service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API control plane"]
  node_api_main["API service<br/>Go entry point<br/>[main.go]"]
  node_api_routes["Routes and middleware<br/>HTTP boundary<br/>[routes.go]"]
  node_task_handlers["Task handlers<br/>API handlers<br/>[task_handler.go]"]
  node_dlq_handlers["DLQ operations<br/>API handlers<br/>[dlq.go]"]
end

subgraph group_async["Async processing"]
  node_worker_main["Worker service<br/>Go entry point<br/>[main.go]"]
  node_processor["Task processor<br/>worker execution<br/>[processor.go]"]
  node_retry["Retry policy<br/>failure handling<br/>[retry.go]"]
  node_result_main["Result service<br/>Go entry point<br/>[main.go]"]
  node_result_handler["Result consumer<br/>persistence handler<br/>[result_handler.go]"]
  node_metrics["Prometheus metrics<br/>observability<br/>[prometheus.go]"]
end

subgraph group_state["State and transport"]
  node_task_contract["Task model<br/>shared contract<br/>[task.go]"]
  node_postgres[("PostgreSQL<br/>durable task store<br/>[postgres.go]")]
  node_redis_queue["Redis queues<br/>message transport<br/>[queue.go]"]
end

subgraph group_ui["Operations UI"]
  node_dashboard["Next.js dashboard<br/>frontend runtime<br/>[Dashboard.tsx]"]
  node_task_context["Task client state<br/>frontend context<br/>[TaskContext.tsx]"]
  node_api_client["Backend API client<br/>frontend adapter<br/>[api.ts]"]
end

subgraph group_runtime["Deployment"]
  node_compose["Compose topology<br/>local orchestration<br/>[docker-compose.yml]"]
  node_fly_manifests["Fly service manifests<br/>managed deployment<br/>[fly.api.toml]"]
end

node_api_main -->|"serves HTTP"| node_api_routes
node_api_routes -->|"dispatches"| node_task_handlers
node_api_routes -->|"dispatches"| node_dlq_handlers
node_task_handlers -->|"uses"| node_task_contract
node_task_handlers -->|"creates and reads tasks"| node_postgres
node_task_handlers -->|"enqueues work"| node_redis_queue
node_dlq_handlers -->|"manages DLQ"| node_redis_queue
node_worker_main -->|"runs"| node_processor
node_processor -->|"consumes work; publishes results"| node_redis_queue
node_processor -->|"marks running"| node_postgres
node_processor -->|"on failure"| node_retry
node_retry -->|"requeues or dead-letters"| node_redis_queue
node_processor -->|"records execution metrics"| node_metrics
node_result_main -->|"runs"| node_result_handler
node_result_handler -->|"consumes result queue"| node_redis_queue
node_result_handler -->|"writes terminal state"| node_postgres
node_dashboard -->|"maintains UI state"| node_task_context
node_task_context -->|"uses"| node_api_client
node_api_client -->|"calls backend API"| node_api_main
node_compose -.->|"runs locally"| node_api_main
node_compose -.->|"runs replicas locally"| node_worker_main
node_compose -.->|"runs locally"| node_result_main
node_fly_manifests -.->|"deploys"| node_api_main
node_fly_manifests -.->|"deploys"| node_worker_main
node_fly_manifests -.->|"deploys"| node_result_main

click node_api_main "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/cmd/api/main.go"
click node_api_routes "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/api/routes/routes.go"
click node_task_handlers "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/api/handlers/task_handler.go"
click node_dlq_handlers "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/api/handlers/dlq.go"
click node_task_contract "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/models/task.go"
click node_postgres "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/db/postgres.go"
click node_redis_queue "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/queue/queue.go"
click node_worker_main "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/cmd/worker/main.go"
click node_processor "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/worker/processor/processor.go"
click node_retry "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/worker/retry/retry.go"
click node_result_main "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/cmd/result/main.go"
click node_result_handler "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/result/handler/result_handler.go"
click node_metrics "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/metrics/prometheus.go"
click node_dashboard "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/frontend/src/components/Dashboard.tsx"
click node_task_context "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/frontend/src/app/context/TaskContext.tsx"
click node_api_client "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/frontend/src/lib/api.ts"
click node_compose "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/deployments/docker-compose.yml"
click node_fly_manifests "https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/deployments/fly.api.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_main,node_api_routes,node_task_handlers,node_dlq_handlers toneBlue
class node_worker_main,node_processor,node_retry,node_result_main,node_result_handler,node_metrics toneAmber
class node_task_contract,node_postgres,node_redis_queue toneMint
class node_dashboard,node_task_context,node_api_client toneRose
class node_compose,node_fly_manifests toneIndigo
```

## Components

### API control plane

- [API service](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/cmd/api/main.go): Go entry point · HTTP producer and operational control plane. · `cmd/api/main.go`
- [Routes and middleware](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/api/routes/routes.go): HTTP boundary · `internal/api/routes/routes.go`
- [Task handlers](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/api/handlers/task_handler.go): API handlers · Submission and task lifecycle queries. · `internal/api/handlers/task_handler.go`
- [DLQ operations](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/api/handlers/dlq.go): API handlers · Inspect, redrive, clear, and reset actions. · `internal/api/handlers/dlq.go`

### Async processing

- [Worker service](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/cmd/worker/main.go): Go entry point · Replica-scalable execution process. · `cmd/worker/main.go`
- [Task processor](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/worker/processor/processor.go): worker execution · Consumes work, marks running, executes, and publishes outcomes. · `internal/worker/processor/processor.go`
- [Retry policy](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/worker/retry/retry.go): failure handling · Exponential backoff sends exhausted tasks to DLQ. · `internal/worker/retry/retry.go`
- [Result service](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/cmd/result/main.go): Go entry point · Independent terminal-state resolver. · `cmd/result/main.go`
- [Result consumer](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/internal/result/handler/result_handler.go): persistence handler · Consumes outcomes and writes terminal task states. · `internal/result/handler/result_handler.go`
- [Prometheus metrics](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/metrics/prometheus.go): observability · Processing duration and status counters. · `pkg/metrics/prometheus.go`

### State and transport

- [Task model](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/models/task.go): shared contract · `pkg/models/task.go`
- [PostgreSQL](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/db/postgres.go): durable task store · Stores task records and lifecycle state. · `pkg/db/postgres.go`
- [Redis queues](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/pkg/queue/queue.go): message transport · Separates work, result, and dead-letter queues. · `pkg/queue/queue.go`

### Operations UI

- [Next.js dashboard](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/frontend/src/components/Dashboard.tsx): frontend runtime · Operational task, queue, worker, result, and DLQ views. · `frontend/src/components/Dashboard.tsx`
- [Task client state](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/frontend/src/app/context/TaskContext.tsx): frontend context · `frontend/src/app/context/TaskContext.tsx`
- [Backend API client](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/frontend/src/lib/api.ts): frontend adapter · `frontend/src/lib/api.ts`

### Deployment

- [Compose topology](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/deployments/docker-compose.yml): local orchestration · Runs API, workers, result service, Redis, and PostgreSQL. · `deployments/docker-compose.yml`
- [Fly service manifests](https://github.com/dragon-in-the-slumber/distributed-task-queue/blob/main/deployments/fly.api.toml): managed deployment · Deploys API, worker, and result services independently. · `deployments/fly.api.toml`

## Connections

- 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
