Step-by-step explanation of Gopher's job lifecycle, architecture, and real-world usage
| Component | Description | Role |
|---|---|---|
| Server | RESTful API | Accepts job submissions and provides status endpoints |
| Worker | Processing daemons | Execute jobs from queues with configurable concurrency |
| CLI | Command-line interface | Administrative tools and job management |
| Redis | Message broker | Central job storage, queue management, and communication |
Clients submit jobs via HTTP API or CLI. Server validates the request, generates a unique job ID, and enqueues it in Redis.
POST /api/jobs
{
"type": "email",
"payload": {
"to": "user@example.com",
"subject": "Welcome!",
"body": "Thank you for signing up."
},
"priority": "high",
"retries": 3
}
Response:
{
"job_id": "e52fe7d2-8be1-4d9f-a27d-2f02cb16ad00",
"status": "enqueued"
}- High Priority Queue: Critical, time-sensitive jobs
- Normal Priority Queue: Standard business operations
- Low Priority Queue: Background tasks and maintenance
Delayed jobs are stored in Redis sorted sets for precise timing:
ZADD scheduled_jobs 1695916800 '{"job_id":"abc123","type":"reminder",...}'Workers implement a sophisticated polling mechanism:
- Priority-based polling with configurable ratios (e.g., 3:2:1)
- Atomic job retrieval preventing duplicate processing
- Handler routing based on job type
- Automatic retry with exponential backoff
// Handler registration
registry.RegisterHandler("email", emailHandler)
registry.RegisterHandler("image_processing", imageHandler)
registry.RegisterHandler("data_export", exportHandler)| State | Description |
|---|---|
enqueued |
Job waiting in queue |
processing |
Currently being executed |
completed |
Successfully finished |
failed |
Failed after all retries |
scheduled |
Waiting for scheduled time |
- Queue depth by priority level
- Average wait time before processing
- Processing rate per worker
- Job execution time percentiles (p50, p95, p99)
- Success and failure rates
- Worker utilization and throughput
- Redis connection status
- Worker pool capacity
- Memory and CPU usage
Offload blocking tasks to workers to improve response times:
func SignupHandler(w http.ResponseWriter, r *http.Request) {
client.SubmitJob("email", EmailPayload{
To: newUser.Email,
Subject: "Welcome!",
Template: "welcome",
Data: map[string]interface{}{ "username": newUser.Name },
})
w.WriteHeader(http.StatusCreated)
}Decoupled job submission for multi-service workflows:
- Service A submits job
- Worker executes
- Another job enqueued to notify user
- Worker processes notification
- Priority queues ensure critical tasks first
- Rate limiting prevents overload
- Worker pools scale with system resources
- Retries with exponential backoff
- DLQ stores jobs that exceed retries
- Operators can requeue after fixing issues
func ProcessOrderHandler(w http.ResponseWriter, r *http.Request) {
// Submit multiple jobs for order processing
client.SubmitJob("payment_processing", paymentData)
client.SubmitJob("inventory_update", inventoryData)
client.SubmitJob("order_confirmation_email", emailData)
// Return immediately to user
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(map[string]string{
"status": "order_received",
"order_id": orderID,
})
}- Video transcoding for multiple formats
- Thumbnail generation at various resolutions
- Metadata extraction and storage
- CDN distribution preparation
- Transaction processing and validation
- Regulatory report generation
- Risk assessment calculations
- Customer notification workflows
- Redis atomic operations: Exactly-once processing
- Worker pool pattern: Concurrency management
- Circuit breaker pattern: Fault tolerance
- Prometheus metrics: Real-time monitoring
- OpenTelemetry: Distributed tracing
Gopher provides a robust, scalable solution for distributed job processing that addresses common challenges in modern applications. Its architecture ensures reliable task execution while maintaining system responsiveness and providing comprehensive monitoring capabilities. By implementing proven patterns like priority queues, retry mechanisms, and circuit breakers, Gopher enables developers to build resilient, high-performance systems with confidence.